2017-07-31 89 views
0

我正在使用谷歌地圖SDK與swift 3.我想獲取當前位置的地圖,但我無法獲得當前位置的真實設備,但在模擬器,它顯示了蘋果的位置。但是在真實設備上,它顯示了應用程序方案中的當前位置,否則顯示錯誤「錯誤:錯誤域= kCLErrorDomain代碼= 0」(空)「」。我使用下面的代碼來獲取當前location.Please幫助me.This代碼在IOS 10,但在IOS 9使用谷歌地圖獲取ios 9中的當前位置sdk

@IBOutlet weak var view_map: GMSMapView! 
    let locationManager = CLLocationManager() 
    var didFindMyLocation = false 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     view_map.isMyLocationEnabled = true 
//  //Location Manager code to fetch current location 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestWhenInUseAuthorization() 
     if #available(iOS 9.0, *) { 
      locationManager.allowsBackgroundLocationUpdates = true 
      locationManager.requestLocation() 

     } 
     locationManager.startUpdatingLocation() 
    } 
    //Location Manager delegates 
     func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let location = locations.last 

     let camera = GMSCameraPosition.camera(withLatitude: (location?.coordinate.latitude)!, longitude: (location?.coordinate.longitude)!, zoom: 17.0) 

     self.view_map.animate(to: camera) 

     //Finally stop updating location otherwise it will come again and again in this delegate 
     self.locationManager.stopUpdatingLocation() 

    } 
    // Handle authorization for the location manager. 
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { 
     switch status { 
     case .restricted: 
      print("Location access was restricted.") 
     case .denied: 
      print("User denied access to location.") 
      // Display the map using the default location. 
      view_map.isHidden = false 
     case .notDetermined: 
      print("Location status not determined.") 
     case .authorizedAlways: fallthrough 
     case .authorizedWhenInUse: 
      print("Location status is OK.") 
     } 
    } 

    // Handle location manager errors. 
    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 
     locationManager.stopUpdatingLocation() 
     print("Error: \(error)") 
    } 
+0

相關不起作用:https://stackoverflow.com/questions/29312453/ ios-8-cant-get-current-location-error-domain-kclerrordomain-code-0 https://stackoverflow.com/questions/1409141/location-manager-error-kclerrordomain-error-0 https:// stackoverflow。 com/questions/32543754/ios-9-error-domain-kclerrordomain-code-0-null –

+0

我會嘗試所有,但不能解決我的問題 –

+0

您是否檢查了您的.plist中所需的密鑰? –

回答

0
//Add these in your info.plist 
<key>NSLocationAlwaysUsageDescription</key> 
    <string>Reason to access location background usage description</string> 
    <key>NSLocationWhenInUseUsageDescription</key> 
    <string>Reason to access location usage description</string> 
+0

我也將它添加到我的info.plist中 –

+0

我認爲GMSMapView不需要CLLocationManager。當您調用view_map.startRendering()時,它將自動開始更新。 –

相關問題