Zoom in a MKMapView programmatically

后端 未结 9 1759
逝去的感伤
逝去的感伤 2020-12-04 13:27

I\'m using a MKMapView inside an iPhone app. When I click a button the zoom level must increase. This is my first approach:

MKCoordinateRegion z         


        
9条回答
  •  庸人自扰
    2020-12-04 14:00

    mapView.setRegion method has problem when your map is rotated

    You can zoom map via mapView.camera.altitude property, but it is not animated:

    mapView.camera.altitude *= 1.05
    

    You can create new camera object and set it with animation:

    let currentCamera = mapView.camera
    let newCamera: MKMapCamera
    if #available(iOS 9.0, *) {
        newCamera = MKMapCamera(lookingAtCenter: currentCamera.centerCoordinate, fromDistance: currentCamera.altitude * 2, pitch: currentCamera.pitch, heading: currentCamera.heading)
    } else {
        newCamera = MKMapCamera()
        newCamera.centerCoordinate = currentCamera.centerCoordinate
        newCamera.heading = currentCamera.heading
        newCamera.altitude = currentCamera.altitude * 2
        newCamera.pitch = currentCamera.pitch
    }
    
    mapView.setCamera(newCamera, animated: true)
    

提交回复
热议问题