PHP 坐标转换【百度转火星,百度转腾讯】

 1 百度坐标转火星坐标 

坐标转换 | 百度地图API SDK

/**
 * 百度坐标转火星坐标
 * @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

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);
}

总结  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值