Site icon Dipin Krishna

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]];
}

Hope it helps. 🙂

Exit mobile version