Tilt map in Mapkit programmatically using Swift

一个人想着一个人 提交于 2020-01-11 10:12:32

问题


I'd like to tilt the map at startup. (Same as the user does when scrolling up or down with 2 fingers)

Is this possible using Swift?


回答1:


MKMapView Class Reference : http://goo.gl/djHXPn

Look at the camera property :

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.

Try to create and set a camera (animated or not).

Edit :

I tried myself. Here is an example of how to use it :

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)

You'll have to find your right eyeCoordinate depending on your location and tilt effect you want to have.




回答2:


Swift 4

This is an easier way: you can set distance, pitch and heading:

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


来源:https://stackoverflow.com/questions/28886169/tilt-map-in-mapkit-programmatically-using-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!