views:

2168

answers:

4

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 on my own app.

I see that the iPhone SDK does not expose the functionality. However, I wonder if it would work to rotate the entire view using CGAffineTransformMakeRotate. Would it affect tapping and zooming? Is there a better way?

+1  A: 

If you look in the 3.0 iPhone Application Programming Guide under Device Support you'll find information on the magnetometer (aka compass). Once you start getting heading information (through the didUpdateHeading method) you should be able to get the compass data then use that to calculate the proper rotation transform value to apply to the map view.

Not sure if it handles zooming. In the standard map application I've noticed that compass heading tracking stops as soon as you start pinch-zooming.

Also, keep in mind that location directions are in degrees whereas transform rotation angles are in radians.

Ramin
A: 

anyone have any sample code for doing this?

icampus
You should not post follow-up questions as an answer, better start a newquestion instead...
sth
+4  A: 

I can confirm that it works fine. Here's the code that I'm using:

[mapView setTransform:CGAffineTransformMakeRotation(-1 * currentHeading.magneticHeading * 3.14159 / 180)];

mapView is my MKMapView instance

Doug Hays
A: 

[mapView setTransform:CGAffineTransformMakeRotation(-1 * currentHeading.magneticHeading * 3.14159 / 180)];

The above code works to rotate the mapview, what about the annotations? How would the transform be applied to those as well?

Darius
@Darius, this should have been a comment not an answer.
notnoop