: This article mainly introduces how to determine which stores are closest to customers by longitude and latitude, for example, the nearest stores within 1000. if you are interested in PHP tutorials, refer to it. Recently, the company needs to query the stores closest to the customer's address through the customer's receiving address. the customer can pick up the goods at the nearest store.
How can we calculate the number of stores with the customer address within 1000? We can calculate the number of parts below.
1. obtain the longitude and latitude of the customer's address. we can use the interface provided by Baidu Map. ($ address is the customer's address)
// Obtain the public function getlat ($ address) {$ url = 'http: // api.map.baidu.com/geocoder/v2 /? City = Shanghai & address = '. $ address. '& output = json & ak = '. $ this-> ak; $ ch = curl_init ($ url); curl_setopt ($ ch, CURLOPT_URL, $ url); curl_setopt ($ ch, CURLOPT_HEADER, 0 ); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); curl_setopt ($ ch, CURLOPT_NOSIGNAL, 1); $ data = curl_exec ($ ch); curl_close ($ ch ); $ data = json_decode ($ data, true); $ ret ['lat'] = $ data ['result'] ['location'] ['lat']; $ ret ['lng '] = $ data ['result'] ['location'] ['lng']; echo json_encode ($ ret );}
2. if the store is within 1000, we need to calculate the latitude and longitude range within 1000.
/** Calculate the latitude and longitude range * $ lat latitude * $ lon longitude * $ raidus radius (meters) */function getAround ($ lat, $ lon, $ raidus) {$ PI = 3.14159265; $ EARTH_RADIUS = 6378137; $ RAD = $ PI/180.0; $ latitude = $ lat; $ longpolling = $ lon; $ degree = (24901*1609) /360.0; $ raidusMile = $ raidus; $ dpmLat = 1/$ degree; $ data = array (); $ radiusLat = $ dpmLat * $ raidusMile; $ minLat = $ latitude-$ radiusLat; $ maxLat = $ latitude + $ radiusLat; $ data ["maxLat"] = $ maxLat; $ data ["minLat"] = $ minLat; $ mpdLng = $ degree * cos ($ latitude * ($ PI/180); $ dpmLng = 1/$ mpdLng; $ radiusLng = $ dpmLng * $ raidusMile; $ minLng = $ longyun-$ radiusLng; $ maxLng = $ longyun+ $ radiusLng; $ data ["maxLng"] = $ maxLng; $ data ["minLng"] = $ minLng; // print_r ($ data); return $ data ;}
3. we know that each store in the database stores the latitude and longitude information of the store. now we can use the latitude and longitude range calculated above to find out which stores are within 1000.
// Calculate the public function getdz ($ lat, $ lng) {include_once ('My _ db. php '); $ this-> qu = new MY_DB_ALL ("QUICK"); // $ ret = json_decode ($ this-> getlat ($ address), true ); // print_r ($ ret); exit; $ data = $ this-> getAround ($ lat, $ lng, 2000); // print_r ($ data ); $ SQL = "select * from shop where baidu_lat '". $ data ['minlat']. "'and '". $ data ['maxlat']. "'and baidu_lng '". $ data ['minlng ']. "'and '". $ data ['maxlng ']. "'and status = 1 and ztd_flag = 2"; $ ret = $ this-> qu-> rquery ($ SQL); for ($ I = 0; $ I
"; Print_r ($ array); exit; echo json_encode ($ array );}
The above code needs to be compiled according to your actual situation. you can focus on the SQL statement $ SQL.
4. if you want to calculate which store is closest to the customer's address, you need to calculate the distance as follows:
/*** @ Desc calculate the distance * @ param float $ lat latitude value * @ param float $ lng longitude value */public function getDistance ($ lat1, $ lng1, $ lat2, $ lng2) {$ earthRadius = 6367000; // Earth radius $ lat1 = ($ lat1 * pi ()/180; $ lng1 = ($ lng1 * pi ()/180; $ lat2 = ($ lat2 * pi ()/180; $ lng2 = ($ lng2 * pi ()) /180; $ calclong.pdf = $ lng2-$ lng1; $ calcLatitude = $ lat2-$ lat1; $ stepOne = pow (sin ($ calcLatitude/2), 2) + cos ($ lat1) * cos ($ lat2) * pow (sin ($ calclong133/2), 2); $ stepTwo = 2 * asin (min (1, sqrt ($ stepOne); $ calculatedDistance = $ earthRadius * $ stepTwo; return round ($ calculatedDistance );}
5. Finally, we can know the nearest store by calculating the minimum distance between those stores within 1000.
// Calculate the latest public function zjd ($ address) {$ ret = $ this-> getdz ($ address ); $ jwd = $ this-> getlat ($ address); if ($ ret) {$ arr = array (); foreach ($ ret as $ k => $ v) {$ arr [$ k] = $ this-> getDistance ($ jwd ['lat'], $ jwd ['lng '], $ v ['Baidu _ lat'], $ v ['Baidu _ lng ']);} asort ($ arr); // print_r ($ arr); foreach ($ arr as $ k1 => $ v1) {$ data [] = $ ret [$ k1];} print_r ($ data);} else {echo 'no recent stores ';}}
The above section describes how to determine which stores are closest to the customers through the longitude and latitude, for example, which of the following stores are closest to the customers within 1000, including relevant content, if you are interested in PHP tutorials.