MapKit缩放按钮

时间:2017-08-28 10:07:45

标签: ios swift mapkit

我的问题是关于地图服务我现在使用带有自定义磁贴服务的地图我需要添加两个按钮或步进器来放大和缩小,我使用MapKit库。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

以下是swift 3

中的答案

用于放大

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)

缩小

   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;
}

它对我有用。

答案 1 :(得分:2)

查看答案

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)
}