[英]Geolocation doesn't work iOS PCL Xamarin forms
我正在嘗試借助此示例https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator來獲取設備位置。 這適用於模擬器,但不適用於iPad Air。 我的info.plist文件包含位置所有必需的鍵,NSLocationUsageDescription和NSLocationWhenInUseUsageDescription
在代碼中
if (this.manager.RespondsToSelector(new Selector("requestWhenInUseAuthorization")))
{
this.manager.RequestWhenInUseAuthorization();
}
使用Geolocator對象的一段代碼。
IGeolocator geolocator = null;
geolocator = DependencyService.Get<IGeolocator>();
if (geolocator != null)
{
if (geolocator.IsGeolocationEnabled)
{
try
{
if (!geolocator.IsListening)
geolocator.StartListening(1000, 1000);
var task = await geolocator.GetPositionAsync(10000, CancellationToken.None);
if(task != null)
{
this.Position = task;
}
但是LocationUpdated事件永遠不會觸發……
設備上的位置已開啟。
如果有人有類似的問題.......請幫助.....一次,這項工作在模擬器上進行。
我在iPad Air上使用VS 2013,Xamarin 3.7,Xamarin.iOS 8.4,iOS 8.1。
所以這在iPad空氣模擬器中可以正常工作嗎? 聽起來像是您所使用設備的特定問題。 也許有人在允許位置彈出窗口上點擊了“取消”?
轉到Settings->General->Restrictions->Location Services
。 確保所有功能均已打開並允許您的應用程序使用。 我還會嘗試另一個應用程序,並確保該應用程序中的位置正在運行。
您是否在沒有Xlabs geoocator服務的情況下進行了嘗試,因為上次嘗試時,它存在問題。
另外,在您的代碼中,我看不到您附加到PositionChanged事件處理程序:
_geolocator.PositionChanged += OnPositionChanged;
如果要連續進行位置調整,則需要此方法。
如果不使用XLabs,我會做這樣的事情:
public bool Start(ActivityType activity = ActivityType.Other, bool alsoWhenInBackground = false, LocationAccuracy accuracy = LocationAccuracy.AccurracyBestForNavigation,
double minDistanceBetweenUpdatesInMeters = 0, TimeSpan? maxDelayBetweenUpdates = null)
{
if (manager != null)
return true;
manager = new CLLocationManager();
manager.Failed += (sender, args) => FireError(args.Error.ToString());
manager.LocationsUpdated += (sender, args) => FireLocationUpdated(args.Locations);
//manager.AuthorizationChanged
manager.ActivityType = (CLActivityType)activity;
manager.DesiredAccuracy = Accuracies[(int)accuracy];
if (maxDelayBetweenUpdates != null || minDistanceBetweenUpdatesInMeters > double.Epsilon)
manager.AllowDeferredLocationUpdatesUntil(minDistanceBetweenUpdatesInMeters, (maxDelayBetweenUpdates ?? TimeSpan.Zero).TotalSeconds);
if (alsoWhenInBackground)
manager.PausesLocationUpdatesAutomatically = true;
//Required: ask for authorization
if (AuthorizationStatus == AuthorizationStatus.NotDetermined)
AskAuthorization(alsoWhenInBackground);
var authStatus = AuthorizationStatus;
if (authStatus < AuthorizationStatus.AuthorizedAlways && authStatus != AuthorizationStatus.NotDetermined)
{
System.Diagnostics.Debug.WriteLine("user denied access to location");
return false;
}
if (authStatus == AuthorizationStatus.AuthorizedWhenInUse && alsoWhenInBackground)
{
System.Diagnostics.Debug.WriteLine("alsoWhenInBackground is true, but user denied access to location in background");
return false;
}
manager.StartUpdatingLocation();
return true;
}
public bool AskAuthorization(bool alsoWhenInBackground = false)
{
if (AuthorizationStatus != AuthorizationStatus.NotDetermined)
return false;
if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
if (alsoWhenInBackground)
manager.RequestAlwaysAuthorization();
else
manager.RequestWhenInUseAuthorization();
}
return true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.