Ios 如何旋转GMSMarker以显示用户移动方向?

Ios 如何旋转GMSMarker以显示用户移动方向?,ios,iphone,cllocationmanager,gmsmapview,Ios,Iphone,Cllocationmanager,Gmsmapview,在我的应用程序中,我需要在一个GMSMapView中显示用户沿着他移动的方向移动,因此我已经设置了自定义GMSMarker并设置了图像(例如自行车或汽车)并在用户开始移动时设置该标记的动画,并在locationManager didUpdateHeading委托方法中更改标记的角度,因为GMSMarker图像(自行车或汽车)应开始朝向用户移动方向 下面是我正在使用的代码,但是当用户移动缓慢时它会工作,比如说走路,当用户快速移动时它会工作不正常,比如在自行车或汽车中以40+的速度移动 - (voi

在我的应用程序中,我需要在一个GMSMapView中显示用户沿着他移动的方向移动,因此我已经设置了自定义GMSMarker并设置了图像(例如自行车或汽车)并在用户开始移动时设置该标记的动画,并在locationManager didUpdateHeading委托方法中更改标记的角度,因为GMSMarker图像(自行车或汽车)应开始朝向用户移动方向

下面是我正在使用的代码,但是当用户移动缓慢时它会工作,比如说走路,当用户快速移动时它会工作不正常,比如在自行车或汽车中以40+的速度移动

- (void)viewDidLoad {
    [super viewDidLoad];
    if(locationManager == nil) {

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;

    locationManager.distanceFilter = 10.0;
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;


    if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
        [locationManager requestAlwaysAuthorization];

    if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
        [locationManager requestWhenInUseAuthorization];

    [locationManager startUpdatingLocation];

        // Start heading updates.
        if ([CLLocationManager headingAvailable]) {
            locationManager.headingFilter = 5;
            [locationManager startUpdatingHeading];
        }

    }
}

-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    // Use the true heading if it is valid.
    CLLocationDirection direction = newHeading.magneticHeading;
    CGFloat radians = -direction / 180.0 * M_PI;

    //For Rotate Niddle
    CGFloat angle = RADIANS_TO_DEGREES(radians);
    [self rotateArrowView:angle];

}

-(void)rotateArrowView:(CGFloat)degrees {

    currentLocationMarker.rotation = degrees;

}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    // If it's a relatively recent event, turn off updates to save power.

    currentLocation = [locations lastObject];

    [CATransaction begin];
    [CATransaction setAnimationDuration:0.5];
    currentLocationMarker.position = currentLocation.coordinate;
    [CATransaction commit];
}

有谁能告诉我,当用户快速移动时,我现在应该做什么来显示正确的准确航向。

嗯,我不是很确定,但在快速驾驶时,航向似乎不是很准确。我看到几个选择:

  • 如果要显示行驶方向,且用户行驶速度足够快,可以进行显著的移动更改,则可以通过以下方式近似旋转:
  • A=旧用户位置(向量2D)

    B=新用户位置(向量2D)

    DeltaMovement=B-A

    然后,假设北向可以表示为2D向量V(0,1),您可以使用数学函数(我更喜欢这样,但我确信也有很好的obj-c材料)来获得运动向量和北向之间的角度

    缺点是轮换时你会随波逐流。当然,您总是可以使用多个旧位置-这使您可以使其更“不敏感的噪音”

  • 使用陀螺仪平滑磁航向,使用一些卡尔曼滤波器()

  • 您可能有“使用课程”选项。请参考这一点,特别是当快速移动v需要使用课程时