2014-11-04 76 views
0

我試圖通過此示例的幫助獲取設備位置https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Geolocator。這適用於模擬器,但在iPad Air上不起作用。包含所有必要的鍵位置我的Info.plist文件。,NSLocationUsageDescription和NSLocationWhenInUseUsageDescription地理定位不起作用iOS PCL Xamarin表格

在代碼

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。

回答

1

所以這工作正常在iPad空氣模擬器?這聽起來像是你正在使用的設備特有的問題。也許有人在允許位置彈出窗口上點擊「取消」?

轉至Settings->General->Restrictions->Location Services。確保所有內容都已打開並允許使用您的應用。我也會嘗試其他應用,並確保位置在該應用中工作。

0

您是否嘗試過沒有Xlabs geoocator服務,因爲上次我嘗試過,它是越野車。

旁邊,在你的代碼我沒有看到你武官到PositionChanged事件處理程序:

_geolocator.PositionChanged += OnPositionChanged; 

如果你想連續位置UPADTE這是必要的。

不使用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; 
    }