Ideas:
1, get my position, that is: my latitude and longitude
2, each site must have a location that is: sorting objects have the latitude and longitude location
3, the query site list to be sorted
4, loop through my distance calculation and
5, a two-dimensional array of the specified column (distance) ordering
details as follows:
1, is a small program function, which applet interface to obtain my latitude and longitude
wx.getLocation ({ type: 'WGS84' , Success (RES) {
the let Latitude = res.latitude; // latitude
let longitude = res.longitude; // longitude
// sort request carries the above parameters
}
})
2, the new site when the project must have Location Map function, save data for each site that contains the site's latitude and longitude data
3, suppose the query query site list slightly site data as follows:
$ List = [ [ 'ID' =>. 1, 'name' => 'site. 1', 'Latitude' => 34.45678, 'longitude' => 116.43055], [ 'ID' => 2, 'name' => 'site 2', 'Latitude' => 34.35678, 'longitude' => 116.43055], [ 'ID' =>. 3, 'name' => 'site 3', 'latitude' => 34.55678, 'longitude' => 116.43055 ] ]
4, calculates the distance to loop through
Latitude $ = $ _POST [ 'my_latitude']; // I latitude $ Latitude = $ _POST [ 'my_latitude']; // I longitude of the foreach ( $ List AS $ K => $ V ) { $ List [ $ K ] [ 'distance'] = get_distance ( $ my_latitude , $ my_longitude , $ V [ 'Latitude'], $ V [ 'longitude' ]); } // distance between two latitude and longitude function get_distance ( $ LAT1 , $ lon1 , LAT2 $ , $ lon2) { $ R & lt = 6,371,393; // Earth mean radius, in meters $ DLAT = deg2rad ( $ LAT2 - $ LAT1 ); // angle into radian $ dlon = deg2rad ( $ lon2 - $ lon1 ); $ A = POW ( SiN ( $ DLAT / 2), 2) + COS ( deg2rad ( $ LAT1 )) * COS ( deg2rad ( $ LAT2 )) * POW ( SiN ( $ dlon / 2), 2 ); $c = 2 * atan2(sqrt($a), sqrt(1-$a)); $d = $R * $c; return round($d); }
5, sorted by distance
List $ = arr_sort ( $ List , 'Distance' ); // two-dimensional array of two-dimensional designated column sort // $ arr to be sort of two-dimensional array // $ key to sort columns // $ order ascending | descending default ascending function arr_sort ( $ ARR , $ Key , $ Order = SORT_ASC) { $ key_arr = array_column ( $ ARR , $ Key ); IF ( empty ( $ key_arr )) { return to false ; } array_multisort ( $ key_arr , $ Order , $ arr ); return $arr; }