基于加速度计旋转 MapView

Rotate MapView Based on accelerometer

我正在尝试使用 CoreMotion 围绕 userLocation 点旋转我的 MapView。我成功地旋转了视图,但是有一个问题:mapView 旋转时背景开始显示为白色。 如图所示(忽略下面的红色框):
我用来完成这个的代码是:

- (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);
                                            }
                                        }];

}

和标题

- (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];
}

此方法调用以下方法:

- (void)outputAccelertionData:(CMAcceleration)acceleration{
    //UIInterfaceOrientation orientationNew;

    // Get the current device angle
    float xx = -acceleration.x;
    float yy = acceleration.y;
    float angle = atan2(yy, xx);
}

最后:

- (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];
}

那么,我在这里缺少什么?据我所知,它与 self.mapView.transform = CGAffineTransformMakeRotation(newRad); 有关,但我不知道将其更改为什么。

尝试以下代码

[self.mapView setUserTrackingMode:MKUserTrackingModeFollowWithHeading animated:true];

有效