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