一:需要接口
/*
* 腾讯地图计算距离
* $from:起点
* $to : 终点
* $key:key
* */
function TX_Map_Api_distance($form,$to){
$url = "http://apis.map.qq.com/ws/distance/v1/?mode=driving&from=".$form."&to=".$to."&key=".$key;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
$output = json_decode($output,true);
if($output['status']==0){
return $output['result']['elements'][0]['distance'];
}else{
return '位置出错';
}
}
二,直接计算
/*
* 地图计算距离
* $lat1:起点纬度
* $lng1 : 起点经度
* $lat2:终点纬度
* $lng2 : 终点经度
* */
function TX_Map_Api_distance($lat1,$lng1,$lat2,$lng2){
// 将角度转为狐度
$radLat1 = deg2rad($lat1);// deg2rad()函数将角度转换为弧度
$radLat2 = deg2rad($lat2);
$radLng1 = deg2rad($lng1);
$radLng2 = deg2rad($lng2);
$a = $radLat1 - $radLat2;
$b = $radLng1 - $radLng2;
$s = 2 * asin(sqrt(pow(sin($a / 2), 2)+cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;
return $s;
}