【问题标题】:Get device location in latitude-longitude in xamarin forms以 xamarin 形式获取经纬度的设备位置
【发布时间】:2016-11-03 09:39:24
【问题描述】:

我的应用程序中有一个扫描仪,当我扫描任何二维码时,我需要以经纬度获取设备的当前位置。我不知道如何获取位置,所以我现在没有任何代码。建议我一些在扫描完成 QR 码时获取位置的方法。

【问题讨论】:

    标签: c# location xamarin.forms xamarin-studio


    【解决方案1】:

    地理定位器插件示例:

    var locator = CrossGeolocator.Current;
    locator.DesiredAccuracy = 50;
    var position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);
    Console.WriteLine ("Position Status: {0}", position.Timestamp);
    Console.WriteLine ("Position Latitude: {0}", position.Latitude);
    Console.WriteLine ("Position Longitude: {0}", position.Longitude);
    

    参考:https://github.com/jamesmontemagno/GeolocatorPlugin

    Nuget:https://www.nuget.org/packages/Xam.Plugin.Geolocator

    【讨论】:

    • 我在我的 OnAppearing 方法中使用了这个方法,但是在 GetPositionAsync 之后 App 崩溃了,你知道它会是什么吗?
    • 嗨,我在我的 xamarin android 项目中使用这个插件,但我收到任务取消异常。我已经在模拟器和真实设备上进行了测试,都得到了相同的结果。
    • 确保在 Android 解决方案属性中选中 ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION
    • 嗨@MichaelYuwono,我正在尝试在应用睡眠模式下获取当前位置的位置。但我无法使用此代码获取它,var position = await locator.GetPositionAsync();请帮助我实现这一目标。
    【解决方案2】:

    对于对这个位置的东西感到困惑的每个人,我建议使用 Xamarin Essentials 而不是上面提到的插件。

                var location = await Geolocation.GetLastKnownLocationAsync();                                
                if (location != null)
                {
                    MyLocation = location;
                    if (!ShowUserLocation)
                        ShowUserLocation = true;
                }
    

    【讨论】:

    【解决方案3】:

    我推荐 Xamarin Essentials。 查看这篇文章并检查您的权限。 https://docs.microsoft.com/en-us/xamarin/essentials/geolocation?tabs=android

    var Latitude;
    var Longitude;    
    var request = new GeolocationRequest(GeolocationAccuracy.Best);
                        var location = await Geolocation.GetLocationAsync(request);
    
                        if (location != null)
                        {
                            if (location.IsFromMockProvider)
                            {
                                //Put a message if detect a mock location.
                            }else
                            {
                                Latitude=location.Latitude;
                                Longitude=location.Longitude;
                            }
                        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-04
      • 1970-01-01
      • 2018-03-30
      • 2021-09-29
      相关资源
      最近更新 更多