2016-08-02 127 views
3

我想讓我的程序在地圖上顯示用戶的位置。它一開始工作,然後隨機停止,所以我添加了下面的代碼來嘗試修復它,但我遇到了問題。提前致謝!位置權限檢查和授權

override func viewDidLoad() 
    { 
     super.viewDidLoad() 
     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     if CLLocationManager.locationServicesEnabled() 
     { 

      let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus() 
      if status == CLAuthorizationStatus.NotDetermined 
      { 
       locationManager.requestAlwaysAuthorization() 
      } 
      } else { 

       print("locationServices disenabled") 
      } 
      locationManager.startUpdatingLocation() 
      self.locationManager.startUpdatingLocation() 
      self.mapView.showsUserLocation = true 
      mapView.delegate = self 
      centerMapOnLocation(initialLocation) 
      addBoundry() 
    } 
+0

你有什麼問題? – eMKa

+0

我們需要更多關於您面臨的問題的信息。另外我注意到有2個調用'locationManager'的'startUpdatingLocation'方法。 – tek3

+1

您是否在info.plist中設置了必填字段? – Rikh

回答

6

你需要打電話,

locationManager.requestAlwaysAuthorization() 

以及

locationManager.requestWhenInUseAuthorization() 

在前臺順手的位置也!

並且您不應該需要兩個實例才能撥打startupdatinglocation。保持一個。您應該使用實例或全局變量而不是本地來獲取整個範圍的位置。

更新:

你有info.plist一樣,NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription用它來設置兩個鍵的用法說明。

+0

謝謝我做了這些改變,但我仍然收到上面提到的錯誤 – Steve

+0

@Caleb:檢查我的更新回覆! – Lion

+0

我得到一個突破點,我現在請求 – Steve

2

我遇到了同樣的問題,即使在info.plist中設置了NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription。我的調試器告訴我也設置NSLocationAlwaysAndWhenInUseUsageDescription ...所以我做到了。有效!