IOS Google Maps skid fit bounds

417 views Asked by At

I have the following code inside a UIViewController's viewDidLoad method. It is within a callback function that is called after fetching a list of places from a remote sever.

            var bounds = GMSCoordinateBounds()

            for place in self.placeCollection.places {

                var position = CLLocationCoordinate2DMake(place.latitude, place.longitude)
                bounds.includingCoordinate(position)
                var marker = GMSMarker(position: position)
                marker.title = place.title;
                marker.map = self.mapView;
            }

            if self.placeCollection.places.count > 0 {
                self.mapView.moveCamera(GMSCameraUpdate.fitBounds(bounds))
            }

The map does not budge and is nowhere near any of the markers, I do not see any errors in the debug panel either. According to the docs this is the correct way to focus the map on a set of markers.

1

There are 1 answers

1
Saxon Druce On BEST ANSWER

bounds.includingCoordinate does not update bounds, instead it returns a new GMSCoordinateBounds containing the new point.

So, you need something like this:

bounds = bounds.includingCoordinate(position)