5

I've found three ways to use the MKMapCamera and I want to know which one is the most recommended one. My goal is to follow the user and I want to update the camera on each location update (so each second).

1.

            MKMapCamera *newCamera = [MKMapCamera camera];
            [newCamera setCenterCoordinate:newCoordinate];
            [newCamera setPitch:60];
            [newCamera setHeading:heading];
            [newCamera setAltitude:eyeAltitude];
            [mapView setCamera:newCamera];

2.

            MKMapCamera *newCamera = [MKMapCamera cameraLookingAtCenterCoordinate:newCoordinate
                                                             fromEyeCoordinate:oldCoordinate
                                                                   eyeAltitude:eyeAltitude];
            [newCamera setPitch:pitch];

            [mapView setCamera:newCamera];

3.

            MKMapCamera *oldCamera = mapView.camera;
            [oldCamera setCenterCoordinate:newCoordinate];
            [oldCamera setPitch:60];
            [oldCamera setHeading:heading];
            [oldCamera setAltitude:eyeAltitude];

Memory wise seems nr 3 the most decent one or is it a singleton class? In most examples they use nr1.

For nr3 I can't get the animation to work.

Thanks!

Sjoerd Perfors
  • 2,317
  • 22
  • 37
  • I know is a bit late, and it may have just been in Objective-C that the animation was not working, but I have used all of your suggestions and they all animate in Swift 5. See my answer below. I find that setting the different properties on `MKMapCamera` is better than calling `setCamera` every time. – Jav Solo Mar 10 '21 at 21:28

2 Answers2

4

Using the MKMapCamera, you can set the orientation of a map without messing with transforms on the view or even detecting the user’s heading.

MKMapCamera *mapCamera = [[self.mvMap camera] copy];
[mapCamera setHeading:headingDegrees]; 
[self.mvMap setCamera:mapCamera animated:YES];

If you don’t want the animation, you can just set the new heading on the existing camera:

[self.mapView.camera setHeading:heading];
Rameshwar Gupta
  • 791
  • 7
  • 21
0

Updated answer in Swift 5

The code below worked for me without calling setCamera

let mapCamera = self.mapView.camera
mapCamera.heading = headingInDegrees
mapCamera.centerCoordinate = newCoordinate

You can also just set the new heading on the existing camera like so:

self.mapView.camera.heading = headingInDegrees
Jav Solo
  • 576
  • 1
  • 6
  • 15