我正在使用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)
{
}
答案 0 :(得分:0)
在此页https://developer.xamarin.com/guides/android/platform_features/maps_and_location/location/上,您可以看到
注意:设置ACCESS_FINE_LOCATION意味着可以访问粗略和精细位置数据。您永远不必设置两个权限,只需设置您的应用所需的最低权限。
答案 1 :(得分:0)
将此内容添加到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);
请参阅: