Skip to content

iOS Google Maps: Set Camera to show all Markers

The intend is to set the boundaries of the map to focus on all the markers.

ios Google Maps Scrren Shot 1

The ‘GMSMapView‘ class used to have a method ‘markers‘ which returned all the markers, but its now deprecated.
So, you will have to keep track of the markers added to the map.

Assuming that you have the following:

GMSMapView *mapView;
NSMutableArray *markers;

and all the markers are add to the NSMutableArray “markers”.

- (void)ShowAllMarkers
{
    CLLocationCoordinate2D firstLocation = ((GMSMarker *)markers.firstObject).position;
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:firstLocation coordinate:firstLocation];
 
    for (GMSMarker *marker in markers) {
        bounds = [bounds includingCoordinate:marker.position];
    }
 
    [mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:50.0f]];
}

iOS Google Maps Screen Shot

Hope it helps. 🙂

Dipin Krishna

Written by Dipin Krishna

Senior full-stack engineer with 15 years across Django, Laravel, SwiftUI and the infrastructure underneath. Available for contract work.

Work with me →

Leave a note

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.