Home  >  Article  >  Backend Development  >  Use longitude and latitude to determine which stores are closest to customers within a certain range, such as which stores are closest to a certain store within 1,000 meters.

Use longitude and latitude to determine which stores are closest to customers within a certain range, such as which stores are closest to a certain store within 1,000 meters.

WBOY
WBOYOriginal
2016-08-08 09:27:362240browse

Recently, the company needs to use the customer's delivery address to check which stores are closest to the customer's address. Customers can go to the nearest store to pick up the goods.

So how do we calculate which stores are within 1000 meters of the customer's address? We can use the following

1. Get the longitude and latitude of the customer's address, we can get it through the interface provided by Baidu Map. ($address is the customer 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. If it is a store within 1000 meters, we need to calculate the distance within 1000 meters The longitude and latitude range.

    /*
     * 计算经纬度范围
     * $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. We know that each store in the database stores the longitude and latitude information of this store. Now we can use the longitude and latitude range calculated above to query which stores are within 1000 meters.

    //计算出半径范围内的店
    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][&#39;shop_phone&#39;] = $rett[$i]["shop_phone"];
            $array[$i][&#39;area&#39;] = $rett[$i]["area"];
        }
        //echo "<pre class="brush:php;toolbar:false">";print_r($array);exit;
        echo json_encode($array);
    }

Above The code needs to be written according to your actual situation. You can mainly look at the SQL statement $sql. Hehe

4. If I want to calculate which store is closest to the customer's address, I need a method to calculate the distance, as follows:

    /**
     *  @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);
    }

5. Finally, we can know the closest store by calculating and comparing the minimum distances of the stores within 1000 meters. Which store is it?
    //计算出最近的一家店
    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 '无最近的门店';
        }
    }

The above introduces how to determine which stores are closest to customers within a certain range through longitude and latitude, such as which stores are closest to a certain store within 1000 meters, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn