swiftgoogle-mapslocalizationmarkeruserlocation

google ios SDK swift fit bounds to user location and selected marker place


I would like to zoom for selected place and user location using google map.

I have method for fit bounds:

func fitToMarker(marker: GMSMarker) {
        let bounds = GMSCoordinateBounds()
        bounds.includingCoordinate(self.userMarker.position)
        bounds.includingCoordinate(marker.position)
        self.mapView.animate(with: GMSCameraUpdate.fit(bounds))
    }

and I use it after selected button in method:

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker)

Solution

  • - (GMSCoordinateBounds *) includingCoordinate: (CLLocationCoordinate2D) coordinate

    Returns a GMSCoordinateBounds representing the current bounds extended to include the passed-in coordinate.

    If the current bounds is invalid, the result is a valid bounds containing only coordinate.

    includingCoordinate method doesn't mutate existing GMSCoordinateBounds, but returns a new GMSCoordinateBounds. Try this

    func fitToMarker(marker: GMSMarker) {
        var bounds = GMSCoordinateBounds()
        bounds = bounds.includingCoordinate(self.userMarker.position)
        bounds = bounds.includingCoordinate(marker.position)
        self.mapView.animate(with: GMSCameraUpdate.fit(bounds))
    }