2017-07-31 37 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()时,它将自动开始更新。 –

相关问题