php+高德地图webapi 高德jsapi 实现 当前位置与目标位置距离 并按照距离排序(坐标逆转换)

<script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak='自己申请'&s=1"></script>
<script type="text/javascript" charset="utf-8">


        var geolocation = new BMap.Geolocation();
        geolocation.getCurrentPosition(function (r) {
            if (this.getStatus() == BMAP_STATUS_SUCCESS) {
                var lat = r.point.lat;
                var lng = r.point.lng;
            }
            // return;
            var data = {'lat': lat, 'merchid': "{$item['id']}",'classid': "{$classitem['id']}", 'lng': lng};
            $.ajax({
                url: "{php echo mobileUrl('shop/category/ajaxcodot')}",
                data: data,
                cache: false
            }).done(function (result) {
                var data = jQuery.parseJSON(result);
                if (data.status == 1) {
                    if(data.result.code == 1){
                        console.log(data.result.code)
                        $(".shop").html(data.result.html)
                        // $("#coll").hide()
                    }else{
                        $(".shop").hide()
                        $(".noinfo").show()
                    }
                } else {
                    alert("微信接口繁忙,请稍后再试!");
                    
                }
            });  
        })
 
</script>
首先定位当前位置
    public function ajaxdot()//查询结果页
    {
         global $_W;
        global $_GPC;

        $province = $_GPC['province'];
        $city = $_GPC['city'];
        $area = $_GPC['area'];
        $classid = $_GPC['classid'];

        
        $lat = floatval($_GPC['lat']);
        $lng = floatval($_GPC['lng']);

        $merch = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_merch_user') . ' WHERE id = '.$_GPC['merchid']);           
        $where = 'where 1';
        $where.= " and province = '$province'";
        if($merch['cateid'] != 6)
        $where.= " and company_number = '{$merch['company_number']}'";
        if(!empty($city)){
            $where.= " and city = '$city'";
        }        
        if(!empty($area)){
            $where.= " and area = '$area'";
        }
        
        
        if($merch['cateid'] == 1 && $classid){
          //  var_dump($classid);exit;
             $class = pdo_fetch('SELECT class_type FROM ' . tablename('ewei_shop_merch_user_class') . ' WHERE id = '.$classid);  
             $class_type = $class['class_type'];
          $where.= " and class_type = '$class_type'";
        }        
 
        
        if(!empty($_GPC['classid'])){
            $classitem = pdo_fetch('SELECT * FROM ' . tablename('ewei_shop_merch_user_class') . ' WHERE id = :classid',array('classid' => $_GPC['classid']));            
        }
        
        $table = tablename('ewei_shop_merch_user_address');
        if($merch['cateid'] == 6){
            if($merch['company_number'] == 'TXDX001'){
                 $where.= " and class_type = 'DX'";
            }else if($merch['company_number'] == 'TXDX002'){
                $where.= " and class_type = 'YD'";
            }else if($merch['company_number'] == 'TXDX003'){
                $where.= " and class_type = 'LT'";
            }
           $table =tablename('ewei_shop_merch_tx_address');
        }
        
        
        $detail = pdo_fetchall("SELECT * FROM $table " ."$where");
        
                // var_dump($detail);
        
        if($detail){
            $html = '';
            foreach ($detail as $key=>$v) {
                if($v['lat'] == '' || $v['lng'] == ''){
                       $lntarr = $this->addresstolatlag($v['detail']);
                    $store['lng'] = $lntarr[0];
                    $store['lat'] = $lntarr[1]; 
                    // var_dump($lntarr);
                    $lat_num = explode('.', $store['lat']);
                    if (1 < sizeof($lat_num)) {
                        $decimal = end($lat_num);
                        $count = strlen($decimal);
                        if ($count <= 6) {
                            $gcj02 = $this->Convert_GCJ02_To_BD09($store['lat'], $store['lng']);
                            $store['lat'] = $gcj02['lat'];
                            $store['lng'] = $gcj02['lng'];
                        }
                    }
                    $distance = m('util')->GetDistance($lat, $lng, $store['lat'], $store['lng'], 2);
                    $where = array();
                    $where['id'] = $v['id'];
                    pdo_update('ewei_shop_merch_user_address', $store,$where);
                }else{
                    
                    $distance = m('util')->GetDistance($lat, $lng, $v['lat'], $v['lng'], 2);
                }
                $detail[$key]['distance'] = $distance;
                $flag[] = $distance;
            }
            array_multisort($flag, SORT_ASC, $detail); //按距离排序
            
            foreach ($detail as $key=>$v) {
                $distance = $v['distance'];
                $html.='<div class="iimg">
                <img id="" width="110" src="../addons/ewei_shopv2/static/img/4S.jpg" alt="" />
                </div>
                <div class="info">
                <p class="s_title">'.$v["shopname"].'</p>
                <p class="s_address"> '.$v['detail'].'</p>
                <p class="s_address" style="    color: green;"> 距您'.$distance.'km</p>
                <div class="s_bot">
                    <a href="tel:'.$v["tel1"].'" style="color:#000"><div class="bimgl" style="    width: 40px;"><img width="20" src="../addons/ewei_shopv2/static/img/carTel.jpg" alt="" /> </div>
                    <div class="btel"> '.$v['tel1'].'</div></a>
                    <div class="bimgr" onclick="docollectad('.$v['id'].','.$table.')"><img width="20" src="../addons/ewei_shopv2/static/img/carSC.jpg" alt="" /></div>
                    <a href="./index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=shop.category.address&merchid='.$merch['id'].'&detail='.$v['detail'].'&shopname='.$v['shopname'].'"><div class="bimgr"><img width="20" src="../addons/ewei_shopv2/static/img/carDW.jpg" alt="" /></div></a>
                    <div class="t_clear"></div>
                </div>        
                </div>
                <div class="t_clear"></div>';
            }            
        
              $arr = array('code' => 1, 'html'=>$html);
            show_json(1, $arr);           
        }else{
               $arr = array('code' => 2);
            show_json(1, $arr);  
        }
    }    
