The first kind of apple comes with
[CPP]View PlainCopy
- Cllocation *orig=[[[cllocation alloc] initwithlatitude:[maindelegate.latitude_self Doublevalue] longitude:[ Maindelegate.longitude_self Doublevalue]] autorelease];
- cllocation* dist=[[[cllocation alloc] initwithlatitude:[tmpnewsmodel.latitude Doublevalue] longitude:[ Tmpnewsmodel.longitude Doublevalue]] autorelease];
- Cllocationdistance Kilometers=[orig distancefromlocation:dist]/1000;
- NSLog (@"Distance:", kilometers);
The second type, manual calculation
[CPP]View PlainCopy
- #pragma mark-calculate distance calculates distances based on 2 latitude and longitude degrees
- #define PI 3.1415926
- + (Double) Lantitudelongitudedist: (Double) Lon1 Other_lat: (double)lat1 Self_lon: (double) Lon2 self_ Lat: (Double) lat2{
- Double er = 6378137; //6378700.0f;
- //ave. Radius = 6371.315 (Someone said more accurate is 6366.707)
- //equatorial radius = 6378.388
- //nautical mile = 1.15078
- double radlat1 = pi*lat1/180.0f;
- double radlat2 = pi*lat2/180.0f;
- //now Long.
- double radlong1 = pi*lon1/180.0f;
- double radlong2 = pi*lon2/180.0f;
- if (RADLAT1 < 0) RADLAT1 = pi/2 + fabs (RADLAT1); // South
- if (radlat1 > 0) radlat1 = PI/2-Fabs (RADLAT1); // North
- if (radlong1 < 0) radlong1 = pi*2-fabs (radlong1); //west
- if (Radlat2 < 0) Radlat2 = pi/2 + fabs (RADLAT2); // South
- if (radlat2 > 0) radlat2 = PI/2-Fabs (RADLAT2); // North
- if (radlong2 < 0) radlong2 = pi*2-fabs (radlong2); //West
- //spherical coordinates x=r*cos (AG) sin (at), Y=r*sin (AG) *sin (at), Z=r*cos (at)
- //zero AG is up so reverse lat
- Double x1 = er * cos (radlong1) * sin (RADLAT1);
- Double y1 = er * sin (radlong1) * sin (RADLAT1);
- Double z1 = er * cos (RADLAT1);
- Double x2 = er * cos (radlong2) * sin (RADLAT2);
- Double y2 = er * sin (radlong2) * sin (RADLAT2);
- Double z2 = er * cos (RADLAT2);
- Double d = sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (y1-y2) + (Z1-Z2) * (Z1-Z2));
- //side, side, side, Law of cosines and Arccos
- Double theta = ACOs ((er*er+er*er-d*d)/(2*er*er));
- Double dist = theta*er;
- return dist;
- }
iOS calculates distance distances based on two latitude and longitude