最近公司需要透過客戶的收貨地址查詢離客戶地址最近有哪些門市,客戶可以去最近的門市取貨.
那我們是如何計算出客戶地址1000米內有哪些門市呢?我們可以通過下面幾部計算出來.
1.取得客戶地址的經緯度,我們可以透過百度地圖提供的介面取得.($address為客戶地址)
//百度接口获取经纬度 public function getlat($address) { $url = 'http://api.map.baidu.com/geocoder/v2/?city=上海&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.如果是1000公尺內的店,我們需要計算出1000公尺內的經緯度範圍.
/* * 计算经纬度范围 * $lat 纬度 * $lon 经度 * $raidus 半径(米) */ 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.我們知道數據庫內每一家店都儲存了這家店的經緯度信息,現在我們可以通過上面計算出的經緯度範圍來查詢出1000米內有哪些門店了.
//计算出半径范围内的店 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 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<count($rett);$i++) { $array[$i]["shop_id"] = $rett[$i]["shop_id"]; $array[$i]["shop_name"] = iconv("gbk","utf-8",$rett[$i]["shop_name"]); $array[$i]["shop_address"] = iconv("gbk","utf-8",$rett[$i]["shop_address"]); $array[$i]["shop_date"] = $rett[$i]["shop_date"]; $array[$i]['shop_phone'] = $rett[$i]["shop_phone"]; $array[$i]['area'] = $rett[$i]["area"]; } //echo "<pre class="brush:php;toolbar:false">";print_r($array);exit; echo json_encode($array); }
4.如果想計算出離客戶址最近的一家店是哪家,我需要一個計算距離的方法,如下:
/** * @desc 根据两点间的经纬度计算距离 * @param float $lat 纬度值 * @param float $lng 经度值 */ public function getDistance($lat1, $lng1, $lat2, $lng2) { $earthRadius = 6367000; //地球半径 $lat1 = ($lat1 * pi() ) / 180; $lng1 = ($lng1 * pi() ) / 180; $lat2 = ($lat2 * pi() ) / 180; $lng2 = ($lng2 * pi() ) / 180; $calcLongitude = $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); }
//计算出最近的一家店 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 '无最近的门店'; } }
以上就介紹了透過經緯度確定一定範圍內哪些門店離客戶最近,如1000米內有哪些最近的某某門店,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。