Calculate the distance between two points on the Earth's surface based on the two-point latitude

Source: Internet
Author: User
Tags abs cos pow sin

Now the application of geo-information is very hot, whether it is from the location of Google map information, or through the iphone or Android, such as mobile terminals with GPS modules to obtain geographical information, are decimal latitude and longitude information. We may need to compute the distance between two points on the Earth's surface when we do the actual application, and here is the example language of JavaScript, which provides a computational method, which is also the method used by Google map.

1. Decimal latitude and longitude degrees, minutes, seconds view plain copy to clipboard print?      function Dec2deg (dec) {var dec = math.abs (dec) + "";         Dec = Dec.split (".");         var deg = dec[0]; DEC[1] = "0."      + dec[1];      var min_sec = dec[1] * 3600;      var min = Math.floor (MIN_SEC/60);         var sec = (Min_sec-(min * 60));    return [deg, Min, sec]; }

function Dec2deg (dec) {
  var dec = math.abs (dec) + "";
  Dec = Dec.split (".");

  var deg = dec[0];

  DEC[1] = "0." + dec[1];
  var min_sec = dec[1] * 3600;
  var min = Math.floor (MIN_SEC/60);
  var sec =  (min_sec-(min *));

  return [deg, Min, sec];
}

2. Degrees, minutes, seconds to decimal latitude and Longitude view plain copy to clipboard print?      function Deg2dec (deg, Min, sec) {var deg = Math.Abs (deg);      var min = math.abs (min);      var sec = Math.Abs (sec);    return deg * 1 + (sec * 1 + min * 60)/3600; }

function Deg2dec (deg, Min, sec) {var deg = Math.Abs (deg);
  var min = math.abs (min);
  var sec = Math.Abs (sec);
return deg * 1 + (sec * 1 + min * 60)/3600; }

3. Calculate the distance between two points view plain copy to clipboard print?   Radians conversion    Function rad (d) {     return d * math.pi /  180.0;   }      //  calculates the distance, resulting in the unit km (km)    function caldistance (LAT1,  LNG1, LAT2, LNG2) {     if (   math.abs ( lat1 )  >  90  )  | | (  math.abs ( lat2 )  > 90 )  )         return false;         if (   math.abs ( lng1 )  > 180  )  | | (  math.abs ( lng2 )  > 180 )  )         return false;         var radlat1 = rad (LAT1);      var radlat2 = rad (LAT2);      var a = radlat1 - radlat2;      var b = rad (lng1)  - rad (lng2);      var s = 2 * math.asin (          math.sqrt (           math.pow ( Math.sin) (  a/2 ), 2 )  + math.cos ( radLat1 )  * math.cos ( radlat2  )  *            math.pow ( math.sin  b/2  ) (, 2 )          )      );      s = s * 6378.137 ; //  Earth radius  6378.137       s = math.round (s * 10000)  / 10000;      return s;   }  

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.