2011-04-12 52 views
0

给定一个起点,初始方位和距离支承点,这将计算目标点和沿(最短距离)行最终轴承大圆弧形:的iOS:目标一定距离,并从起始点

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

任何人都知道一个会做诡计的类?

+2

在亚马逊书店里有一些关于C和objective-c的优秀书籍。我建议买一个并学习语言。除此之外,看看math.h – 2011-04-12 16:05:50

回答

4

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

在这个答案的计算比OP中给出的公式更复杂,但他们似乎工作。任何想法,如果有关这个函数背后的逻辑写一个地方? – 2011-07-03 20:21:01

+0

@ D-尼斯我已经保存了一些我曾用来完成这个工作的有用网站,其中包括一个以伪代码的形式提供的网站,并解释了它的工作原理,但不幸的是,似乎我删除了它们。如果我追踪他们,我会发布他们,但赔率不是很好(从备份中检索他们的工作量比我现在可以接受的更多)。 – 2011-07-03 22:14:44

+0

这个计算是Vincenty公式,它比OP中的Haversine公式更准确。 Vincenty公式精确到0.5毫米(这非常疯狂),并且Haversine不准确但更快。但是,看到你如何不手工做,最好还是与文森特合作。[链接] http://en.wikipedia.org/wiki/Vincenty%27s_formulae – 2013-04-22 03:40:49

0

你快把东西很难:)

试试我的代码:

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

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

就是这样。很简单。

相关问题