使用位置管理器进行键值观察时出错

时间:2017-04-19 06:29:07

标签: ios swift key-value-observing

我正在练习谷歌地图。我收到了错误:

  

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
    }
}

0 个答案:

没有答案