基于加速度计旋转MapView

时间:2016-07-18 11:08:14

标签: ios objective-c mapkit core-motion

我正在尝试围绕MapView点使用CoreMotion轮换userLocation。我在旋转视图方面取得了成功,但有一个问题:mapView旋转时,背景白色开始显示。如下图所示(忽略下面的红框) :enter image description here
我用来完成此任务的代码是:

- (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);有关,但我不知道该怎么改变它。

1 个答案:

答案 0 :(得分:1)

尝试以下代码

[Bindable]
    public function createArray():ArrayCollection
    {
      var dataBrick:ArrayCollection = new ArrayCollection();  
      var data:DataAboutBrick;
      for (var x:int = 0; x < 5; x++)
        for (var y:int = 0; y < 7; y++)
        { 
          data = new DataAboutBrick();
          data.x = 0;
          data.y = 0;
          data.color = colorBrick;
          dataBrick.addItem(data);
        }      
      return dataBrick;
    }

它有效