android - Plugin.Geolocator 授权错误

标签 android xamarin xamarin.forms geolocation

我想获取设备的 GPS 位置。我正在使用 Xamarin 的 GeolocatorPlugin 来获取我设备的经度和纬度。我的设备启用了 GPS。我已经向我的 android list 添加了权限,但我仍然收到错误消息:“发生地理定位错误:未经授权”我该如何解决这个问题?

try
{
   var locator = CrossGeolocator.Current;
   locator.DesiredAccuracy = 50;

    var position = await locator.GetPositionAsync();
    entLocation.Text = position.Longitude.ToString() + "," + position.Latitude.ToString();
}
catch (Exception ex)
{
    //will catch any general exception and set message in a string
    string msg = ex.Message;
    await DisplayAlert("error", msg, "ok");
}

最佳答案

将下面的代码放入您的 Android 项目中的 MainActivity.cs 中。

if(CheckSelfPermission(Manifest.Permission.AccessCoarseLocation) != (int)Permission.Granted)
{
    RequestPermissions(new string[] { Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 0);
}

关于android - Plugin.Geolocator 授权错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51869620/

相关文章:

firebase - Xamarin.ios native Firebase 推送通知

c# - 如何从 Xamarin Forms View 创建 native UWP View ?

c# - Visual Studio 2017企业版是否提供Xamrin Page的设计?

android - Intentservice 不调用 runnable

android - 创建一个在 UI 线程 Android 中更新的秒表

android - 考虑添加 android :paddingStart ="25dp" to better support right-to-left layouts Error

c# - xamarin 中的 "layout renderer failed"错误

ios - View Controller Segue Xamarin

c# - Xamarin - Prism : How do I pass the whole ViewModel of the ContentPage to a child ContentView

android - 在 Android 中查询噪音水平