31

如何使用适用于 iOS 的 google maps sdk 拟合坐标数组的边界?我需要为 4 个可见标记缩放地图。

4

5 回答 5

73

这是我对这个问题的解决方案。GMSCoordinateBounds通过多个坐标构建对象。

- (void)focusMapToShowAllMarkers
{       
    CLLocationCoordinate2D myLocation = ((GMSMarker *)_markers.firstObject).position;
    GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] initWithCoordinate:myLocation coordinate:myLocation];

    for (GMSMarker *marker in _markers)
        bounds = [bounds includingCoordinate:marker.position];

    [_mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:15.0f]];
}

更新答案:由于GMSMapView 标记属性已被弃用,您应该将所有标记保存在您自己的数组中。

更新了 swift 3 答案:

    func focusMapToShowAllMarkers() {
        let firstLocation = (markers.first as GMSMarker).position
        var bounds = GMSCoordinateBoundsWithCoordinate(firstLocation, coordinate: firstLocation)

        for marker in markers {
            bounds = bounds.includingCoordinate(marker.position)
        }
        let update = GMSCameraUpdate.fitBounds(bounds, withPadding: CGFloat(15))
        self.mapView.animate(cameraUpdate: update)
  }
于 2013-07-15T12:20:32.407 回答
9

Lirik 回答的Swift 3.0版本:

func focusMapToShowAllMarkers() {
    let myLocation: CLLocationCoordinate2D = self.markers.first!.position
    var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: myLocation, coordinate: myLocation)

    for marker in self.markers {
        bounds = bounds.includingCoordinate(marker.position)
        self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
    }
}

这是我自己的方式:

func focusMapToShowMarkers(markers: [GMSMarker]) {

    guard let currentUserLocation = self.locationManager.location?.coordinate else {
        return
    }

    var bounds: GMSCoordinateBounds = GMSCoordinateBounds(coordinate: currentUserLocation,
                                                          coordinate: currentUserLocation)

    _ = markers.map {
        bounds = bounds.includingCoordinate($0.position)
        self.mapView.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 15.0))
    }
}

你可以像这样调用我上面的函数:

self.focusMapToShowMarkers(markers: [self.myLocationMarker, currentPokemonMarker])

于 2017-02-24T12:48:25.730 回答
5

Lirik 回答的Swift 5版本:

    func focusMapToShowAllMarkers() {

        if arrMarkers.count > 0 {
            let firstLocation = (arrMarkers.first!).position
            var bounds = GMSCoordinateBounds(coordinate: firstLocation, coordinate: firstLocation)

            for marker in arrMarkers {
              bounds = bounds.includingCoordinate(marker.position)
            }

           let update = GMSCameraUpdate.fit(bounds, withPadding: CGFloat(15))
           self.mapView.animate(with: update)
        }
    }
于 2019-10-16T08:59:20.877 回答
3

暂时,谷歌终于实现了 GMSCoordinateBounds,你可以通过 GMSCameraUpdate 来使用它。

详情请查看官方参考

于 2013-05-21T09:12:15.077 回答
1

我们可以使用如下代码简化这一点:

extension Array where Element: GMSMarker {
  func encompassingCoordinateBounds() -> GMSCoordinateBounds {
    reduce(GMSCoordinateBounds(), { $0.includingCoordinate($1.position) })
  }
}

呼叫站点希望:

let markers = [GMSMarker]()
let encompassingCoordinateBounds = markers.encompassingCoordinateBounds()
于 2020-09-02T14:38:41.737 回答