Xamarin Forms Geolocator 持续更新位置
Xamarin Forms Geolocator Continuously Updating the Location
我希望不断更新设备在 Google 地图上的当前位置。
由于各种原因,我不得不实现自己的蓝点。我所做的是:
我隐藏了Google地图的默认我的位置标记(默认蓝点)。
我自己画的蓝点
我创建了一个不断调用地理定位器更新其当前位置的线程:await locator.GetPositionAsync(TimeSpan.FromSeconds(2), null, false);
但是,位置更新需要很长的延迟。我希望以非常低的间隔(大约 50 毫秒)更新位置,这可以给我一个与设备移动时 Google 地图的原始 蓝点 类似的结果.
我怎样才能做到这一点?
我发现CrossGeolocator
有一个监听功能如果有位置更新会提示。此函数不会一直调用当前位置,而是会持续监听任何位置变化。
if (CrossGeolocator.IsSupported &&
CrossGeolocator.Current.IsGeolocationAvailable &&
CrossGeolocator.Current.IsGeolocationEnabled)
{
CrossGeolocator.Current.PositionChanged += HandlePositionChanged;
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 10);
}
我希望不断更新设备在 Google 地图上的当前位置。
由于各种原因,我不得不实现自己的蓝点。我所做的是:
我隐藏了Google地图的默认我的位置标记(默认蓝点)。
我自己画的蓝点
我创建了一个不断调用地理定位器更新其当前位置的线程:
await locator.GetPositionAsync(TimeSpan.FromSeconds(2), null, false);
但是,位置更新需要很长的延迟。我希望以非常低的间隔(大约 50 毫秒)更新位置,这可以给我一个与设备移动时 Google 地图的原始 蓝点 类似的结果.
我怎样才能做到这一点?
我发现CrossGeolocator
有一个监听功能如果有位置更新会提示。此函数不会一直调用当前位置,而是会持续监听任何位置变化。
if (CrossGeolocator.IsSupported &&
CrossGeolocator.Current.IsGeolocationAvailable &&
CrossGeolocator.Current.IsGeolocationEnabled)
{
CrossGeolocator.Current.PositionChanged += HandlePositionChanged;
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 10);
}