Compass won't work on MKMapView if I use MKMapCamera

268 views Asked by At

So I am trying to do 3 things.

  1. Have a map which shows my location.
  2. Map should be slightly slanted to give a 3d feel; same like when we scroll up and down with 2 fingers on apple map.
  3. Use compass to rotate the map.

I have enabled userTrackingMode on the map and the map rotates with compass but if I set a MKMapCamera on the map the compass won't work.

Here is my code.

    override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.delegate = self
            locationManager.requestWhenInUseAuthorization()
            mapView.setUserTrackingMode(.FollowWithHeading, animated: true)
            locationManager.startUpdatingLocation()

        }


func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {


            let userCordinate = CLLocationCoordinate2D(latitude: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude)

            let eyeCordinate = CLLocationCoordinate2D(latitude: newLocation.coordinate.latitude - 0.021078, longitude: newLocation.coordinate.longitude - 0.04078 )



            let mapCamera = MKMapCamera(lookingAtCenterCoordinate: userCordinate, fromEyeCoordinate: eyeCordinate, eyeAltitude: 1400)

            mapView.setCamera(mapCamera, animated: true)
            print("Camera set")

        }

What am I doing wrong here?

1

There are 1 answers

0
Wedge Martin On

You have to update the heading of the camera with the compass. Here's something that I'm currently using:

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