[英]How can I get a continous geolication from Xamarin.Forms?
我正在尝试使用 Xamarin Essentials 连续获取设备的位置,但他的行为很奇怪。 每次我使用 GetLocationAsync 方法(就像闪烁模式)时,都会激活和禁用 GPS 信号。
这是我的代码:
主线:
Xamarin.Forms.Device.StartTimer(TimeSpan.FromMilliseconds(gpsFrequencyRead, () =>
{
Xamarin.Forms.Device.BeginInvokeOnMainThread(async () =>
{
if (!isBusy)
{
isBusy = true;
await MainMethodProcess();
isBusy = false;
}
});
return activeMainTimer;
});
主要方法过程:
...
Location location = new Location();
location = await Geolocation.GetLocationAsync(request);
...
如果我将此行为与另一个使用 GPS 位置的应用程序进行比较,则只需激活 GPS 信号一次即可。 我做错了什么?
这些是结果:
据我所知, Xamarin.Essentials没有位置侦听器来检测背景中的位置变化。 一个解决方案是使用 jamesmontemagno 的Geolocator 插件。
这是有关如何使用它的示例( 来源)
async Task StartListening()
{
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true, new Plugin.Geolocator.Abstractions.ListenerSettings
{
ActivityType =
Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation,
AllowBackgroundUpdates = true,
DeferLocationUpdates = true,
DeferralDistanceMeters = 1,
DeferralTime = TimeSpan.FromSeconds(1),
ListenForSignificantChanges = true,
PauseLocationUpdatesAutomatically = false
});
CrossGeolocator.Current.PositionChanged += Current_PositionChanged;
}
private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
var Position = e.Position;
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.