时间:2021-07-01 10:21:17 帮助过:9人阅读
那我们是如何计算出客户地址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";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教程有兴趣的朋友有所帮助。