thinkphp 百度地图Api坐标计算 A坐标距离B坐标多少公里 并按照距离近的排序 坐标排序 外部字段排序

时间:2024-09-25 18:06:26

感谢我磊哥

函数封装方法:

//计算距离
/*
**$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 );/*按照距离排序*/ }