简体   繁体   中英

Can not get current location Xamarin.Forms

I am using GeoLocator in my Xamarin forms application also implemented Permissions Plugin. But on android locator.GetPositionAsync(TimeSpan.FromSeconds(10)); It shows exception 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>

I was actually trying to solve the same problem today and may have found a solution.

First of all, make sure you install both Permissions and Location plugin into all your projects (including the .NET Standard shared project and platform projects).

Also, I was using the latest beta version, but that seemed to be causing issues and threw the Unauthorized exception even though a direct check resulted in Granted . After installing the stable version (4.5.0.6), it started working as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM