PHP queries nearby locations based on a given latitude and longitude point? Rational use of algorithms
The implementation principle first calculates the four points of the rectangle around the point, and then uses the longitude and latitude to directly match the records in the database. // Obtain the surrounding coordinate public function returnSquarePoint ($ lng, $ lat, $ distance = 0.5) {$ earthRadius = 6378138; $ dlng = 2 * asin (sin ($ distance/(2 * $ earthRadius)/cos (deg 2rad ($ 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 ));} // calculate the straight line distance between two coordinates. public function getDistance ($ lat1, $ lng1, $ lat2, $ lng2) {$ earthRadius = 6378138; // converts the approximate Earth radius meter to Radian $ lat1 = ($ lat1 * pi ()/180; $ lng1 = ($ lng1 * pi ()/180; $ lat2 = ($ lat2 * pi ()/180; $ lng2 = ($ lng2 * pi ()/180; // use the semi-positive vector formula to calculate $ calclong133 = $ lng2-$ lng1; $ calcLatitude = $ lat2-$ lat1; $ stepOne = pow (sin ($ calcLatitude/2), 2) + cos ($ lat1) * cos ($ lat2) * pow (sin ($ calcLongitude/2 ), 2); $ stepTwo = 2 * asin (min (1, sqrt ($ stepOne); $ calculatedDistance = $ earthRadius * $ stepTwo; return round ($ calculatedDistance );}