Rotate MapView using Compass orientation

后端 未结 5 1652
故里飘歌
故里飘歌 2020-12-16 04:57

Is it possible to have an embedded MKMapView rotate to always face the direction the iPhone is facing? Basically I want to mimic the Map app rotation feature o

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 05:53

    Simple solution in swift 3.0. Make sure to put the line in mapViewDidFinishLoadingMap or it will ignore it

    public func mapViewDidFinishLoadingMap(_ mapView: MKMapView)
    {
        mapView.setUserTrackingMode(.followWithHeading, animated: false)
    }
    

    If you don't want the map to center on the user location you might do something like this

    public func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
    {
        if(CLLocationCoordinate2DIsValid(mapView.centerCoordinate))
        {
            mapView.camera.heading = newHeading.trueHeading
        }
    }
    

提交回复
热议问题