如何使用 Swift 将 Mapbox 缩放到以千米为单位的给定半径?
How to zoom Mapbox using Swift to a given radius in kilometers?
我最近从旧版 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
将完成这项工作:
Changes the receiver’s viewport to fit the given coordinate bounds, optionally animating the change.
这是一个例子:
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)
我最近从旧版 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
将完成这项工作:
Changes the receiver’s viewport to fit the given coordinate bounds, optionally animating the change.
这是一个例子:
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)