2017-08-28 97 views
-2

我的問題是關於地圖服務我現在正在使用具有自定義tile服務的地圖我需要添加兩個按鈕或步進器來放大和縮小,我使用MapKit庫。有人可以幫助嗎?MapKit縮放按鈕

+1

我有答案,但在目標C –

回答

1

退房答案

switch sender.tag { 
    case 10: 
    //Zoom In 

     var region: MKCoordinateRegion = map_view.region 
     region.span.latitudeDelta /= 2.0 
     region.span.longitudeDelta /= 2.0 
     map_view.setRegion(region, animated: true) 
    case 20: 

    //Zoom Out 
     var region: MKCoordinateRegion = map_view.region 
     region.span.latitudeDelta = min(region.span.latitudeDelta * 2.0, 180.0) 
     region.span.longitudeDelta = min(region.span.longitudeDelta * 2.0, 180.0) 
     map_view.setRegion(region, animated: true) 
} 
+0

Thnx花花公子對我的問題簡單而容易 – Saneth

1

這裏是

let region = MKCoordinateRegionMake(self.mapView.region.center, MKCoordinateSpanMake(mapView.region.span.latitudeDelta*0.7, mapView.region.span.longitudeDelta*0.7)) 
mapView.setRegion(region, animated: true) 

在迅速3

變焦答案變焦出

let zoom = getZoom() // to get the value of zoom of your map. 
    if zoom > 3.5{ // **here i have used the condition that avoid the mapview to zoom less then 3.5 to avoid crash.** 

      let region = MKCoordinateRegionMake(self.mapView.region.center, MKCoordinateSpanMake(mapView.region.span.latitudeDelta/0.7, mapView.region.span.longitudeDelta/0.7)) 
      mapView.setRegion(region, animated: true) 
     } 

在這裏你可以得到多少的地圖視野放大。

func getZoom() -> Double { 

    var angleCamera = self.mapView.camera.heading 
    if angleCamera > 270 { 
     angleCamera = 360 - angleCamera 
    } else if angleCamera > 90 { 
     angleCamera = fabs(angleCamera - 180) 
    } 
    let angleRad = Double.pi * angleCamera/180 
    let width = Double(self.view.frame.size.width) 
    let height = Double(self.view.frame.size.height) 
    let heightOffset : Double = 20 
    let spanStraight = width * self.mapView.region.span.longitudeDelta/(width * cos(angleRad) + (height - heightOffset) * sin(angleRad)) 
    return log2(360 * ((width/256)/spanStraight)) + 1; 
} 

它適用於我。

+0

日Thnx花花公子它也能工作 – Saneth

+0

歡迎兄弟如果有的話,然後問我。 –