2010-06-24 3 views

Répondre

6

Vous pouvez utiliser la méthode suivante sur un objet CLLocation:

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

Par exemple:

CLLocation* location_a; 
CLLocation* location_b; 

CLLocationDistance distance = [location_a distanceFromLocation:location_b]; 
0

Vous pouvez utiliser cette méthode. Il est pris de http://wiki.forum.nokia.com/index.php/How_to_calculate_distance_between_two_landmarks, mais je suppose que les mathématiques sont les mêmes pour l'iPhone aussi. :)

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; 
} 
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; 
Questions connexes