gpt4 book ai didi

ios - 计算罗盘航向到特定坐标而不是北方

转载 作者:可可西里 更新时间:2023-11-01 05:56:45 24 4
gpt4 key购买 nike

我无法正确使用该算法。我正在尝试制作一个指向某个位置的指南针,而不是仅仅指向,比方说,北方。出了点问题。我花了很多时间试图弄清楚这一点,但我就是找不到。有什么想法吗?

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

角度 是两个位置之间的方位角。

关于ios - 计算罗盘航向到特定坐标而不是北方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13600648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com