[英]How to rotate GMSMarker to show user moving direction?
在我的应用程序中,我需要向用户显示用户在GMSMapView中的移动方向,因此我放置了自定义GMSMarker并设置了图像(例如,自行车或汽车),并在用户开始移动并更改其角度时设置了该标记的动画位置管理器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 = oldUserLocation(矢量2D)
B = newUserLocation(矢量2D)
DeltaMovement = B-A
然后,假设可以将北向表示为2D向量V(0,1),则可以使用数学函数(对此我更喜欢https://github.com/nicklockwood/VectorMath ,但我确定有很好的obj -c的东西)以获取运动矢量和北方之间的角度。
缺点是您在转弯时会漂移。 当然,您始终可以使用多个旧位置-这使您对“噪声不敏感”更为明显。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.