目前我的 swift 应用程序有一个带有各种注释的 map View ,当用户从 map 上的各种注释中行走时,有没有办法不总是锁定在北方方向?我希望 map 转向他们行走的方向,而不是总是指向北方。
这是我当前的更新位置函数
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, noteTime.span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
map.tintColor = UIColor(red:0.19, green:0.69, blue:0.73, alpha:1.0)
}
我应该在这里声明吗?
最佳答案
此示例会将 mapView
的旋转更新为指向与用户相同的方向。
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.startUpdatingHeading()
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
mapView.camera.heading = newHeading.magneticHeading
mapView.setCamera(mapView.camera, animated: true)
}
}
这就是你想要的吗?希望对您有所帮助:)
关于Swift 3 随用户方向旋转 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43672353/