如何在gmap for iPhone中用经纬度计算两地之间的距离?
发布于 2010-06-24 18:45:52
您可以在CLLocation对象上使用以下方法:
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location例如:
CLLocation* location_a;
CLLocation* location_b;
CLLocationDistance distance = [location_a distanceFromLocation:location_b];发布于 2010-06-24 18:41:04
查看haversine formula
发布于 2010-06-24 18:41:21
您可以使用此方法。它取自http://wiki.forum.nokia.com/index.php/How_to_calculate_distance_between_two_landmarks,但我认为数学对iPhone也是一样的。:)
const TInt KEarthRadius = 6372795; // in meters
TReal64 YourClassName :: CalcDistance( TReal64 aLat1, TReal64 aLong1,
TReal64 aLat2, TReal64 aLong2 )
{
TReal64 result = -1,
lat1Sin, lat2Sin, lat1Cos, lat2Cos, deltaLongCos, deltaLongSin;
if( Math :: Sin( lat1Sin, aLat1 * KPi / 180) == KErrNone &&
Math :: Sin( lat2Sin, aLat2 * KPi / 180) == KErrNone &&
Math :: Cos( lat1Cos, aLat1 * KPi / 180) == KErrNone &&
Math :: Cos( lat2Cos, aLat2 * KPi / 180) == KErrNone &&
Math :: Cos( deltaLongCos, ( aLong2 - aLong1 ) * KPi / 180 ) == KErrNone &&
Math :: Sin( deltaLongSin, ( aLong2 - aLong1 ) * KPi / 180 ) == KErrNone )
{
TReal64 a = lat2Cos * deltaLongSin,
b = lat1Cos * lat2Sin - lat1Sin * lat2Cos * deltaLongCos,
c = lat1Sin * lat2Sin,
d = lat1Cos * lat2Cos * deltaLongCos,
e = a * a + b * b,
f;
if( Math :: Sqrt( f, e ) == KErrNone )
{
e = f / ( c + d );
if( Math :: ATan( f, e ) == KErrNone )
{
while( f < 0 )
f += KPi;
result = f * KEarthRadius;
}
}
}
return result;
}https://stackoverflow.com/questions/3109084
复制相似问题