xamarin 形式的地理定位器

Posted

技术标签:

【中文标题】xamarin 形式的地理定位器【英文标题】:Geo Locator in xamarin forms 【发布时间】:2016-09-08 08:15:31 【问题描述】:

我正在使用 xamarin 表单开发 android 应用程序。这是我用于获取纬度和经度的代码。但是当我尝试使用模拟器时,它每次都有效,但移动设备有时有效,有时无效。请帮我。

            var locator = Geolocator.Plugin.CrossGeolocator.Current;
            locator.DesiredAccuracy = 50;

            try
            
                var locator2 = Geolocator.Plugin.CrossGeolocator.Current;
                locator2.DesiredAccuracy = 50;

                var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);

                if (position == null)
                

                    return;
                
            
            catch (Exception ex)
            

            

【问题讨论】:

每次我没有得到位置时,我都试图获取大厅的经纬度,但是当我尝试在大厅的一侧时,纬度和经度就会出现。有人有解决这个问题的办法吗? 试试这个***.com/questions/39210048/… 这些权限:ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION 已经启用。 【参考方案1】:

在这个页面https://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/你可以看到

注意:设置 ACCESS_FINE_LOCATION 意味着可以访问粗略和精细的位置数据。您永远不必同时设置这两种权限,只需设置您的应用运行所需的最低权限即可。

【讨论】:

【参考方案2】:

在 android MainActiviy OnCreate() 中添加这个

Plugin.CurrentActivity.CrossCurrentActivity.Current.Activity = this;

将此添加到android mainactivity:

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Android.Content.PM.Permission[] grantResults)

    Plugin.Permissions.PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);

然后此代码将起作用,提示用户提供位置权限。

                var locator = CrossGeolocator.Current;
                locator.DesiredAccuracy = 1000;
                var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);

参考:

https://jamesmontemagno.github.io/GeolocatorPlugin/

【讨论】:

以上是关于xamarin 形式的地理定位器的主要内容,如果未能解决你的问题,请参考以下文章

Xamarin - 假地理位置

如何扩展地理定位 Web 应用程序

以 xamarin 形式获取经纬度的设备位置

将地理定位从 Swift 2 ViewController 传递给 Javascript 方法

在 Celery 任务中使用 GeoIP 进行地理定位的正确方法

“原生”地理定位与“基于PhoneGap”的地理定位功能