The latitude and longitude of two points are known, and the distance between two points is calculated (Php,javascript)

Source: Internet
Author: User
Tags asin cos pow sin

PHP Code: Reprint http://www.cnblogs.com/caichenghui/p/5977431.html

1 /**2 * Find the distance between two known longitude and latitude, in meters3  * 4 * @param lng1 $, lng2 longitude5 * @param lat1 $, lat2 latitude6 * @return float distance, Unit meter7 * @author www.Alixixi.com8  */9 functionGetdistance ($LNG 1,$lat 1,$LNG 2,$lat 2) {Ten     //turn angle into Fox degree One     $radLat 1=Deg2rad($lat 1);//the Deg2rad () function converts an angle to radians A     $radLat 2=Deg2rad($lat 2); -     $radLng 1=Deg2rad($LNG 1); -     $radLng 2=Deg2rad($LNG 2); the     $a=$radLat 1-$radLat 2; -     $b=$radLng 1-$radLng 2; -     $s= 2 *ASIN(sqrt(POW(Sin($a/2), 2) +Cos($radLat 1) *Cos($radLat 2) *POW(Sin($b/2), 2)) * 6378.137 * 1000; -     return $s; +}

JavaScript code:

1 //return Unit m2 get_distance (lat1,lng1,lat2,lng2) {3                 varRADLAT1 = LAT1 * math.pi/180.0;4                 varRADLAT2 = Lat2 * math.pi/180.0;5                 varA = RADLAT1-RadLat2;6                 varb = lng1 * Math.pi/180.0-lng2 * math.pi/180.0;7                 vars = 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)));8s = S * 6378.137;9s = Math.Round (S * 10000)/10000;Tens = s*1000; One                 returns; A},

The latitude and longitude of two points are known, and the distance between two points is calculated (Php,javascript)

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.