iOS:给出距起点的距离和方位的目的地点

发布于 2024-11-01 07:50:51 字数 420 浏览 0 评论 0原文

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

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 中。

有人知道有一个类可以做到这一点吗?

Given a start point, initial bearing, and distance, this will calculate the destination point and final bearing travelling along a (shortest distance) great circle arc:

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

This code is in JavaScript.
I'dd like the same thing for iOS, so in objective-c.

Anyone knows about a class that would do the trick?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2024-11-08 07:50:51

我不会翻译你的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;
}

I'm not translating your Javascript, this is just a routine I found somewhere for a project of mine that does the same thing:

- (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;
}
森林很绿却致人迷途 2024-11-08 07:50:51

你让事情变得非常困难:)

试试我的代码:

LatDistance = Cos(BearingInDegrees)*distance;
LongDistance = Sin(BearingInDegrees)*distance;

CLLocationDegrees *destinationLat = CurrentLatitude + (LatDistance*0.00001);
CLLocationDegrees *destinationLong = CurrentLongitude + (LongDistance*0.00001);

就是这样。很简单。

You are making things very difficult :)

Just try my code:

LatDistance = Cos(BearingInDegrees)*distance;
LongDistance = Sin(BearingInDegrees)*distance;

CLLocationDegrees *destinationLat = CurrentLatitude + (LatDistance*0.00001);
CLLocationDegrees *destinationLong = CurrentLongitude + (LongDistance*0.00001);

That's it. Very simple.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文