PHP MYSQL searches the perimeter coordinates and calculates the distance between the two points

Source: Internet
Author: User
Tags asin cos php mysql pow sin

Search for nearby locations, for example, within 500 meters of coordinates (39.91, 116.37), first figure out the range of coordinates for the "500 meters near the given coordinates". Although it is a circle, we can first find out the circle's external square, and then take the square latitude and longitude range to search the database. The red part is the required search scope, and the green part is the actual search scope.

/** * Get around coordinates * Enter description here ... * @param unknown_type $lng Fixed coordinates longitude * @param unknown_type $lat fixed coordinates latitude * @param Unknown_type $distance searching for nearby distances, in km * @return multitype:multitype:number*/functionReturnsquarepoint ($LNG,$lat,$distance= 1){    $earthRadius= 6378.138;//radius of the Earth, per km    $DLNG= 2 *ASIN(Sin($distance/(2 *$earthRadius)) /Cos(Deg2rad($lat))); $DLNG=rad2deg($DLNG); $dlat=$distance/$earthRadius; $dlat=rad2deg($dlat); return 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)    ); //Use this function to calculate the resulting result, and bring in the SQL query. $squares = Returnsquarepoint ($LNG, $lat);//$info _sql = "Select Id,locateinfo,lat,lng from ' Lbs_info ' where LAT&L T;>0 and lat>{$squares [' Right-bottom '] [' lat ']} and lat<{$squares [' left-top '] [' lat ']} and lng>{$squares [' Left-top ' [' LNG ']} and lng<{$squares [' Right-bottom '] [' LNG ']} ";}/** * Calculate the straight line distance of two coordinates * Enter description here ... * @param unknown_type $lat 1 * @param unknown_type $lng 1 * @param unkno Wn_type $lat 2 * @param unknown_type $LNG 2*/functionGetdistance ($lat 1,$LNG 1,$lat 2,$LNG 2){          $earthRadius= 6378.138;//approximate earth radius km//convert to radians    $lat 1= ($lat 1*Pi())/180; $LNG 1= ($LNG 1*Pi())/180; $lat 2= ($lat 2*Pi())/180; $LNG 2= ($LNG 2*Pi())/180; //use the semi-normal vector formula to calculate with the ruler gauge    $calcLongitude=$LNG 2-$LNG 1; $calcLatitude=$lat 2-$lat 1; $stepOne=POW(Sin($calcLatitude/2), 2) +Cos($lat 1) *Cos($lat 2) *POW(Sin($calcLongitude/2), 2); $stepTwo= 2 *ASIN(min(1,sqrt($stepOne))); $calculatedDistance=$earthRadius*$stepTwo; return round($calculatedDistance);}

PHP MYSQL searches the perimeter coordinates and calculates the distance between the two points

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.