2016-08-14 39 views
2

我最近从传统的Mapbox SDK(版本1.6)更新到最新的Mapbox iOS SDK 3.x.如何使用Swift将公式中的Mapbox缩放到给定的半径?

在新版本中,我无法弄清楚如何将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半径放大?

回答

1

现在-setVisibleCoordinateBounds:animated将做的工作:

更改接收器的视口设置符合给定的坐标范围,可选动画的变化。

Mapbox iOS SDK Reference

下面是一个例子:

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

感谢一大堆!保存我的一天 – AamirR

+0

这可能是真正有用的,如果你真的可以在那里放置一些真实的代码,就像描述的情况一样。 (我的意思是转换半径+中心 - > MGLCoordinateBounds) – Aleksandr

+0

人们应该如何知道sw和ne?我有一个重复使用的屏幕显示2个不同的注释。但它总是不同的位置度。我是否需要计算2个给定注释的sw/ne坐标? –

2

这是我如何做放大到特定半径从给出的坐标:

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)