Recently, the company needs to check the customer's delivery address to find out which stores are closest to the customer's address, and the customer can go to the nearest store to pick up the goods.
So how do we figure out which stores are within 1000 meters of the customer's address? We can calculate it from the following sections.
1. Obtain the latitude and longitude of the customer's address, we can get through the interface provided by Baidu Map. ($address for customer address)
Baidu interface get latitude and longitude 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 meters, we need to calculate the latitude and longitude range within 1000 meters.
/* * Calculate latitude and longitude range * $lat latitudes * $lon Longitude * $raidus radius (m) */function Getaround ($lat, $ Lon, $raidus) {$PI = 3.14159265; $EARTH _radius = 6378137; $RAD = $PI/180.0; $latitude = $lat; $longitude = $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 = $longitude-$radiusLng; $maxLng = $longitude + $radiusLng; $data ["maxlng"] = $maxLng; $data ["minlng"] = $minLng; Print_r ($data); return $data; }
3. We know that every store in the database stores the latitude and longitude information of this store, and now we can find out which stores are within 1000 meters through the latitude and longitude range calculated above.
Calculates the store public function Getdz within the radius ($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 between '". $data [' Minlat ']. "' and '". $data [' Maxlat ']. "' and baidu_lng between '". $data [' minlng ']. "' and '". $data [' maxlng ']. "' and Status=1 and ztd_flag=2"; $rett = $this->qu->rquery ($sql); for ($i =0; $i
";p Rint_r ($array); exit; echo Json_encode ($array); }
4. If you want to figure out which store is closest to the client, I need a way to calculate the distance, as follows:
/** * @desc calculates distance according to latitude and longitude between two points * @param float $lat latitude value * @param float $lng Longitude value */public function Getdistance ($lat 1, $lng 1, $lat 2, $lng 2) { $earthRadius = 6367000;//Earth radius $lat 1 = ($lat 1 * PI ())/180;< c11/> $lng 1 = ($lng 1 * PI ())/; $lat 2 = ($lat 2 * PI ())/; $LNG 2 = ($lng 2 * PI ())/; $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); }
5. Finally, we can find out which shop has been recently by calculating the minimum distance between the stores within 1000 meters.
Calculates the nearest store 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 $k 1 = $v 1) { $data [] = $ret [$k 1]; } Print_r ($data); } else { echo ' no nearest store '; } }
The above is introduced through the latitude to determine a certain range of stores away from the customer recently, such as 1000 meters within the nearest a certain store, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.