Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

iOS, How to use GMSCoordinateBounds in order to show all the markers of the map? (Google Maps SDK)

I want to show all the markers that are on my map, after doing some searches I found that it should be done with GMSCoordinateBounds (Google Maps SDK) I've read the official documentation about it, but I have not understand how to use it and implement it in my code.

https://developers.google.com/maps/documentation/ios/reference/interface_g_m_s_camera_update#aa5b05606808272f9054c54af6830df3e

1 Answer

Hi Petra,

You are correct that you need a GMSCoordinateBounds that is large enough to contain all of your markers. There are a couple ways you can go about this.

The easiest way I have found is to create a GMSMutablePath and then add all the coordinates of your markers to it. Then you can use the GMSCoordinateBounds initializer initWithPath: to create the bounds.

For example, if you have an array of GMSMarker's:

NSArray *myMarkers;

GMSMutablePath *path = [GMSMutablePath path];

for (GMSMarker *marker : myMarkers) {
    [path addCoordinate: marker.position];
}

GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithPath:path];

Once you have the bounds, you can create the GMSCameraUpdate and use it to animate the map to the visible bounds containing all of your markers. =)