Change the camera position of GMS Map View with animation

Viewed 1274

I have tried to zoom in and zoom out the GMSCamera but failed to perform this with animation. I can have gone through the documentation as well. but nothing helped.

UIView.animate(withDuration: 5.0) {
    let zoom = GMSCameraUpdate.zoom(to: 15.0)
    self.mapView.animate(toZoom: zoom)
}

The above code does not animate the mapview.

2 Answers

Try the below code. You may update it for latest swift syntax.

    mapView.camera = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 1)


    CATransaction.begin()
    CATransaction.setValue(2.0, forKey: kCATransactionAnimationDuration)
    let city = GMSCameraPosition.cameraWithLatitude(58.998400,longitude: 10.035604, zoom: 15)
    self.mapView.animateToCameraPosition(city)
    CATransaction.commit()

You do not need to use UIView.animate on google map as google map has its own method to animate with zoom level.

You just need to call this method.

let lat = 21.78841 //latitude of the location to display on map
let lng = 72.25478 //longitude of the location to display on map
let camera = GMSCameraPosition(target: CLLocationCoordinate2D(latitude: lat, longitude: lng), zoom: 17)
 mapView.animate(to: camera)

From Google's Doc.

/** Animates the camera of this map to |cameraPosition|. */
- (void)animateToCameraPosition:(GMSCameraPosition *)cameraPosition;
Related