How to Calculate distance between two place using latitude-longitude in gmap for iPhone?
views:
95answers:
4
A:
You can use this method. It is taken from http://wiki.forum.nokia.com/index.php/How_to_calculate_distance_between_two_landmarks, but I suppose that math is the same for iPhone too. :)
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;
}
Haspemulator
2010-06-24 10:41:21
A:
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;
SamSol
2010-06-24 10:43:52
+1
A:
You can use the following method on a CLLocation object:
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
For example:
CLLocation* location_a;
CLLocation* location_b;
CLLocationDistance distance = [location_a distanceFromLocation:location_b];
Mr. Matt
2010-06-24 10:45:52