[英]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.