无法获取当前位置Xamarin.Forms

时间:2018-10-11 11:41:59

标签: xamarin xamarin.forms

我在Xamarin表单应用程序中使用的GeoLocator也实现了Permissions插件。但是在Android locator.GetPositionAsync(TimeSpan.FromSeconds(10));上显示异常A geolocation error occurred: Unauthorized

Manifest.xml

<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="27" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

AssemblyInfo.cs

[assembly: UsesPermission(Android.Manifest.Permission.AccessCoarseLocation)]
[assembly: UsesPermission(Android.Manifest.Permission.AccessFineLocation)]
[assembly: UsesFeature("android.hardware.location", Required = false)]
[assembly: UsesFeature("android.hardware.location.gps", Required = false)]
[assembly: UsesFeature("android.hardware.location.network", Required = false)]

Info.plist

<key>NSLocationWhenInUseUsageDescription</key>
<string>Location access is required to serve you better</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Location access is required to serve you better</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Location access is required to serve you better</string>

1 个答案:

答案 0 :(得分:1)

我实际上今天正在尝试解决相同的问题,可能已经找到了解决方法。

首先,请确保将权限 Location 插件安装到所有项目(包括.NET Standard共享项目和平台项目)中。

我还使用了最新的Beta版本,但这似乎引起了问题,并抛出了off异常,即使直接检查导致了Unauthorized。安装稳定版本(4.5.0.6)后,它开始按预期运行。