gpt4 book ai didi

javascript - iOS:给定距离和距起点方位的目的地点

转载 作者:行者123 更新时间:2023-11-28 19:24:15 27 4
gpt4 key购买 nike

给定起点、初始方位和距离,这将计算沿(最短距离)大圆弧行进的目的地点和最终方位:

var lat2 = 

Math.asin( Math.sin(lat1)*Math.cos(d/R) +
Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );

var lon2 =

lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1),
Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));

这段代码是用 JavaScript 编写的。我希望 iOS 也有同样的东西,所以在 objective-c 中。

有人知道可以解决这个问题的类吗?

最佳答案

我不是在翻译你的 Javascript,这只是我在某处为我的一个项目找到的一个例程,它做同样的事情:

- (CLLocationCoordinate2D) NewLocationFrom:(CLLocationCoordinate2D)startingPoint 
atDistanceInMiles:(float)distanceInMiles
alongBearingInDegrees:(double)bearingInDegrees {

double lat1 = DEG2RAD(startingPoint.latitude);
double lon1 = DEG2RAD(startingPoint.longitude);

double a = 6378137, b = 6356752.3142, f = 1/298.257223563; // WGS-84 ellipsiod
double s = distanceInMiles * 1.61 * 1000; // Convert to meters
double alpha1 = DEG2RAD(bearingInDegrees);
double sinAlpha1 = sin(alpha1);
double cosAlpha1 = cos(alpha1);

double tanU1 = (1 - f) * tan(lat1);
double cosU1 = 1 / sqrt((1 + tanU1 * tanU1));
double sinU1 = tanU1 * cosU1;
double sigma1 = atan2(tanU1, cosAlpha1);
double sinAlpha = cosU1 * sinAlpha1;
double cosSqAlpha = 1 - sinAlpha * sinAlpha;
double uSq = cosSqAlpha * (a * a - b * b) / (b * b);
double A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
double B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));

double sigma = s / (b * A);
double sigmaP = 2 * kPi;

double cos2SigmaM;
double sinSigma;
double cosSigma;

while (abs(sigma - sigmaP) > 1e-12) {
cos2SigmaM = cos(2 * sigma1 + sigma);
sinSigma = sin(sigma);
cosSigma = cos(sigma);
double deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
sigmaP = sigma;
sigma = s / (b * A) + deltaSigma;
}

double tmp = sinU1 * sinSigma - cosU1 * cosSigma * cosAlpha1;
double lat2 = atan2(sinU1 * cosSigma + cosU1 * sinSigma * cosAlpha1, (1 - f) * sqrt(sinAlpha * sinAlpha + tmp * tmp));
double lambda = atan2(sinSigma * sinAlpha1, cosU1 * cosSigma - sinU1 * sinSigma * cosAlpha1);
double C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
double L = lambda - (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));

double lon2 = lon1 + L;

// Create a new CLLocationCoordinate2D for this point
CLLocationCoordinate2D edgePoint = CLLocationCoordinate2DMake(RAD2DEG(lat2), RAD2DEG(lon2));

return edgePoint;
}

关于javascript - iOS:给定距离和距起点方位的目的地点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5638095/

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