zoukankan      html  css  js  c++  java
  • thinkphp 百度地图Api坐标计算 A坐标距离B坐标多少公里 并按照距离近的排序 坐标排序 外部字段排序

    感谢我磊哥

    函数封装方法:

    //计算距离
    /*
    **$a 可多数坐标 就是可数组类型的
    ***$b 是登录者的坐标
    ***ps: lat经度  lng纬度  经度在前纬度在后
    ***
    ***/
    function juli($a, $b)
    {
    //    $key[0] = '3uF44dvwWrW7S9GLgBPk3CVh';
    //    $key[1] = 'G0APbmvaqwQTqqNVCcRk6gtOlT1DW9r3';
    //    $kk = $key[rand(0,1)];
        $juli_api = 'http://api.map.baidu.com/routematrix/v2/riding?output=json&origins=' . $a . '&destinations=' . $b . '&ak=' . 'G0APbmvaqwQTqqNVCcRk6gtOlT1DW9r3';
    //    dump($juli_api);exit;
        return os_get($juli_api);
    }
    
    
    
    /**
     * 发送get请求
     * @param url $url 需要get的地址
     * @return json $data 返回数据
     * @author 5heAtMin9 <sheatming@foxmail.com>
     */
    function os_get($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        curl_close($curl);
        return $data;
    }

    控制器里调用方法:

    $dingdan = M('ying_dingdan')->where($where)->page($p,10)->order('time DESC')->select();
    $zuobiao = array();
            foreach($dingdan as $k=>$r){
                $member = M('ying_member')->where(array('uid'=>$r['uid']))->field('logo,name,lng,lat')->find();/*找出发布者的lat lng*/
                $zuobiao[$k] = $member['lat'].','.$member['lng'];
            }
            
            /*根据登陆者的经度纬度 算出与发布者的距离*/
            if($post['lat'] != '' && $post['lng'] != ''){
                $a = implode('|',$zuobiao);
                $b = $post['lat'] .','. $post['lng'];
                $c = juli($a,$b);
                $d = json_decode($c,true);
                foreach($dingdan as $k=>$r){
                    if(!$post['lat']){
                        $dingdan[$k]['juli'] = '';
                    }else{
                        $dingdan[$k]['juli'] = round(($d['result'][$k]['distance']['value'] / 1000),1);
                    }
                }
    $dingdan = M('ying_dingdan')->where($where)->page($p,10)->order('time DESC')->select();
    $zuobiao = array();
            foreach($dingdan as $k=>$r){
                $member = M('ying_member')->where(array('uid'=>$r['uid']))->field('logo,name,lng,lat')->find();/*找出发布者的lat lng*/
                $zuobiao[$k] = $member['lat'].','.$member['lng'];
            }
            
            /*根据登陆者的经度纬度 算出与发布者的距离*/
            if($post['lat'] != '' && $post['lng'] != ''){
                $a = implode('|',$zuobiao);
                $b = $post['lat'] .','. $post['lng'];
                $c = juli($a,$b);
                $d = json_decode($c,true);
                foreach($dingdan as $k=>$r){
                    if(!$post['lat']){
                        $dingdan[$k]['juli'] = '';
                    }else{
                        $dingdan[$k]['juli'] = round(($d['result'][$k]['distance']['value'] / 1000),1);
                    }
                }
                $orderFile = array();
                foreach($dingdan as $k=>$r){
                   $orderFile[]=$r['juli'];
                }
                array_multisort($orderFile ,SORT_ASC, $dingdan );/*按照距离排序*/
    
            }     
  • 相关阅读:
    [na]ip数据包格式
    [js]浏览器同源策略(same-origin policy)
    [sql] 同库表(结构)的备份和sql聚合&navicat使用
    [svc]tcp三次握手四次挥手&tcp的11种状态(半连接)&tcp的time-wait
    [svc]ip地址划分
    [css]单/多行居中&字体设置
    时间戳转为C#格式时间
    windows 8 中 使用 httpclient
    oralce 查看是否启动 登陆 创建用户 常用命令小记
    SQL递归查询(with cte as)
  • 原文地址:https://www.cnblogs.com/zc290987034/p/8032686.html
Copyright © 2011-2022 走看看