google ios SDK swift 使边界适合用户位置和选定的标记位置

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

我想使用 google 地图缩放所选地点和用户位置。

我有适合边界的方法:

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

我在方法中选择按钮后使用它:

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

- (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 方法不会改变现有的 GMSCoordinateBounds,而是 returns 一个新的 GMSCoordinateBounds。试试这个

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))
}