使用用户方向旋转Swift 3地图

6
目前我的Swift应用程序中有一个地图视图,其中包含各种注释。当用户步行到地图上的各个注释时,是否有一种方法可以使地图不始终锁定在北方方向?我希望地图能随着他们的步行方向旋转,而不是始终指向北方。
这是我的当前updatelocation函数:
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)
}

我应该在这里声明吗?这与IT技术有关。请注意,保留HTML标记。

https://developer.apple.com/reference/mapkit/mkmapview/1451879-showscompass - Leo Dabus
1个回答

14

这个示例将更新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)
    }
}

这就是你想要的吗?希望能对你有所帮助 :)


2
如何旋转针脚而不是地图? - Cristina
@Cristina,你需要将旋转应用于注释图像(即图钉),而不是地图。 - Duncan Groenewald

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接