具有完整功能的 Xamarin Forms Location [英] Xamarin Forms Location with Full features
问题描述
您好,我正在尝试以 Xamarin 形式构建一个应用程序,该应用程序对定位服务有很大的依赖性.
Hi I am trying to build one Application in Xamarin forms which has huge dependency on location services.
使用 Xamarin.Essentials 我能够获取位置,但在 Android 中它给出最后一个已知位置,有时它不是那么准确.
Using Xamarin.Essentials I am able to get the location, but in Android its give the last known location and sometime its not that much accurate.
问题 1.
但我无法添加位置侦听器,因此一旦用户更改位置,我就可以收到通知.
But I am not able to add Location Listener so that once user change location, I can get notified.
问题 2.如何准确知道用户是否禁用了电话位置或应用位置?
Problem 2. How to exactly know whether user has disabled Phone Location or App Location ?
请指导我.. 提前致谢
Please guide me.. Thanks in advance
推荐答案
问题 #1
我不知道,但我认为 Xamarin.Essentials 还没有位置侦听器功能,可能是错误的,但您可以使用这个 plugin 来自 jamesmontemagno(也是 Xamarin.Essentials 的主要开发者),这个插件可以像这样跟踪地理位置的变化:
I don't know, but i think Xamarin.Essentials doesn't have Location Listener feature yet, might be wrong, but you can use this plugin from jamesmontemagno (also a main developer of Xamarin.Essentials), this plugin can track geolocation changes like this:
CrossGeolocator.Current.IsListening
问题 2
遵循文档在这里,您会收到以下情况的例外:未授予权限或未启用/支持位置
Following the documentation here, you receive an exception for the following cases: permission was not granted or if the location is not enabled/supported
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
}
这篇关于具有完整功能的 Xamarin Forms Location的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!