Php calculates the distance between users within 3 kilometers

Source: Internet
Author: User
: 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.

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.