Search code examples
iosswiftgoogle-mapslocationgmsmapview

Moving Google Maps Camera to a Location


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?


Solution

  • The GMSMapView class has the following function:

    animate(to: GMSCameraPosition)
    

    So in your code sample, instead of doing this:

    mapView.camera = location
    

    Try doing this:

    mapView.animate(to: location)
    

    Hope this helps!