计算距离 并返回 数据
    /**
     * 计算两组经纬度坐标 之间的距离
     * params :lat1 纬度1; lng1 经度1; lat2 纬度2; lng2 经度2; len_type (1:m or 2:km);
     * return m or km
     */
    public function GetDistance($lat1, $lng1, $lat2, $lng2, $len_type = 1, $decimal = 2)
    {
        $pi = 3.1415926000000001;
        $er = 6378.1369999999997;
        $radLat1 = $lat1 * $pi / 180;
        $radLat2 = $lat2 * $pi / 180;
        $a = $radLat1 - $radLat2;
        $b = $lng1 * $pi / 180 - $lng2 * $pi / 180;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $s = $s * $er;
        $s = round($s * 1000);

        if (1 < $len_type) {
            $s /= 1000;
        }

        return round($s, $decimal);
    }
计算距离 函数
    public function Convert_GCJ02_To_BD09($lat, $lng)
    {
        $x_pi = 3.1415926535897931 * 3000 / 180;
        $x = $lng;
        $y = $lat;
        $z = sqrt($x * $x + $y * $y) - 2.0000000000000002E-5 * sin($y * $x_pi);
        $theta = atan2($y, $x) - 3.0000000000000001E-6 * cos($x * $x_pi);
        $data['lng'] = $z * cos($theta) + 0.0064999999999999997;
        $data['lat'] = $z * sin($theta) + 0.0060000000000000001;
//         var_dump($data);
        return $data;
    }
    public function addresstolatlag($address){
        $url='http://restapi.amap.com/v3/geocode/geo?address='.$address.'&key=48e598c5f71f20d2cd5a18d6e3142d27';
        // var_dump(file_get_contents($url));exit;
        if($result=file_get_contents($url))
        {
            $result = json_decode($result,true);
            //判断是否成功
            if(!empty($result['count'])){
                return  explode(',',$result['geocodes']['0']['location']);
    
            }else{
                return false;
            }
        }
    }

   
校正坐标函数 webapi函数

说明下 由于 客户给的地址 比较乱 只有详细地址 没有坐标
这边利用高德提供的webapi转换了坐标 之后计算的距离

猜你喜欢

转载自www.cnblogs.com/liiu/p/10175086.html