Php,mysql calculating distances and sorting according to latitude and longitude

Source: Internet
Author: User
Tags asin cos pow sin

Calculation formula:

Mysql:
 //lng1 represents a point of latitude and longitude, Lat2 Lng2 represents B-point latitude and longitude//a  = Lat1–lat2 is two points of latitude difference b = Lng1-lng2 is the difference of two points of longitude //6378. 137 is the radius of the Earth, in kilometers per km//calculated result units for km SELECT  *, (2 * 6378.137* asin  (sqrt  (pow  ( sin  (pi  () * (111.86141967773438-LNG)/360), 2) +cos  (pi  () *33.07078170776367/180) * cos  (lat * pi  ()/180) *pow  (sin  (pi  () * (33.07078170776367-lat)/360), 2))) as   Juli from ' area ' ORDER by Juli ASC limit  0,20 

Php:

functionGetdistance ($LNG 1,$lat 1,$LNG 2,$lat 2){        //turn angle into Fox degree        $radLat 1=Deg2rad($lat 1);//the Deg2rad () function converts an angle to radians        $radLat 2=Deg2rad($lat 2); $radLng 1=Deg2rad($LNG 1); $radLng 2=Deg2rad($LNG 2); $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;//The calculated result is measured in meters.
return  Floor ($s);}

Php,mysql calculating distances and sorting according to latitude and longitude

Related Article

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.