Xamarin:IOS中的geoLocator中的监听问题

时间:2018-11-19 06:34:07

标签: ios xamarin.ios geolocation

我在xamarin.so中使用GeoLocator作为当前位置,所以当我开始收听时,它显示了我的当前位置,但是当我暂停并再次开始时,它显示lat = 0,long = 0 仅在IOS中使用。(在Andorid中,一切正常。) 这是我的代码:

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

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

public void Start_button()
{
CurrentLocation();
}
public async void Pause_button()
{
 await CrossGeolocator.Current.StopListeningAsync();
}

请帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

在某些情况下,您会将插件安装到尚不支持的平台上。这意味着您将有权访问该接口,但是不存在任何实现。您可以在调用任何API之前进行简单的检查,以查看运行代码的平台是否支持它。

public bool IsLocationAvailable()
{
 if(!CrossGeolocator.IsSupported)
    return false;

 return CrossGeolocator.Current.IsGeolocationAvailable;
}

在iOS中,您的应用必须在Info.plist中的NSLocationWhenInUseUsageDescription中具有密钥,才能访问设备的位置。

<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to location when open.</string>
  

背景更新

仅当您需要对应用程序进行后台更新时,才实施此操作并添加这些属性。您极有可能不会。添加它也直接影响权限和对用户的提示。添加此信息时请非常小心。

在info.plist内,您必须启用Background Modes / UIBackgroundModes进行位置更新。这是完整的指南。您的info.plist应该包含以下内容:

<key>UIBackgroundModes</key>
  <array>
    <string>location</string>
  </array>

除了NSLocationWhenInUseUsageDescription之外,您还需要在应用程序的Info.plist文件中添加NSLocationAlwaysAndWhenInUseUsageDescription键。 (如果您的应用支持iOS 10及更低版本,则还需要NSLocationAlwaysUsageDescription密钥。)如果这些密钥不存在,授权请求将立即失败。

<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access to location when in the background.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>This app needs access to location when open and in the background.</string>

有关更多详细信息,请参阅Github demo