Mono for Android - LocationServices无法正常工作 [英] Mono for Android - LocationServices not working

查看:80
本文介绍了Mono for Android - LocationServices无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我的位置活动中有以下代码。

(此代码是从Xamarin的位置复制的服务演示)



Hi,

I have the following code in my location activity.
(this code was copied from Xamarin''s Location Services demo)

namespace LocationTutorialModified
{
    [Activity (Label = "LocationActivity", MainLauncher = true)]          
    public class LocationActivity : Activity, ILocationListener
    {
        LocationManager _locMgr;
     
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            SetContentView (Resource.Layout.LocationView);
            
            // use location service directly       
            _locMgr = GetSystemService (Context.LocationService) as LocationManager;

            _locMgr.RequestLocationUpdates("gps", 0, 0, this);

            Location lastKnownLocation = _locMgr.GetLastKnownLocation("gps");



            var locationText = FindViewById<TextView>(Resource.Id.locationTextView);

            if (lastKnownLocation != null)
            {
                locationText.Text = string.Format("Lat : {0}, Lon : {1}", lastKnownLocation.Latitude,
                    lastKnownLocation.Longitude);
            }
            else
            {
                locationText.Text = "Unaware of current location";
            }
        }
     
        protected override void OnResume ()
        {
            base.OnResume ();
         
            var locationCriteria = new Criteria ();
         
            locationCriteria.Accuracy = Accuracy.NoRequirement;
            locationCriteria.PowerRequirement = Power.NoRequirement;
         
            string locationProvider = _locMgr.GetBestProvider (locationCriteria, true);
            
            _locMgr.RequestLocationUpdates (locationProvider, 0, 0, this);
            //_locMgr.RequestLocationUpdates (LocationManager.GpsProvider, 2000, 1, this);
        }
        
        protected override void OnPause ()
        {
            base.OnPause ();
            
            _locMgr.RemoveUpdates (this);
        }

     #region ILocationListener implementation
        public void OnLocationChanged (Location location)
        {
            var locationText = FindViewById<TextView> (Resource.Id.locationTextView);
         
            locationText.Text += String.Format ("Latitude = {0}, Longitude = {1}", location.Latitude, location.Longitude);
         
            //// demo geocoder
         
            //new Thread (new ThreadStart (() => {
            //    var geocdr = new Geocoder (this);
             
            //    var addresses = geocdr.GetFromLocation (location.Latitude, location.Longitude, 5);
             
            //    //var addresses = geocdr.GetFromLocationName("Harvard University", 5);
             
            //    RunOnUiThread (() => {
            //        var addrText = FindViewById<TextView> (Resource.Id.addressTextView);
         
            //        addresses.ToList ().ForEach ((addr) => {
            //            addrText.Append (addr.ToString () + "\r\n\r\n");
            //        });
            //    });
             
            //})).Start ();
        }

        public void OnProviderDisabled (string provider)
        {
         
        }

        public void OnProviderEnabled (string provider)
        {
         
        }

        public void OnStatusChanged (string provider, Availability status, Bundle extras)
        {
         
        }
     #endregion


    }
}





配置选项:

1.我已设置项目属性 - >目标api到10,因为我正在运行android 2.3.3来自AVD经理的虚拟设备



2.我为 - >设置了项目属性Access_Fine / Coarse / Mock位置。项目构建后,它们已被放入AndroidManifest





当我部署应用程序时,我得到不知道当前位置(设置)正确,因为我们可以说当模拟器启动时,它不知道位置)。



我正在尝试发送DDMS - 使用模拟器控制选项卡模拟纬度和经度在DDMS工具中,仍然没有更新位置。



任何人都可以看到并告诉我出了什么问题。请建议任何修改,让我试一试。



谢谢。



Configuration Options:
1. I have set the project properties --> Target api to 10 since i''m running android 2.3.3 Virtual device from AVD manager

2. I have set project properties for --> Access_Fine / Coarse / Mock locations. They have been put in AndroidManifest after project build


When i deploy the applicaion, I get the "Unaware of current location" (set properly since lets say when the emulator starts, it doesnt know the location).

I''m trying to send DDMS - Mock latitude and longitude by using Emulator Control tab in DDMS tool, Still not getting the locations updated.

Can anyone please see and tell me what''s going wrong. Please suggest any modifications and let me try it.

Thanks.

推荐答案

工作解决方案:



主要问题:我运行的API 10设备(Android 2.3.3)不正确。

虽然它运行正常,但它不会接收DDMS模拟位置。



解决方案:将API(Android虚拟设备)更改为:Google Inc设备,API级别10(适用于Android 2.3.3)。



现在,DDMS将正确发送位置,Google Inc Api Device将捕获位置并显示它。此外,OnStatusChanged方法需要toast消息。如果没有,那么我的运行时间会崩溃(会发现原因)。



