Xamarin.forms CrossGeolocator.Current.IsGeolocationEnabled始终返回false
我的要求是从gps获取纬度和经度,在我更新NuGet软件包和android sdk之前,它一直在工作。即使位置服务仍然打开,我也无法访问该位置 var定位器=CrossGeolocator.CurrentXamarin.forms CrossGeolocator.Current.IsGeolocationEnabled始终返回false,xamarin.forms,Xamarin.forms,我的要求是从gps获取纬度和经度,在我更新NuGet软件包和android sdk之前,它一直在工作。即使位置服务仍然打开,我也无法访问该位置 var定位器=CrossGeolocator.Current if (locator.IsGeolocationEnabled) { locator.DesiredAccuracy = 15; var position = awa
if (locator.IsGeolocationEnabled)
{
locator.DesiredAccuracy = 15;
var position = await
locator.GetPositionAsync(TimeSpan.FromMilliseconds(3000));
Latitude = position.Latitude;
Longitude = position.Longitude;
}
这里if语句返回的总是false
我的AndroidManifest看起来像这样
您可能正在使用过时的xamarin forms>4.0回购协议。此项目已移至Xamarin.Essentials。我强烈建议使用这个 然后在代码中可以使用
internal static async Task<Location> GetDeviceLocation()
{
try
{
var request = new GeolocationRequest(GeolocationAccuracy.Medium);
var location = await Geolocation.GetLocationAsync(request);
return location;
}
catch (Exception ex)
{
Helpers.ShowError("could not locate device: " + ex.Message, 6000);
}
return null;
}
您必须使用才能访问GPS,仅在清单中定义权限将不允许您使用GPS Ref:谢谢,我如何知道设备GPS是打开还是关闭?除了try catch之外,是否有其他方法检查第一个功能是否已启用?
try
{
var location = await Geolocation.GetLastKnownLocationAsync();
if (location != null)
{
Console.WriteLine($"Latitude: {location.Latitude}, Longitude: {location.Longitude}, Altitude: {location.Altitude}");
}
}
catch (FeatureNotSupportedException fnsEx)
{
// Handle not supported on device exception
}
catch (FeatureNotEnabledException fneEx)
{
// Handle not enabled on device exception
}
catch (PermissionException pEx)
{
// Handle permission exception
}
catch (Exception ex)
{
// Unable to get location
}