Search nearby PHP implementation code and search php implementation code
The examples in this article share with you the specific code for implementing PHP search nearby for your reference. The specific content is as follows:
Implementation ideas:
First, we should think like this: since we know the longitude and latitude of the user's current location and the range we are going to search for, can we calculate a range? That is to say, the maximum and minimum values of the longitude and latitude that meet the condition are calculated based on a center point and a radius.
Specific implementation:
So far, the friends who want to think independently can not continue to read it.
We mentioned the implementation principle of this function above. Next we will explain the specific implementation steps.
We declare a function to calculate the latitude and longitude range:
/*** Calculate the range based on latitude and longitude and radius * @ param string $ lat latitude * @ param String $ lng longitude * @ param float $ radius * @ return Array range Array */ private function calcsat ($ lat, $ lng, $ radius) {$ degree = (24901*1609)/360.0; $ dpmLat = 1/$ degree; $ radiusLat = $ dpmLat * $ radius; $ minLat = $ lat-$ radiusLat; // minimum latitude $ maxLat = $ lat + $ radiusLat; // maximum latitude $ mpdLng = $ degree * cos ($ lat * (PI/180); $ dpmLng = 1/$ mpdLng; $ radiusLng = $ dpmLng * $ radius; $ minLng = $ lng-$ radiusLng; // minimum longitude $ maxLng = $ lng + $ radiusLng; // maximum longitude/** returned range array */$ scope = array ('minlat' => $ minLat, 'maxlat' => $ maxLat, 'minlng '=> $ minLng, 'maxlng' => $ maxLng); return $ scope ;}
The returned array contains the maximum and minimum longitude and latitude that meet the condition within the range of $ radius.
Now that we have obtained the range, we can start to search for all records that meet the conditions within the range of longitude and latitude from the database:
/*** Query all power stations in this range based on the longitude and latitude and radius * @ param String $ lat latitude * @ param String $ lng longitude * @ param float $ radius * @ return result calculated by Array */public function searchByLatAndLng ($ lat, $ lng, $ radius) {$ scope = $ this-> calcsat ($ lat, $ lng, $ radius); // call the Range Calculation function, obtain the maximum and minimum Latitude and longitude/** query the detailed address of the power station within the range of $ radius */$ SQL = 'select' field 'from' table name 'where 'latitude '<'. $ scope ['maxlat']. and 'latitude '> '. $ scope ['minlat']. and 'longyun' <'. $ scope ['maxlng ']. and 'longyun'> '. $ scope ['minlng ']; $ stmt = self: $ db-> query ($ SQL); $ res = $ stmt-> fetchAll (PDO: FETCH_ASSOC ); // obtain the query result and return $ res ;}
Extension:
Until now, we know how to calculate people nearby, but in actual needs, we often need to calculate the actual distance between each person and the current center.
Next, let's look at a method:
/*** Obtain the distance between two longitude and latitude dimensions * @ param string $ lat1 latitude 1 * @ param String $ lng1 longitude 1 * @ param String $ lat2 latitude 2 * @ param String $ lng2 longitude 2 * @ return float returns the distance between two points */public function calcDistance ($ lat1, $ lng1, $ lat2, $ lng2) {/** convert the data type to double */$ lat1 = doubleval ($ lat1); $ lng1 = doubleval ($ lng1 ); $ lat2 = doubleval ($ lat2); $ lng2 = doubleval ($ lng2);/** the following algorithms are developed by Google, consistent with the results of most latitude and longitude computing tools */$ theta = $ lng1-$ lng2; $ dist = sin (deg 2rad ($ lat1) * sin (deg 2rad ($ lat2 )) + cos (deg 2rad ($ lat1) * cos (deg 2rad ($ lat2) * cos (deg 2rad ($ theta); $ dist = acos ($ dist ); $ dist = rad2deg ($ dist); $ miles = $ dist * 60*1.1515; return ($ miles * 1.609344 );}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.