P.S.我知道我正在回答我自己的问题,但这对于处理来说是令人沮丧的。因此可以帮助某人。



谢谢Greg Shackles的解决方案。



Working Solution:

Main Issue: The API 10 Device i was running (Android 2.3.3) was not correct.
Although it runs properly, it will not receive DDMS mock locations.

Solution: Change API (Android Virtual Device) to : Google Inc Device, API Level 10 (for Android 2.3.3).

Now the DDMS will send locations properly and Google Inc Api Device, will catch locations and display it. Also, the OnStatusChanged method needs toast message. If not, then i''m getting run time crashes (will find out why).

P.S. I know i''m answering my own question, but this was frustrating to deal with. Hence might help someone.

Thank you Greg Shackles for your solutions.

namespace LocationDemo
{
    [Activity(Label = "LocationDemo", MainLauncher = true, Icon = "@drawable/icon")]
    public class LocationActivity : Activity, ILocationListener
    {
        private LocationManager _locationManager;


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            SetContentView(Resource.Layout.Main);

            var welcomeText = FindViewById<textview>(Resource.Id.welcomeMessage);

            _locationManager = (LocationManager)GetSystemService(LocationService);
        }

        protected override void OnResume()
        {
            base.OnResume();

            var criteria = new Criteria();
            criteria.PowerRequirement = Power.NoRequirement;
            criteria.Accuracy = Accuracy.NoRequirement;

            string bestProvider = _locationManager.GetBestProvider(criteria, true);

            _locationManager.RequestLocationUpdates(bestProvider, 5000, 20, this);
        }

        protected override void OnPause()
        {
            base.OnPause();

            _locationManager.RemoveUpdates(this);
        }

        public void OnLocationChanged(Location location)
        {
            //var currentLocation = new GeoPoint((int) (location.Latitude * 1e6), (int) (location.Longitude * 1e6));

            //_mapOverlay.Add(currentLocation, "Current Location");

            //_map.Controller.AnimateTo(currentLocation);

            var locationText = FindViewById<textview>(Resource.Id.welcomeMessage);

            locationText.Text = string.Format("Latitude : {0}, Longitude : {1}", location.Latitude,
                location.Longitude);
        }

        public void OnProviderDisabled(string provider)
        {
            // called when a provider is disabled
        }

        public void OnProviderEnabled(string provider)
        {
            // called when a provider is enabled
        }

        public void OnStatusChanged(string provider, Availability status, Bundle extras)
        {
            Toast
                .MakeText(this, "Status for " + provider + " changed to " + status, ToastLength.Short)
                .Show();
        }
    }
}


上面的代码,经过一些修改应该有效。但正如我在原帖中发布的解决方案所示,代码还可以,但模拟器就是问题。



首先我起诉Android 2.3这样做因某种原因无法工作。然后我为版本2.3安装了Google API,即... API级别10.当你启动模拟器时,不要使用说明Android 2.3 API级别10的那个,使用新的GOOGLE模拟器(再次使用版本) 2.3和API 10)。



对于谷歌设备,假设我是从SDK管理器安装Android 2.3。

To获取Google模拟器,安装子类别:Android 2.3.3 - > Google API也是。这将为您提供创建2个不同API级别10模拟器的选项。

1.最初的Android 2.3 API级别10(这对我不起作用)

2。谷歌API Android 2.3级别10(实际上通过捕获DDMS发送的位置,也可以在实际的Android手机上调试时工作)。



用于模拟手机,我也使用GOOGLE API设备。



希望这会有所帮助。我会在我的VS 2012副本上安装Mono for Android后立即发布代码更新。



你很高兴你学习。在我写这篇文章时,我几乎一无所知。它来自时间和研究(像往常一样)。



快乐编码..
The code above, with some modifications should work. But as the solution I have posted to my original post suggests, the code was ok, but the "emulator" was the problem.

First I sued Android 2.3 this does not work for some reason. Then I installed Google API''s for version 2.3, that is.. API level 10. When you start the emulator, don''t use the one that says Android 2.3 API level 10, use the new GOOGLE emulator (again with version 2.3 and API 10).

For Google devices, lets say if I''m installing Android 2.3 from the SDK manager.
To get the Google emulator for that, install the sub category: Android 2.3.3 --> Google API''s also. This will give you the option to create 2 different API level 10 emulators.
1. The original Android 2.3 API level 10 (which did not work for me)
2. The Google API Android 2.3 level 10 (which actually works by catching locations sent by DDMS, and also works while debugging on an actual android phone).

For simulations with the phone, I use GOOGLE API device as well.

Hope this helps. I will post a code update as soon as I''ve installed Mono for Android on my VS 2012 copy.

And It''s great that you''re learning. At the time I wrote this post, I pretty much didn''t know anything about it. It comes by time and research (as usual).

Happy coding..


这篇关于Mono for Android - LocationServices无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