iOS calculates distance distances based on two latitude and longitude

Source: Internet
Author: User

The first kind of apple comes with

[CPP]View PlainCopy
    1. Cllocation *orig=[[[cllocation alloc] initwithlatitude:[maindelegate.latitude_self Doublevalue] longitude:[ Maindelegate.longitude_self Doublevalue]] autorelease];
    2. cllocation* dist=[[[cllocation alloc] initwithlatitude:[tmpnewsmodel.latitude Doublevalue] longitude:[ Tmpnewsmodel.longitude Doublevalue]] autorelease];
    3. Cllocationdistance Kilometers=[orig distancefromlocation:dist]/1000;
    4. NSLog (@"Distance:", kilometers);

The second type, manual calculation

[CPP]View PlainCopy
  1. #pragma mark-calculate distance calculates distances based on 2 latitude and longitude degrees
  2. #define PI 3.1415926
  3. + (Double) Lantitudelongitudedist: (Double) Lon1 Other_lat: (double)lat1 Self_lon: (double) Lon2 self_ Lat: (Double) lat2{
  4. Double er = 6378137;  //6378700.0f;
  5. //ave. Radius = 6371.315 (Someone said more accurate is 6366.707)
  6. //equatorial radius = 6378.388
  7. //nautical mile = 1.15078
  8. double radlat1 = pi*lat1/180.0f;
  9. double radlat2 = pi*lat2/180.0f;
  10. //now Long.
  11. double radlong1 = pi*lon1/180.0f;
  12. double radlong2 = pi*lon2/180.0f;
  13. if (RADLAT1 < 0) RADLAT1 = pi/2 + fabs (RADLAT1); // South
  14. if (radlat1 > 0) radlat1 = PI/2-Fabs (RADLAT1); // North
  15. if (radlong1 < 0) radlong1 = pi*2-fabs (radlong1); //west
  16. if (Radlat2 < 0) Radlat2 = pi/2 + fabs (RADLAT2); // South
  17. if (radlat2 > 0) radlat2 = PI/2-Fabs (RADLAT2); // North
  18. if (radlong2 < 0) radlong2 = pi*2-fabs (radlong2); //West
  19. //spherical coordinates x=r*cos (AG) sin (at), Y=r*sin (AG) *sin (at), Z=r*cos (at)
  20. //zero AG is up so reverse lat
  21. Double x1 = er * cos (radlong1) * sin (RADLAT1);
  22. Double y1 = er * sin (radlong1) * sin (RADLAT1);
  23. Double z1 = er * cos (RADLAT1);
  24. Double x2 = er * cos (radlong2) * sin (RADLAT2);
  25. Double y2 = er * sin (radlong2) * sin (RADLAT2);
  26. Double z2 = er * cos (RADLAT2);
  27. Double d = sqrt ((x1-x2) * (X1-X2) + (y1-y2) * (y1-y2) + (Z1-Z2) * (Z1-Z2));
  28. //side, side, side, Law of cosines and Arccos
  29. Double theta = ACOs ((er*er+er*er-d*d)/(2*er*er));
  30. Double dist = theta*er;
  31. return dist;
  32. }

iOS calculates distance distances based on two latitude and longitude

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.