![](/img/trans.png)
[英]Xamarin iOS Geolocator Background Service Dying after about 30 mins
[英]Xamarin:Listening issue in geoLocator in IOS
我在xamarin.so中使用GeoLocator作為當前位置,所以當我開始收聽時,它顯示了我的當前位置,但是當我暫停並再次開始時,僅在IOS中顯示lat = 0,long = 0。(在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();
}
請幫助我,在此先感謝。
在某些情況下,您會將插件安裝到尚不支持的平台上。 這意味着您將有權訪問該接口,但是不存在任何實現。 您可以在調用任何API之前進行簡單的檢查,以查看運行代碼的平台是否支持該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的內部,必須啟用后台模式/ 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演示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.