Ios 基于加速度计的旋转地图视图

Ios 基于加速度计的旋转地图视图,ios,objective-c,mapkit,core-motion,Ios,Objective C,Mapkit,Core Motion,我正在尝试使用CoreMotion围绕userLocation点旋转我的MapView。我成功地旋转了视图,但有一个问题:当地图视图旋转时,背景白色开始显示。如图所示(忽略下面的红色框):我用来完成此操作的代码是: - (void)viewDidLoad { locationManager = [[CLLocationManager alloc] init]; _mapView.delegate = self; locationManager.delegate = self; [

我正在尝试使用
CoreMotion
围绕
userLocation
点旋转我的
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);
}
anf最后:

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

如果你想跟随标题,可以使用self.mapView.setUserTrackingMode(MKUserTrackingMode.FollowWithHeading,动画:true)。@sanman是的,这正是我需要的。。。这行代码运行得很好,但是如果你想快速移动,它会导致应用程序崩溃,并说
EXE\u BAD\u ACCESS
有什么办法吗?我这样做了
[UIView animateWithDuration:0.6延迟:0选项:UIView AnimationOptionCurveeAseInOut动画:^{//self.mapView.transform=CGAffineTransformMakeRotation(newRad);[self.mapView设置用户跟踪模式:MKUserTrackingModeFollowWithHeading animated:true];}完成:nil]
@sanman I评论
[self.mapView setCenterCoordinate:userLocation.location.coordinate:YES]
didUpdateUserLocation
中,它不再崩溃了。:)非常感谢您的帮助。您可以在viewDidLoad中添加该行,而不是该块。我不知道它为什么会崩溃。很高兴它为您解决了:)它一直在崩溃应用程序,在描述中没有显示任何错误,但有时它会显示
\u NSCFNumber isPitched
,有时
EXE\u BAD\u ACCESS
我不知道失败的原因崩溃,但希望这会有所帮助:)