如何从 Xamarin.Forms 获得连续的地理位置?
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;
});
MainMethodProcess:
...
Location location = new Location();
location = await Geolocation.GetLocationAsync(request);
...
如果我将此行为与另一个使用 GPS 定位的应用程序进行比较,则只需激活一次 GPS 信号即可。我做错了什么?
这些是结果:
据我所知,Xamarin.Essentials 没有位置侦听器来检测后台的位置变化。
一个解决方案是使用来自 jamesmontemagno 的 Geolocator Plugin。
这里有一个如何使用它的例子(source)
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;
});
}
我正在尝试使用 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;
});
MainMethodProcess:
...
Location location = new Location();
location = await Geolocation.GetLocationAsync(request);
...
如果我将此行为与另一个使用 GPS 定位的应用程序进行比较,则只需激活一次 GPS 信号即可。我做错了什么?
这些是结果:
据我所知,Xamarin.Essentials 没有位置侦听器来检测后台的位置变化。 一个解决方案是使用来自 jamesmontemagno 的 Geolocator Plugin。
这里有一个如何使用它的例子(source)
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;
});
}