Rotate MapView Using Compass Orientation
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?
Answer
To rotate the mapView but not the annotations you could use the following code to compensate for the maps rotation.
- (void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
double rotation = newHeading.magneticHeading * 3.14159 / 180;
CGPoint anchorPoint = CGPointMake(0, -23); // The anchor point for your pin
[mapView setTransform:CGAffineTransformMakeRotation(-rotation)];
[[mapView annotations] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
MKAnnotationView * view = [mapView viewForAnnotation:obj];
[view setTransform:CGAffineTransformMakeRotation(rotation)];
[view setCenterOffset:CGPointApplyAffineTransform(anchorPoint, CGAffineTransformMakeRotation(rotation))];
}];
}
Another sollution is using a new method that has been added in iOS 5 to MKMapView.
Take a look at: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
- (void)setUserTrackingMode:(MKUserTrackingMode)mode animated:(BOOL)animated;
Related Questions
- → Web App Image Rotate Issue
- → Website not loading on AT&T LTE; all other carriers and wifi work fine
- → Disable wheel selector on iOS Safari?
- → Opening Image File from JavaScript (doesn't work on mobile phones)
- → React router & Phonegap - First page not found
- → unable to add photo from iphone to canvas
- → What's the best practice for having a Laravel application serve both the browser-based application, and act as an API for the iPhone and Android app?
- → Wordpress mobile nav menu: menu appears on click on iPhone and other mobile devices, but not iPad
- → How to save the response From My server , and How can i access That data
- → Communication between iPhone app and AWS server
- → NSDateFormatterStyle.MediumStyle doesn't show in iPhone
- → Could not find Developer Disk Image - Xcode 7.2 & iOS 9.2
- → String encryption in Objective-C decrypt in C#