如何适应协调与谷歌地图SDK阵列的iOS界限? 我需要放大地图4个可见的标记。
Answer 1:
下面是我对这个问题的解决方案。 构建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
标记属性已过时,应保存所有标记在自己的阵列。
更新迅速的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)
}
Answer 2:
雨燕3.0的版本Lirik的回答:
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])
Answer 3:
对于目前,谷歌终于实现了GMSCoordinateBounds,你可以利用它与GMSCameraUpdate。
有关详细信息,请查看官方的参考 。
文章来源: How to fit bounds for coordinate array with google maps sdk for iOS?