How to rotate map based on direction of which the User is travelling (not camera facing) - Swift 5 - IOS

Viewed 572

I've done the research on here to try solve my problem, but I cant seem to find a solution. I am wanting my map to rotate, so the top of my screen is the direction which the user is travelling. The code below works but rotates the screen based on the direct the phone is facing. I only want the map to rotate when the user moves in a different direction.

Has anyone solved this problem?

  let manager = CLLocationManager()

   @IBOutlet weak var map: MKMapView!



    func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading){
    map.camera.heading = newHeading.magneticHeading
    map.setCamera(map.camera, animated: true)
    print("updatingHeading is called")
   }


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


    let location = locations[0]
    let span:MKCoordinateSpan = MKCoordinateSpan.init(latitudeDelta: 0.01,longitudeDelta: 0.01)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegion.init(center: myLocation, span: span)
        map.setRegion(region, animated: true)
        self.map.showsUserLocation = true

        let mapCamera = MKMapCamera(lookingAtCenter: myLocation, fromDistance: 5000, pitch: 30, heading: 0)
        map.setCamera(mapCamera, animated: true)
 }




 func viewDidLoad() {
 manager.startUpdatingLocation()
 manager.startUpdatingHeading() 
}

Is there anything built into swift to help me?

cheers

0 Answers
Related