On the calculation of the distance between two longitudes and latitudes

Time:2021-5-4

Generally speaking, the linear distance between two points we usually see when calculating latitude and longitude is directly linked to the distance between the driving and cycling curves, as the map docking of APP docking map, official account and other small programs, etc., so the two cannot be equated attention.

Here are two ways to write the linear distance

Please pay attention to the problem before transfer

Longitude
latitude latitude

 

If the data cannot be called or the distance is not right, consider whether the latitude and longitude are reversed first

 

1 public method call

1 /**
 2 * calculate the distance between two geographical coordinates
 3 * @ param decimal $longitude1 starting longitude
 4 * @ param decimal $latitude1 starting latitude
 5 * @ param decimal $longitude2 end longitude
 6 * @ param decimal $latitude2 end latitude
 7 * @ param int $unit 1: M 2: km
 8 * @ param int $decimal
 9  * @return Decimal
10  */
11 
12 function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=1, $decimal=2){
13     $EARTH_ RADIUS = 6370.996; //  Earth radius coefficient
14     $PI = 3.1415926;
15     $radLat1 = $latitude1 * $PI / 180.0;
16     $radLat2 = $latitude2 * $PI / 180.0;
17     $radLng1 = $longitude1 * $PI / 180.0;
18     $radLng2 = $longitude2 * $PI /180.0;
19     $a = $radLat1 - $radLat2;
20     $b = $radLng1 - $radLng2;
21     $distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
22     $distance = $distance * $EARTH_RADIUS * 1000;
23     if($unit==2){
24         $distance = $distance / 1000;
25     }
26     return round($distance, $decimal);
27 }

2 pagination display distance data

1 public function list(){
 2         $data = $this->request->param();
 3         $page=$data['page'];
 4         $where['status']= 1;
 5 
 6         $lat=$data['lat'];
 7         $lng=$data['lng'];
 8 
 9         $order='mi asc';
10         $list= StoreApply::field("*,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(({$lat}*PI()/180-lng*PI()/180)/2),2)+COS({$lat}*PI()/180)*COS(lng*PI()/180)*POW(SIN(({$lng}*PI()/180-lat*PI()/180)/2),2)))*1000) as mi")
11             ->where($where)
12             ->order($order)
13             ->page($page,16)
14             ->select();
15 
16 $this - > success ('request successful ', compact ('List');
17     }

There is a slight difference between the above two forms of algorithm, but a big difference

In addition, the method is involved

1 /*
 2 * get the N range near the current position
 3  * */
 4 
 5 function SquarePoint($lng, $lat,$distance = 5){
 6     $dlng =  2 * asin(sin($distance / (2 * 6371)) / cos(deg2rad($lat)));
 7     $dlng = rad2deg($dlng);
 8     $dlat = $distance/6371;
 9     $dlat = rad2deg($dlat);
10     return array(
11         'left-top'=>array('lat'=>$lat + $dlat,'lng'=>$lng-$dlng),
12         'right-top'=>array('lat'=>$lat + $dlat, 'lng'=>$lng + $dlng),
13         'left-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng - $dlng),
14         'right-bottom'=>array('lat'=>$lat - $dlat, 'lng'=>$lng + $dlng)
15     );
16 }

 

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]