Change Camera Zoom based on Radius Google Maps iOS SDK

You could use the fitBounds method of GMSCameraUpdate, passing in a GMSCoordinateBounds which is calculated from the edges of your circle.

Based on this answer, it looks like you could use MKCoordinateRegionMakeWithDistance to convert your centre (lat/lon) plus radius (metres) into a MKCoordinateRegion, which will convert the metres into a span in degrees, therefore allowing you to calculate the coordinates in degrees which you'd use to create the GMSCoordinateBounds.


Here's a simpler solution for getting the bounds of a GMSCircle. It doesn't rely on MapKit and avoids the two calls that change the camera position (moveCamera and animateToLocation)

import GoogleMaps

extension GMSCircle {
    func bounds () -> GMSCoordinateBounds {
        func locationMinMax(_ positive : Bool) -> CLLocationCoordinate2D {
            let sign: Double = positive ? 1 : -1
            let dx = sign * self.radius  / 6378000 * (180 / .pi)
            let lat = position.latitude + dx
            let lon = position.longitude + dx / cos(position.latitude * .pi / 180)
            return CLLocationCoordinate2D(latitude: lat, longitude: lon)
        }

        return GMSCoordinateBounds(coordinate: locationMinMax(true),
                               coordinate: locationMinMax(false))
    }
}

After adding this file to your project, all you have to do is:

let update = GMSCameraUpdate.fit(myCircle.bounds())
myMap.animate(with: update)

where myCircle and myMap are replaced by the actual circle and map.


With the help of Saxon Druce,finally I did it.

class MapUtil {

class func translateCoordinate(coordinate: CLLocationCoordinate2D, metersLat: Double,metersLong: Double) -> (CLLocationCoordinate2D) {
    var tempCoord = coordinate

    let tempRegion = MKCoordinateRegionMakeWithDistance(coordinate, metersLat, metersLong)
    let tempSpan = tempRegion.span

    tempCoord.latitude = coordinate.latitude + tempSpan.latitudeDelta
    tempCoord.longitude = coordinate.longitude + tempSpan.longitudeDelta

    return tempCoord
}

class func setRadius(radius: Double,withCity city: CLLocationCoordinate2D,InMapView mapView: GMSMapView) {

    let range = MapUtil.translateCoordinate(city, metersLat: radius * 2, metersLong: radius * 2)

    let bounds = GMSCoordinateBounds(coordinate: city, coordinate: range)

    let update = GMSCameraUpdate.fitBounds(bounds, withPadding: 5.0)    // padding set to 5.0

    mapView.moveCamera(update)

    // location
    let marker = GMSMarker(position: city)
    marker.title = "title"
    marker.snippet = "snippet"
    marker.flat = true
    marker.map = mapView

    // draw circle
    let circle = GMSCircle(position: city, radius: radius)
    circle.map = mapView
    circle.fillColor = UIColor(red:0.09, green:0.6, blue:0.41, alpha:0.5)

    mapView.animateToLocation(city) // animate to center
}

}enter image description here