高德经纬度距离计算php,高德地图api获取两地经纬度并计算驾驶距离
时间: 2023-11-06 22:07:21 浏览: 158
可以使用以下代码进行高德经纬度距离计算和驾驶距离计算:
```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 密钥。
阅读全文