如何使用Swift将Mapbox缩放到公里的给定半径?

我最近从传统的Mapbox SDK(版本1.6)更新到最新的Mapbox iOS SDK 3.x.

在新版本中,我无法弄清楚如何将Mapbox MGLMapView缩放到以公里为单位的给定半径以适应屏幕。

在旧的(版本1.6) RMMapView.zoomWithLatitudeLongitudeBoundsSouthWest方法做如下工作:

 func centerAndZoom(center: CLLocationCoordinate2D, kilometers: Float) { let meters = Double(kilometers * 1000) let region = MKCoordinateRegionMakeWithDistance(center, meters / 2, meters / 2); let northEastLat = center.latitude - (region.span.latitudeDelta / 2); let northEastLon = center.longitude + (region.span.longitudeDelta / 2); let northEast = CLLocationCoordinate2D(latitude: northEastLat, longitude: northEastLon) let southEastLat = center.latitude + (region.span.latitudeDelta / 2); let southEastLon = center.longitude - (region.span.longitudeDelta / 2); let southEast = CLLocationCoordinate2D(latitude: southEastLat, longitude: southEastLon) self.mapView.zoomWithLatitudeLongitudeBoundsSouthWest(southEast, northEast: northEast, animated: true) } 

如何使用Swift在最新的Mapbox中实现半径缩放?

现在-setVisibleCoordinateBounds:animated会做这个工作:

将接收器的视口更改为适合给定的坐标边界,并可select对更改进行animation处理。

Mapbox iOS SDK参考

这里是一个例子:

 let bounds = MGLCoordinateBounds( sw: CLLocationCoordinate2D(latitude: 43.7115, longitude: 10.3725), ne: CLLocationCoordinate2D(latitude: 43.7318, longitude: 10.4222)) mapView.setVisibleCoordinateBounds(bounds, animated: false) 

这里是我如何从给定的坐标缩放到特定的半径:

 let center = CLLocationCoordinate2D(latitude: <#T##CLLocationDegrees#>, longitude: <#T##CLLocationDegrees#>) let kilometers: Double = 2.0 let halfMeters = (kilometers * 1000) / 2 let region = MKCoordinateRegionMakeWithDistance(center, halfMeters, halfMeters) let southWest = CLLocationCoordinate2D( latitude: center.latitude - (region.span.latitudeDelta / 2), longitude: center.longitude - (region.span.longitudeDelta / 2) ) let northEast = CLLocationCoordinate2D( latitude: center.latitude + (region.span.latitudeDelta / 2), longitude: center.longitude + (region.span.longitudeDelta / 2) ) let bounds = MGLCoordinateBounds(sw: southWest, ne: northEast) mapView.setVisibleCoordinateBounds(bounds, edgePadding: .zero, animated: false)