I am trying to rotate my MapView
using CoreMotion
around userLocation
point. I'm successful in rotating the view but there is one problem: When the mapView
is rotated the background white started showing. As shown in this picture (Ignore the red box below):
The code I'm using to accomplish this is:
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
_mapView.delegate = self;
locationManager.delegate = self;
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
_mapView.showsUserLocation = YES;
[_mapView setMapType:MKMapTypeStandard];
[_mapView setZoomEnabled:YES];
[_mapView setScrollEnabled:YES];
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.headingFilter = 1;
[locationManager startUpdatingHeading];
motionManager = [[CMMotionManager alloc] init];
motionManager.accelerometerUpdateInterval = 0.01;
motionManager.gyroUpdateInterval = 0.01;
[motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue]
withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
if (!error) {
[self outputAccelertionData:accelerometerData.acceleration];
}
else{
NSLog(@"%@", error);
}
}];
}
and for the heading
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
//self.lblGrados.text = [NSString stringWithFormat:@"%.0f°", newHeading.magneticHeading];
// Convert Degree to Radian and move the needle
float newRad = -newHeading.trueHeading * M_PI / 180.0f;
[UIView animateWithDuration:0.6 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.mapView.transform = CGAffineTransformMakeRotation(newRad);
} completion:nil];
}
This method calls the one below:
- (void)outputAccelertionData:(CMAcceleration)acceleration{
//UIInterfaceOrientation orientationNew;
// Get the current device angle
float xx = -acceleration.x;
float yy = acceleration.y;
float angle = atan2(yy, xx);
}
anf finally:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800.0f, 200.0f);
//[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
[self.mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
[self.mapView setRegion:region animated:YES];
}
- (NSString *)deviceLocation {
return [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
}
- (NSString *)deviceLat {
return [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
}
- (NSString *)deviceLon {
return [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];
}
- (NSString *)deviceAlt {
return [NSString stringWithFormat:@"%f", locationManager.location.altitude];
}
So, what am I missing here? As far as I gone it has something to do with self.mapView.transform = CGAffineTransformMakeRotation(newRad);
but I don't know what to change it to.
Try following code
[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:true];
It works