接口地址:http://api.map.baidu.com/routematrix/v2/(driving/riding/walking/walking) |
---|
返回格式:json/xml |
请求方式:get |
请求示例:http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=&destinations=ak=youak |
名称 | 类型 | 必填 | 说明 |
---|---|---|---|
ak | string | 必填 | 用户的AK 扫码关注公众号 |
origins | string | 必填 | 起点坐标串 |
destinations | string | 必填 | 终点坐标串 |
tactics | string | 选填 | 算路偏好,该参数只对驾车算路(driving)生效 |
gps_direction | string | 选填 | 起点的车头方向,取与正北方向顺时针夹角,取值范围:0-359 |
radius | float | 选填 | 起点的定位精度,由GPS或定位SDK返回,配合gps_direction字段使用 |
speed | float | 选填 | 起点车辆的行驶速度,该字段只对驾车算路(driving)生效。配合gps_direction字段使用,当speed>1.5米/秒且gps_direction存在时,采用gps_direction的方向 |
output | string | 选填 | 表示输出类型,可设置为xml或json |
coord_type | string | 选填 | 坐标类型,可选值为:bd09ll(百度经纬度坐标)、bd09mc(百度墨卡托坐标)、gcj02(国测局加密坐标)、wgs84(gps设备获取的坐标) |
名称 | 类型 | 说明 |
---|---|---|
status | int | 状态码 |
message | string | 返回信息 |
result | array | 返回的结果 |
distance | object | 路线距离 |
duration | object | 路线耗时 |
{
"status": 0,
"result": [{
"distance": {
"text": "18.8公里",
"value": 18827
},
"duration": {
"text": "12分钟",
"value": 705
}
}, {
"distance": {
"text": "20.6公里",
"value": 20615
},
"duration": {
"text": "13分钟",
"value": 773
}
}, {
"distance": {
"text": "44.7公里",
"value": 44742
},
"duration": {
"text": "28分钟",
"value": 1677
}
}, {
"distance": {
"text": "46.5公里",
"value": 46530
},
"duration": {
"text": "29分钟",
"value": 1744
}
}],
"message": "成功"
}
错误码 | 说明 |
---|---|
0 | 服务请求正常召回 |
1 | 服务器内部错误 |
2 | 请求参数非法 |
3 | 权限校验失败 |
4 | 配额校验失败 |
5 | ak不存在或者非法 |
102 | 不通过白名单或者安全码不对 |
2xx | 无权限 |
3xx | 配额错误 |
<?php
/**
* Created by PhpStorm.
* User: FZS
* Time: 2019/9/3 23:10
*/
//----------------------------------
// 批量算路 调用类
//----------------------------------
class freeApi{
private $ak;
private $apiUrl;
public function __construct($ak){
$this->ak = $ak;
$this->apiUrl = 'http://api.map.baidu.com/routematrix/v2/driving?output=json&origins=40.45,116.34|40.54,116.35&destinations=40.34,116.45|40.35,116.46&ak='.$this->ak;
}
/**
* 获取结果
* @return array
*/
public function getResult(){
return $this->freeApiCurl($this->apiUrl);
}
/**
* 请求接口返回内容
* @param string $url [请求的URL地址]
* @param string $params [请求的参数]
* @param int $ipost [是否采用POST形式]
* @return string
*/
public function freeApiCurl($url,$params=false,$ispost=0){
$ch = curl_init();
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
curl_setopt( $ch, CURLOPT_USERAGENT , 'free-api' );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 60 );
curl_setopt( $ch, CURLOPT_TIMEOUT , 60);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
if( $ispost )
{
curl_setopt( $ch , CURLOPT_POST , true );
curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
curl_setopt( $ch , CURLOPT_URL , $url );
}
else
{
if($params){
curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
}else{
curl_setopt( $ch , CURLOPT_URL , $url);
}
}
$response = curl_exec( $ch );
if ($response === FALSE) {
return false;
}
curl_close( $ch );
return $response;
}
}