【问题标题】:How to get the current location on a button click in android using Xamarin MVVM?如何使用 Xamarin MVVM 在 android 中单击按钮获取当前位置?
【发布时间】:2016-01-08 21:19:50
【问题描述】:

如何使用 Xamarin MVVM 在 android 中单击按钮获取当前位置?

我正在尝试使用 GPS 坐标进行后台处理,我需要在用户单击按钮时获取当前设备位置,并且我不想在我的 UI 上的任何位置显示它,尝试了一些东西但没有任何效果,谁能帮我解决这个问题?

【问题讨论】:

    标签: c# android mobile mvvm xamarin


    【解决方案1】:

    您可以通过多种方式获取位置信息。这个问题的按钮点击方面仅仅是一个事件处理程序,遵循您设置的 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

    【讨论】:

    • 您好 Jon 尝试使用 Xamarin 库,但我无法成功,您有相同的示例吗?也尝试使用 xamarin.Mobile.dll 和其他
    • 尝试使用 Xam.Plugin.Geolocator 无法成功获取坐标
    【解决方案2】:

    您需要启动位置侦听器并等待一段时间。然后将触发 onlocation 更改。只有我们才能捕捉到用户的当前位置。

    我更喜欢显示进度对话框并启动线程来监听位置变化。 不要使用任何 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)
            {
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 2017-06-19
      • 2019-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      相关资源
      最近更新 更多