我正在练习谷歌地图。我收到了错误:
NSInternalInconsistencyException',reason:ViewController :: an -observeValueForKeyPath:ofObject:change:context:收到邮件但未处理
override func viewDidLoad() {
super.viewDidLoad()
let camera1:GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 48.857165, longitude: 2.354613, zoom: 8.0)
mapView.camera = camera1
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView .addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.new, context: nil)
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus){
if status == CLAuthorizationStatus.authorizedWhenInUse{
mapView.isMyLocationEnabled = true
}
}
我在这里创建了观察者路径:
func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutableRawPointer) {
if !didFindMyLocation {
let myLocation: CLLocation = (change as AnyObject)[NSKeyValueChangeKey.newKey] as! CLLocation
mapView.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0)
mapView.settings.myLocationButton = true
didFindMyLocation = true
}
}