【问题标题】:Geo Locator in xamarin formsxamarin 形式的地理定位器
【发布时间】: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)
            {

            }

【问题讨论】:

  • 每次我没有得到位置时,我都试图获取大厅的经纬度,但是当我尝试在大厅的一侧时,纬度和经度就会出现。有人有解决这个问题的办法吗?
  • 这些权限:ACCESS_COARSE_LOCATION 和 ACCESS_FINE_LOCATION 已经启用。

标签: c# geolocation xamarin.forms


【解决方案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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多