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