Search code examples
iosswiftswift3google-maps-sdk-ios

Make the current location appear on the map once the user’s position has been spotted swift 3


Hi I'm trying to fix this function that was totally fine up until swift 3, does anyone know what the change is?

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if !didFindMyLocation {
        let myLocation: CLLocation = change?[NSKeyValueChangeKey] as! CLLocation //Getting an error here Cannot convert value of type '(NSKeyValueChangeKey).Type' (aka 'NSKeyValueChangeKey.Type') to expected argument type 'DictionaryIndex<_, _>'
        viewMap.camera = GMSCameraPosition.camera(withTarget: myLocation.coordinate, zoom: 10.0)
        viewMap.settings.myLocationButton = true

        didFindMyLocation = false
    }

}

Solution

  • You should provide proper key like below :

    let myLocation: CLLocation = change?[NSKeyValueChangeKey.newKey] as! CLLocation