Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to use the fitBounds method to fit all my markers in the google maps camera view. So I have my markers stored in markersArray and I use the following code to init GMSCoordinateBounds with the 1st and 2nd markers in markersArray which works fine.

Then when I try to add the 3rd marker from markersArray using includingCoordinate I don't see the bounds updating anything neither in its values nor in the map is it changing the camera accordingly.

The weird thing is that in Google maps SDK for iOS docs it's saying that GMSCoordinateBounds "is immutable and can't be modified after construction." Does that make sense? I can't change the bounds after constructing them? Then how do I add more coordinates to the bounds?

Here is my code:

    GMSCoordinateBounds *bounds= [[GMSCoordinateBounds alloc] init];

    GMSMarker *marker1 = [markersArray objectAtIndex:0];
    GMSMarker *marker2 = [markersArray objectAtIndex:1];
    GMSMarker *marker3 = [markersArray objectAtIndex:2];

    bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:marker1.position    coordinate:marker2.position];

    //Add the 3rd marker to the bounds
    [bounds includingCoordinate:marker3.position];

    GMSCameraUpdate *update = [GMSCameraUpdate fitBounds:bounds withPadding:600.0f];
    [mapView_ animateWithCameraUpdate:update];
share|improve this question

1 Answer

up vote 2 down vote accepted

The GMSCoordinateBounds includingCoordinate: method returns a new bounds containing the combination of the original bounds and the new location, it doesn't modify the object you call it on.

So you would need something like this:

bounds = [bounds includingCoordinate: marker3.position];
share|improve this answer
 
Thanks it works fine now. It is so obvious I feel embarrassed for asking. –  Ali Jun 13 at 9:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.