Moving Google Maps Camera to a Location

Viewed 27125

I'm working with a Google Maps View and I want to add a button to the map that when tapped, will move the camera to a specific location. I currently have a button outlet and an action connected to the button.

@IBAction func locationTapped(_ sender: Any) {
    print("tapped")
    let location = GMSCameraPosition.camera(withLatitude: place.latitude, longitude: place.longitude, zoom: 17.0)

    mapView.camera = location
}

place exists but for some reason, the camera will not budge. I've tried different versions of code and looked at the Google Maps documentation but none of the options are producing results. Can anyone tell me what I'm doing wrong?

6 Answers

For Objective-c the method is:

[mapView moveCamera:[GMSCameraUpdate setTarget:<CLLocationCoordinate2DMake>]];

Maybe this is to late but i resolve that problem with adding this:

DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(500), execute: {
        let camera = GMSCameraPosition.camera(withLatitude: lat!, longitude: lon!, zoom: 17.0)
        self.mMap.animate(to: camera)
    })

You have to wait until map load delegate

   func moveMapCamera(at cordinate: CLLocationCoordinate2D, animated: Bool = false) {
        let camera = MKMapCamera()
        camera.centerCoordinate = cordinate
        camera.pitch = 0
        camera.altitude = 9000
        mapV.setCamera(camera, animated: animated)
    }
Related