1 百度坐标转火星坐标
/**
* 百度坐标转火星坐标
* @param [string] $bcoordinate [百度坐标系]
* @return [string] $newcoordinate [火星坐标系]
*/
function baiToMars($bcoordinate)
{
$link = "https://api.map.baidu.com/geoconv/v1/?";
$options = array("coords"=>$coor,"from"=>5,"to"=>3,"ak"=>"XXXX");
$link .= http_build_query($options,'','&');
$dir_arr = file_get_contents($link);
$dir_arr = json_decode($dir_arr,true);
$newcoordinate = $dir_arr['result'][0]['x'].",".$dir_arr['result'][0]['y'];
return $newcoordinate;
}
2 百度坐标转腾讯坐标
方式1
//百度转腾讯坐标转换 $a = Latitude , $b = Longitude
protected function coordinate_switch($a,$b){
$x = (double)$b - 0.0065;
$y = (double)$a - 0.006;
$x_pi = 3.14159265358979324*3000/180;
$z = sqrt($x * $x+$y * $y) - 0.00002 * sin($y * $x_pi);
$theta = atan2($y,$x) - 0.000003 * cos($x*$x_pi);
$gb = number_format($z * cos($theta),15);
$ga = number_format($z * sin($theta),15);
return ['Latitude'=>$ga,'Longitude'=>$gb];
}
注意:最开始使用的方式一,但是最后得出的坐标不是太准确,因此,建议使用第二种方式来获取。
方式2 使用腾讯 WebService API
/**
* 百度坐标转腾讯坐标
* @param [string] $bcoordinate [百度坐标系]
* @return [string] $qcoordinate [腾讯坐标系]
*/
function baiToMars($bcoordinate)
{
$coordinate = explode(",",$bcoordinate);
$coor = $coordinate[1].",".$coordinate[0];
$link = "https://apis.map.qq.com/ws/coord/v1/translate?locations=".$coor."&type=3&key=XXXXXX";
$dir_arr = file_get_contents($link);
$dir_arr = json_decode($dir_arr,true);
$qcoordinate = $dir_arr['locations'][0]['lng'].",".$dir_arr['locations'][0]['lat']
return $qcoordinate;
}
3 计算两个坐标之间的距离
注意:建议使用腾讯坐标进行计算,相对比较准确 。
/**
* 计算两点地理坐标之间的距离
* @param Decimal $longitude1 起点经度
* @param Decimal $latitude1 起点纬度
* @param Decimal $longitude2 终点经度
* @param Decimal $latitude2 终点纬度
* @param Int $unit 单位 1:米 2:公里
* @param Int $decimal 精度 保留小数位数
* @return Decimal
*/
protected function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit=2, $decimal=2){
$EARTH_RADIUS = 6370.996; // 地球半径系数
$PI = 3.1415926;
$radLat1 = $latitude1 * $PI / 180.0;
$radLat2 = $latitude2 * $PI / 180.0;
$radLng1 = $longitude1 * $PI / 180.0;
$radLng2 = $longitude2 * $PI /180.0;
$a = $radLat1 - $radLat2;
$b = $radLng1 - $radLng2;
$distance = 2 * asin(sqrt(pow(sin($a/2),2) + cos($radLat1) * cos($radLat2) * pow(sin($b/2),2)));
$distance = $distance * $EARTH_RADIUS * 1000;
if($unit==2){
$distance = $distance / 1000;
}
return round($distance, $decimal);
}
文章评论