php 算法 计算两地之间的距离

public function geo(Request $request){
     //$city地点1
     //$goodsCity地点2
        $city=$request->input('city');
        $goodsCity=$request->input('goodsCity');
     //地址转为为经纬度
      //腾讯位置服务
        $url='http://apis.map.qq.com/ws/geocoder/v1/?address='.$city.'&key=ZSYBZ-RF6RK-RONJP-AZYJM-BQBKQ-4OB3J';
        $result=file_get_contents($url);
        $data = json_decode($result,TRUE);
//        var_dump($data['result']['location']['lng']);
//        var_dump($data['result']['location']['lat']);
        $url='http://apis.map.qq.com/ws/geocoder/v1/?address='.$goodsCity.'&key=你的key值';
        $result=file_get_contents($url);
        $res = json_decode($result,TRUE);
//        var_dump($res['result']['location']['lng']);
//        var_dump($res['result']['location']['lat']);
     $form=[$data['result']['location']['lat'],$data['result']['location']['lng']];
     $to=[$res['result']['location']['lat'],$res['result']['location']['lng']];
    //调用算法
     $data= $this->get_distance($form,$to);
//       echo $data.'km';
        return ['code'=>200,'data'=>'两地之间的距离为'.$data.'km','msg'=>'查询成功'];
    }
   //计算的算法
    function get_distance($from,$to,$km=true,$decimal=2){
        sort($from);
        sort($to);
        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $distance = $EARTH_RADIUS*2*asin(sqrt(pow(sin( ($from[0]*pi()/180-$to[0]*pi()/180)/2),2)+cos($from[0]*pi()/180)*cos($to[0]*pi()/180)* pow(sin( ($from[1]*pi()/180-$to[1]*pi()/180)/2),2)))*1000;
        if($km){
            $distance = $distance / 1000;
        }
        return round($distance, $decimal);
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值