2017-04-12 94 views
0

我一直在這個問題上一整天都陷入困境。我的應用程序沒有在用戶選擇「允許位置在使用中」導致任何東西,但零到處都沒有出現的位置更新位置返回零

最初我使用mapView.userLocation作爲一種方式來查找用戶的位置,但是當我開始使用CLLocationManager停止工作。

從頂部導入CoreLocation並在我的類中添加CLLocationManagerDelegate。

以上的viewDidLoad中()我有 讓的LocationManager = CLLocationManager(CLLocationManager爲) 並在viewDidLoad中我有 locationManager.delegate =自 locationManager.desiredAccuracy = kCLLocationAccuracyBest

現在,我會有關檢查方法閹用戶已經批准或者如果他們剛剛拒絕它是通過該方法

switch CLLocationManager.authorizationStatus(){ 
case.authorizedWhenInUse: 
     print(mapView.userLocation?.coordinate) 
     mapView.setCenter((mapView.userLocation?.coordinate)!, zoomLevel: 12, animated: true) 
     break 
    case.notDetermined: 
     locationManager.requestWhenInUseAuthorization() 
     break 
    case .restricted, .denied: 
     let alertController = UIAlertController(
      title: "Background Location Access Disabled", 
      message: "In order to access this feature you need to allow to access your location while in use", preferredStyle: .alert) 
     let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil) 
     alertController.addAction(cancelAction) 

     let openAction = UIAlertAction(title: "Open Settings", style: .default, handler: {(action) in 
      if let url = NSURL(string:UIApplicationOpenSettingsURLString) { 
       UIApplication.shared.openURL(url as URL) 
      } 
     }) 
     alertController.addAction(openAction) 
     self.present(alertController, animated: true, completion: nil) 
     break 
    default: locationManager.requestWhenInUseAuthorization() 
    } 

我在三個部分,一個在用戶按下一個按鈕,它會放大噸有這個繼承人當前位置,其中一個搜索用戶,最後一個位於viewDidLoad中。如果用戶從提示中選擇選項並將其發送到其設置,並且他們更改其位置設置,那麼當他們重定向回我的應用程序時,什麼都不會發生,如果我執行涉及用戶位置的任何事情,它都會崩潰。

我嘗試過使用startUpdatingLocation,但是根本沒有做任何事情,當我嘗試從mapView.userLocation捕獲用戶位置時,它只是以零的形式出現。

我一直在打我的頭,這一切的一天,因此任何形式的幫助,將不勝感激

+1

您是否在plist中添加NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription? – karthikeyan

+0

您在哪裏編寫mapView.userLocation? –

+0

你在哪裏登記設備?或在模擬器? –

回答

0

試試這個:在模擬器選擇調試 - >位置 - >有的位置。並重新啓動你的應用程序