Search code examples
iosswiftgoogle-mapsgmsmapview

Determine proper zoom level on GMSMapView to fit all the locations needed?


In my iOS app I'm using GoogleMaps and APIs for maps and I'm not very familiar with it.

I know that with iOS native MKMapView you can set a visible map rect to fit all the points needed at a proper zoom level, like described here: Zooming MKMapView to fit annotation pins?

but I'm not sure how to do such thing for GMSMapView. Does anyone know how to do it?

Any kind of help is highly appreciated!


Solution

  • Add this after setting up markers on the Map:

    var bounds = GMSCoordinateBounds()
    for coordinate in testCoordinates {
        bounds = bounds.includingCoordinate(coordinate)
    }
    let update = GMSCameraUpdate.fit(bounds, withPadding: 50)
    mapView.animate(with: update)