Xamarin表格:GeoLocator问题

时间:2018-09-20 13:14:59

标签: xamarin geolocation mvvmcross

我有一个问题是我已将“时间”定义为10秒的地理位置定位器。然后也  Android:它会在10秒或10秒以上更新。在IOS中:它每秒钟更新一次。 这是我的代码:

     public async void CurrentLocation()
    {

        try
        {
            await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(10), 0, true, new Plugin.Geolocator.Abstractions.ListenerSettings
            {
                ActivityType = Plugin.Geolocator.Abstractions.ActivityType.AutomotiveNavigation,
                AllowBackgroundUpdates = true,
                DeferLocationUpdates = true,
                DeferralDistanceMeters = 1,
                //DeferralTime = TimeSpan.FromSeconds(10),
                ListenForSignificantChanges = false,
                PauseLocationUpdatesAutomatically = false

            });
            count++;
            CrossGeolocator.Current.PositionChanged += changedPosition;
        }

请给我一些解决方案。提前致谢。

1 个答案:

答案 0 :(得分:0)

关于代码https://github.com/jamesmontemagno/GeolocatorPlugin/blob/master/src/Geolocator.Plugin/Apple/GeolocatorImplementation.apple.cs#L369minimumTime尚未在iOS中实现(至少目前尚未实现)。

如果您将DeferralTime = TimeSpan.FromSeconds(10)设置为docs状态,则在后台工作应该会起作用:

  

摘要:如果推迟位置更新,则交付更新之前应经过的最短时间(> = iOS 6)。设置为null表示无限期等待。默认值:5分钟

     

值:更新之间的时间

因此,要解决前台的情况,可以跳过PositionChanged处理程序中不需要的位置:

Plugin.Geolocator.Abstractions.Position lastPosition = null;
var timePeriod = TimeSpan.FromSeconds(10);

private void ChangedPosition(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
{
    var lapsed = e.Position.Timestamp - lastPosition.Timestamp;
    lastPosition = e.Position;

    if (lapsed < timePeriod)
        return;

    // your logic
}

这是在Android implementation中完成该任务的基本操作。

HIH