php 根据具体位置获取经纬度 根据经纬度计算位置距离 百度地图api

直接代码了。很好理解。
/**
     * 根据经纬度计算距离 
     *
     */
    public function getDistance($lat1=32.052639218124, $lng1=118.78637828936, $lat2=31.906570901919, $lng2=118.91701861624) 
    { 
      $earthRadius = 6367000; //approximate radius of earth in meters lat1纬度 经度118.91701861624
       // print_r($lat2);
       // echo "<br>";
       // print_r($lng1);
       // echo "<br>";
      $lat1 = ($lat1 * pi() ) / 180; 
      $lng1 = ($lng1 * pi() ) / 180; 
       
      $lat2 = ($lat2 * pi() ) / 180; 
      $lng2 = ($lng2 * pi() ) / 180; 

       
      $calcLongitude = $lng2 - $lng1; 
      $calcLatitude = $lat2 - $lat1; 
      $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2); 
      $stepTwo = 2 * asin(min(1, sqrt($stepOne))); 
      // print_r($stepTwo);
      $calculatedDistance = $earthRadius * $stepTwo; 
    // echo "<br>";
    //    print_r($calculatedDistance);
      return round($calculatedDistance); 
    }

/**
     * 根据地址获取经纬度  
     *
     */
    public function getLatng($prepAddr)
    {
        // $prepAddr = "上海徐汇区漕宝70号";    
               
        $geocode=file_get_contents("http://api.map.baidu.com/geocoder/v2/?address=$prepAddr&output=json&ak=自己的ak");
        $output= json_decode($geocode,true);    
        // print_r($output);die;    
        $latng = $output['result']['location'];    
        
        return $latng;
    }


  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下代码进行高德经纬度距离计算和驾驶距离计算: ```php //高德地图api获取两地经纬度 $origin = "北京市朝阳区阜通东大街6号"; $destination = "北京市海淀区中关村南大街甲2号"; $origin_url = "https://restapi.amap.com/v3/geocode/geo?key=YOUR_KEY&address=".urlencode($origin); $destination_url = "https://restapi.amap.com/v3/geocode/geo?key=YOUR_KEY&address=".urlencode($destination); $origin_json = file_get_contents($origin_url); $origin_data = json_decode($origin_json,true); $origin_location = explode(",",$origin_data['geocodes'][0]['location']); $destination_json = file_get_contents($destination_url); $destination_data = json_decode($destination_json,true); $destination_location = explode(",",$destination_data['geocodes'][0]['location']); $origin_lng = $origin_location[0]; $origin_lat = $origin_location[1]; $destination_lng = $destination_location[0]; $destination_lat = $destination_location[1]; //高德经纬度距离计算 $distance_url = "https://restapi.amap.com/v3/distance?key=YOUR_KEY&origins={$origin_lng},{$origin_lat}&destination={$destination_lng},{$destination_lat}&type=1"; $distance_json = file_get_contents($distance_url); $distance_data = json_decode($distance_json,true); $distance = $distance_data['results'][0]['distance']; //高德地图api计算驾驶距离 $driving_url = "https://restapi.amap.com/v3/direction/driving?key=YOUR_KEY&origin={$origin_lng},{$origin_lat}&destination={$destination_lng},{$destination_lat}"; $driving_json = file_get_contents($driving_url); $driving_data = json_decode($driving_json,true); $duration = $driving_data['route']['paths'][0]['duration']; $distance_driving = $driving_data['route']['paths'][0]['distance']; ``` 请将上述代码中的 `YOUR_KEY` 替换为你的高德地图 API 密钥。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值