使用 Swift 以编程方式在 Mapkit 中倾斜地图

Tilt map in Mapkit programmatically using Swift

我想在启动时倾斜地图。 (与用户用 2 根手指向上或向下滚动时的操作相同)

这可以使用 Swift 吗?

MKMapView Class 参考:http://goo.gl/djHXPn

看看 camera 属性 :

A camera object defines a point above the map’s surface from which to view the map. Applying a camera to a map has the effect of giving the map a 3D-like appearance. You can use a camera to rotate the map so that it is oriented to match the user’s heading or to apply a pitch angle to tilt the plane of the map.

Assigning a new camera to this property updates the map immediately and without animating the change. If you want to animate changes in camera position, use the setCamera:animated: method instead.

You must not set this property to nil. To restore the map to a flat appearance, apply a camera with a pitch angle of 0, which yields a camera looking straight down onto the map surface.

尝试创建和设置相机(动画或非动画)。

编辑:

我试过自己。这是一个如何使用它的例子:

let userCoordinate = CLLocationCoordinate2D(latitude: 58.592725, longitude: 16.185962)
let eyeCoordinate = CLLocationCoordinate2D(latitude: 58.571647, longitude: 16.234660)
let mapCamera = MKMapCamera(lookingAtCenterCoordinate: userCoordinate, fromEyeCoordinate: eyeCoordinate, eyeAltitude: 400.0)

let annotation = MKPointAnnotation()
annotation.setCoordinate(userCoordinate)

mapView.addAnnotation(annotation)

mapView.setCamera(mapCamera, animated: true)

您必须根据您的位置和您想要的倾斜效果找到合适的 eyeCoordinate

Swift 4

这是一种更简单的方法:您可以设置距离、俯仰和航向:

let mapCamera = MKMapCamera(lookingAtCenter: userCoordinate, fromDistance: 10000, pitch: 65, heading: 0)
map.setCamera(mapCamera, animated: true)