如何使用Xamarin MVVM获取Android按钮上的当前位置?

时间:2016-01-08 21:19:51

标签: c# android mobile mvvm xamarin

如何使用Xamarin MVVM获取Android按钮上的当前位置?

我正在尝试使用GPS坐标进行后台处理,我需要在用户点击按钮时获取当前设备位置,而我不希望在我的UI上显示任何位置,尝试了几种东西但没有任何效果,任何人都可以帮我解决问题吗?

2 个答案:

答案 0 :(得分:0)

您可以通过多种方式获取位置。此问题的按钮单击方面仅仅是遵循您设置的MVVM中的相应命令模式的事件处理程序。

MVVMLight中的命令示例(一个支持Xamarin的流行MVVM库):

https://msdn.microsoft.com/en-us/magazine/dn237302.aspx

让我们了解一下获取GPS坐标的方法:

1)本地通过LocationManager

http://developer.android.com/guide/topics/location/strategies.html

http://developer.android.com/reference/android/location/LocationManager.html

2)在已经提供此功能的Xamarin库中(内部使用LocationManager

https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Geolocator

答案 1 :(得分:0)

您需要启动位置监听器并等待一段时间。然后点击更改将被解雇。然后,只有我们能够捕获用户的当前位置。

我更喜欢显示进度对话框并启动线程来监听位置更改。 不要使用任何xamarin第三方插件。使用原生Location Manager

整合以下代码。

 [Activity(Label = "Location Activity", MainLauncher = false, Icon = "@drawable/icon")]
 public class GetLoationActivity : BaseActivity, Android.Locations.ILocationListener
    {
        Location userLocation;
        Button getLocationButton = FindViewById<Button>(Resource.Id.getLocationButton);
        getLocationButton.LongClick += (e, d) =>
            {
                //TODO:Show Progress Dialog 
                var _LocationManager = LocationContext.GetSystemService(Context.LocationService) as LocationManager;
                var LocationChangedCalled = false;
                var PublishAwayFenceThread = new Thread(new ThreadStart(delegate
                    {
                        StartLocationChangeListener(this);
                    }
                    }));
                PublishAwayFenceThread.Start();
            };

    }

    public void StartLocationChangeListener(Activity activity)
    {
        try
        {
            var locationCriteria = new Criteria();
            locationCriteria.Accuracy = Accuracy.Coarse;
            locationCriteria.PowerRequirement = Power.Medium;
            string locationProvider = Helper.Instance._LocationManager.GetBestProvider(locationCriteria, true);
            if (!String.IsNullOrEmpty(locationProvider))
            if (activity!= null)
            {
                activity.RunOnUiThread(() =>
                    {
                        var _LocationManager = LocationContext.GetSystemService(Context.LocationService) as LocationManager;
                        _LocationManager.RequestLocationUpdates(locationProvider, 1000, 1, this);
                        Console.WriteLine("****---------*****Location Listener Started****---------*****");
                    });
            }
        }
        catch (Exception e)
        {

        }
    }

    public void OnLocationChanged(Location location)
    {
        try
        {
            userLocation = location;
            //TODO:Hide Loader
            StopLocationChangeListener();
            Console.WriteLine("****---------*****Location changed fired****---------*****");
            Console.WriteLine("****---------*****" + location.Latitude + "," + location.Longitude + "****---------*****");
        }
        catch (Exception e)
        {

        }
    }

    public void StopLocationChangeListener()
    {
        try
        {
            Activity _LocationContextAcitivity = (Activity)Helper.Instance.LocationContext;
            _LocationContextAcitivity.RunOnUiThread(() =>
                {
                    var _LocationManager = LocationContext.GetSystemService(Context.LocationService) as LocationManager;
                    _LocationManager.RemoveUpdates(this);
                    Console.WriteLine("****---------*****Location Listener stopped****---------*****");
                });
        }
        catch (Exception e)
        {
        }
    }