使用gmap for iPhone中的纬度 - 经度计算两个地方之间的距离

时间:2010-06-24 10:25:41

标签: iphone

如何使用gmap for iPhone中的纬度 - 经度计算两个地方之间的距离?

4 个答案:

答案 0 :(得分:6)

您可以在CLLocation对象上使用以下方法:

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location

例如:

CLLocation* location_a;
CLLocation* location_b;

CLLocationDistance distance = [location_a distanceFromLocation:location_b];

答案 1 :(得分:0)

答案 2 :(得分:0)

您可以使用此方法。它取自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;
}

答案 3 :(得分:0)

CLLocation *userLoc = [[CLLocation alloc] initWithCoordinate:appDelegate.mapView2.userLocation.coordinate];
CLLocation *poiLoc = [[CLLocation alloc] initWithLatitude: [aPOI.latitude doubleValue] longitude: [aPOI.longitude doubleValue]];

double dist = [userLoc getDistanceFrom:poiLoc] / 1000;