2016-08-14 31 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)