: This article mainly introduces the distance between users within three kilometers of php computing. if you are interested in the PHP Tutorial, please refer to it.
/*** Calculate the user within 3 kilometers * @ param type $ lng string longitude * @ param type $ lat string dimension * @ param type $ keyword * @ return type */public function actionNearUserlist () {$ lng =$ _ GET ['lng ']; $ lat =$ _ GET ['lat']; $ keyword =$ _ GET ['keyword']? $ _ GET ['keyword']: ""; $ half = 6371; $ distance = 20; // 3 km $ dlng = 2 * asin (sin ($ distance/(2 * $ half)/cos (deg 2rad ($ lat ))); $ dlng = rad2deg ($ dlng); $ dlat = $ distance/$ half; $ dlat = rad2deg ($ dlat ); $ fourpoint = array ('left-top' => array ('lat' => $ lat + $ dlat, 'lng '=> $ lng-$ dlng ), 'Right-top' => array ('lat' => $ lat + $ dlat, 'lng '=> $ lng + $ dlng ), 'Left-bottom' => array ('lat' => $ lat-$ dlat, 'Lng '=> $ lng-$ dlng), 'right-bottom' => array ('lat' => $ lat-$ dlat, 'lng '=> $ lng + $ dlng); $ where = ""; if ($ keyword) {$ where = "and. name like '% ". $ keyword. "% '";} // query the outlets in this range from the database $ SQL = "select uid, mobile, lng, lat from std_student where lat! = 0 and lng! = 0 and lat> ". $ fourpoint ['right-bottom '] ['lat']. "and lat <". $ fourpoint ['left-top'] ['lat']. "and lng> ". $ fourpoint ['left-top'] ['lng ']. "and lng <". $ fourpoint ['right-bottom '] ['lng']. "{$ where}"; $ res = Yii: app ()-> db-> createCommand ($ SQL)-> queryAll (); // print_r ($ res ); $ o = new StdStudent (); $ point1 = array ('lat' => $ lng, 'long' => $ lat); $ distanc = array (); foreach ($ res as $ k =>$ v) {$ distanc = $ o-> getTwoDistance ($ point1 ['lat'], $ point1 ['long'], $ v ['lng '], $ v ['lat']); $ res [$ k] ['meters'] = $ distanc ['meters'];} // print_r ($ res); if ($ res) {$ this->AjaxMessage (0, 'users in three kilometers nearby ', $ res);} else $ this->AjaxMessage (-1, 'attachment user query failed ');}
public function getTwoDistance($latitude1,$longitude1,$latitude2,$longitude2) { $theta = $longitude1 - $longitude2; $miles = (sin(deg2rad($latitude1)) * sin(deg2rad($latitude2))) + (cos(deg2rad($latitude1)) * cos(deg2rad($latitude2)) * cos(deg2rad($theta))); $miles = acos($miles); $miles = rad2deg($miles); $miles = $miles * 60 * 1.1515; $feet = $miles * 5280; $yards = $feet / 3; $kilometers = $miles * 1.609344; $meters = $kilometers * 1000; return compact('miles','feet','yards','kilometers','meters'); }
The preceding section describes the distance between users within three kilometers of php computing, including related content, and hopes to help those who are interested in PHP tutorials.