我不能让这种算法权利。我试图做一个指南针指向在某个位置,而不是仅仅指向,让我们说,北部。什么是错的。我花了很多时间试图弄清楚这一点,但我无法找到它。任何想法?
- (无效)locationManager:(CLLocationManager *)经理didUpdateHeading:(CLHeading *)newHeading {
双distanceEast =(location.longitude大于0&安培;&安培; otherLocation.longitude℃的)? 180 - location.longitude + otherLocation.longitude - -180:otherLocation.longitude - location.longitude;
如果(distanceEast℃,){
distanceEast + = 360;
}
双distanceWest =(location.longitude&℃,安培;&安培; otherLocation.longitude大于0)? -180 - location.longitude - 180 - otherLocation.longitude:location.longitude - otherLocation.longitude;
如果(distanceWest℃,){
distanceWest + = 360;
}
浮动latitudinalDifference =(otherLocation.latitude - location.latitude);
浮longitudinalDifference = FMIN(distanceEast,distanceWest);
浮ARCTAN = ATAN(longitudinalDifference / latitudinalDifference);
浮动oldRadian =(-manager.heading.trueHeading * M_PI /180.0f)+arcTan+M_PI;
浮动newRadian =(-newHeading.trueHeading * M_PI /180.0f)+arcTan+M_PI;
CABasicAnimation *动画;
动画= [CABasicAnimation animationWithKeyPath:@transform.rotation];
animation.fromValue = [NSNumber的numberWithFloat:oldRadian]。
animation.toValue = [NSNumber的numberWithFloat:newRadian]。
animation.duration = 0.5F;
directionsArrow.layer.anchorPoint = CGPointMake(0.5,0.5);
[directionsArrow.layer addAnimation:动画forKey:@rotationCompass];
directionsArrow.transform = CGAffineTransformMakeRotation(newRadian);
}
解决方案
双LON = location.longitude - otherLocation.longitude;
双Y = SIN(LON)* COS(otherLocation.latitude);
双X = COS(location.latitude)*罪(otherLocation.latitude) - 罪(location.latitude)* COS(otherLocation.latitude)* COS(LON);
双角= ATAN2(Y,X);
角度
是两个位置之间的方位。
I can't get this algorithm right. I'm trying to make a compass that points at a certain location instead of just pointing to, let's say, north. Something is wrong. I've spent a lot of time trying to figure this out, but i just can't find it. Any ideas?
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
double distanceEast = (location.longitude > 0 && otherLocation.longitude < 0) ? 180 - location.longitude + otherLocation.longitude - -180: otherLocation.longitude - location.longitude;
if (distanceEast < 0) {
distanceEast += 360;
}
double distanceWest = (location.longitude < 0 && otherLocation.longitude > 0) ? -180 - location.longitude - 180 - otherLocation.longitude : location.longitude - otherLocation.longitude;
if (distanceWest < 0) {
distanceWest += 360;
}
float latitudinalDifference = (otherLocation.latitude - location.latitude);
float longitudinalDifference = fmin(distanceEast,distanceWest);
float arcTan = atan(longitudinalDifference / latitudinalDifference);
float oldRadian = (-manager.heading.trueHeading *M_PI /180.0f)+arcTan+M_PI;
float newRadian = (-newHeading.trueHeading *M_PI /180.0f)+arcTan+M_PI;
CABasicAnimation *animation;
animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
animation.fromValue = [NSNumber numberWithFloat:oldRadian];
animation.toValue = [NSNumber numberWithFloat:newRadian];
animation.duration = 0.5f;
directionsArrow.layer.anchorPoint = CGPointMake(0.5, 0.5);
[directionsArrow.layer addAnimation:animation forKey:@"rotationCompass"];
directionsArrow.transform = CGAffineTransformMakeRotation(newRadian);
}
解决方案
double lon = location.longitude - otherLocation.longitude;
double y = sin(lon) * cos(otherLocation.latitude);
double x = cos(location.latitude) * sin(otherLocation.latitude) - sin(location.latitude) * cos(otherLocation.latitude) * cos(lon);
double angle = atan2(y, x);
angle
is the bearing between the two locations.
上一篇:发现两个向量之间的最佳/缩放/移位向量、缩放、两个、发现
下一篇:循环滚动算法算法