Question

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];
Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top