驾车路线规划
百度地图 官方文档
根据起终点坐标检索符合条件的驾车路线规划方案,支持以下功能:1. 支持一次请求返回多条路线(备用路线)2. 支持20个以内的途径点3. 支持传入车牌规避限行路段4. 支持传入起点车头方向,辅助判断起点所在正逆向车道,辅助更准确算路5. 支持未来出行规划,指定未来7天任意出发时刻,将依据智能预测路况和道路限行规划合理路线
基本说明:
接口地址:http://api.map.baidu.com/direction/v2/driving
返回格式:json/xml
请求方式:get
请求示例:http://api.map.baidu.com/direction/v2/driving?origin=40.01,116.339&destination=39.93,116.4&ak=您的AK
请求参数说明:
名称 类型 必填 说明
ak string 必填 用户的访问权限,AK申请 扫码关注公众号
origin string 必填 起点经纬度,小数点后不超过6位
destination string 必填 终点经纬度,小数点后不超过6位
origin_uid string 选填 POI 的 uid(在已知起点POI 的 uid 情况下,请尽量填写uid,将提升路线规划的准确性)
destination_uid string 选填 POI 的 uid(在已知终点POI 的 uid 情况下,请尽量填写uid,将提升路线规划的准确性)
waypoints string 选填 途径点坐标串,支持20个以内的有序途径点。多个途径点坐标按顺序以英文竖线符号分隔
coord_type string 选填 坐标类型,可选参数,默认为bd09ll。允许的值为:bd09ll(百度经纬度坐标)、bd09mc(百度墨卡托坐标)、gcj02(国测局加密坐标)、wgs84(gps设备获取的坐标)
ret_coordtype string 选填 返回结果坐标类型,默认为bd09ll,允许的值为:bd09ll(百度经纬度坐标)、gcj02(国测局加密坐标)
tactics string 选填 0:默认3:不走高速4:高速优先5:躲避拥堵6:少收费7:躲避拥堵&高速优先8:躲避拥堵&不走高速9:躲避拥堵&少收费10:躲避拥堵&不走高速&少收费11:不走高速&少收费
alternatives string 选填 是否返回备选路线
plate_number string 选填 车牌号
departure_time string 选填 设置出发时间(支持未来7天)UNIX时间戳
ext_departure_time string 选填 更多出发时间,UNIX时间戳
gps_direction int 选填 起点的车头方向
radius float 选填 起点的定位精度
speed float 选填 起点车辆的行驶速度
sn string 选填 若用户所用ak的校验方式为sn校验时该参数必须
output string 选填 输出格式为json或者xml
callback string 选填 将json格式的返回值通过callback函数返回以实现jsonp功能
返回参数说明:
名称 类型 说明
见json返回示例 - -
JSON返回示例:
{
	"status": 0,
	"message": "成功",
	"type": 2,
	"result": {
		"restriction": "",
		"total": 1,
		"routes": [{
			"origin": {
				"lng": 116.339646,
				"lat": 40.010519
			},
			"destination": {
				"lng": 116.452389,
				"lat": 39.936406
			},
			"tag": "常规路线",
			"distance": 18616,
			"duration": 1699,
			"taxi_fee": 63,
			"toll": 0,
			"toll_distance": 0,
			"steps": [{
				"leg_index": 0,
				"road_name": "无名路",
				"direction": 6,
				"distance": 541.64,
				"road_type": 6,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 0,
					"geo_cnt": 12,
					"distance": 541.64
				}],
				"path": "116.339646,40.010519;116.339676,40.010169;116.339706,40.009359;116.339336,40.009351;116.339346,40.009271;116.339436,40.008411;116.339437,40.008291;116.338705,40.008266;116.338344,40.008268;116.338364,40.008018;116.338384,40.007878;116.338415,40.006998;116.338435,40.006748",
				"start_location": {
					"lng": 116.339646,
					"lat": 40.010519
				},
				"end_location": {
					"lng": 116.338435,
					"lat": 40.006748
				}
			}, {
				"leg_index": 0,
				"road_name": "清华路",
				"direction": 9,
				"distance": 143.48,
				"road_type": 6,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 0,
					"geo_cnt": 3,
					"distance": 143.48
				}],
				"path": "116.338435,40.006748;116.338355,40.006739;116.337392,40.006726;116.336750,40.006702",
				"start_location": {
					"lng": 116.338435,
					"lat": 40.006748
				},
				"end_location": {
					"lng": 116.33675,
					"lat": 40.006702
				}
			}, {
				"leg_index": 0,
				"road_name": "东直门南大街",
				"direction": 6,
				"distance": 427.54,
				"road_type": 3,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 1,
					"geo_cnt": 14,
					"distance": 427.54
				}],
				"path": "116.440176,39.943837;116.440196,39.943417;116.440215,39.942927;116.440215,39.942817;116.440225,39.942636;116.440225,39.942576;116.440235,39.941986;116.440275,39.941655;116.440205,39.941627;116.440165,39.941547;116.440165,39.940997;116.440175,39.940797;116.440195,39.940427;116.440184,39.940197;116.440164,39.940037",
				"start_location": {
					"lng": 116.440176,
					"lat": 39.943837
				},
				"end_location": {
					"lng": 116.440164,
					"lat": 39.940037
				}
			}, {
				"leg_index": 0,
				"road_name": "东四十条桥",
				"direction": 8,
				"distance": 222.65,
				"road_type": 3,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 1,
					"geo_cnt": 15,
					"distance": 222.65
				}],
				"path": "116.440164,39.940037;116.440134,39.940018;116.439893,39.939822;116.439872,39.939742;116.439883,39.939552;116.439983,39.939360;116.440214,39.939226;116.440255,39.939205;116.440345,39.939194;116.440386,39.939193;116.440879,39.939224;116.440919,39.939234;116.441040,39.939242;116.441160,39.939299;116.441341,39.939466;116.441362,39.939496",
				"start_location": {
					"lng": 116.440164,
					"lat": 39.940037
				},
				"end_location": {
					"lng": 116.441362,
					"lat": 39.939496
				}
			}, {
				"leg_index": 0,
				"road_name": "工人体育场北路",
				"direction": 3,
				"distance": 630.89,
				"road_type": 4,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 1,
					"geo_cnt": 11,
					"distance": 630.89
				}],
				"path": "116.441362,39.939496;116.441663,39.939551;116.442036,39.939554;116.442186,39.939551;116.442277,39.939550;116.442508,39.939546;116.442629,39.939544;116.443263,39.939533;116.444309,39.939545;116.444953,39.939535;116.446380,39.939512;116.448783,39.939465",
				"start_location": {
					"lng": 116.441362,
					"lat": 39.939496
				},
				"end_location": {
					"lng": 116.448783,
					"lat": 39.939465
				}
			}, {
				"leg_index": 0,
				"road_name": "无名路",
				"direction": 6,
				"distance": 7.79,
				"road_type": 6,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 1,
					"geo_cnt": 1,
					"distance": 7.79
				}],
				"path": "116.448783,39.939465;116.448783,39.939395",
				"start_location": {
					"lng": 116.448783,
					"lat": 39.939465
				},
				"end_location": {
					"lng": 116.448783,
					"lat": 39.939395
				}
			}, {
				"leg_index": 0,
				"road_name": "工人体育场北路辅路",
				"direction": 3,
				"distance": 134.88,
				"road_type": 5,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 1,
					"geo_cnt": 2,
					"distance": 134.88
				}],
				"path": "116.448783,39.939395;116.449265,39.939378;116.450371,39.939362",
				"start_location": {
					"lng": 116.448783,
					"lat": 39.939395
				},
				"end_location": {
					"lng": 116.450371,
					"lat": 39.939362
				}
			}, {
				"leg_index": 0,
				"road_name": "工人体育场西路",
				"direction": 6,
				"distance": 297.23,
				"road_type": 5,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 1,
					"geo_cnt": 5,
					"distance": 297.23
				}],
				"path": "116.450371,39.939362;116.450371,39.939182;116.450361,39.938783;116.450360,39.937963;116.450360,39.936773;116.450360,39.936693",
				"start_location": {
					"lng": 116.450371,
					"lat": 39.939362
				},
				"end_location": {
					"lng": 116.45036,
					"lat": 39.936693
				}
			}, {
				"leg_index": 0,
				"road_name": "无名路",
				"direction": 3,
				"distance": 190.93,
				"road_type": 6,
				"toll": 0,
				"toll_distance": 0,
				"traffic_condition": [{
					"status": 1,
					"geo_cnt": 1,
					"distance": 5.12
				}, {
					"status": 0,
					"geo_cnt": 7,
					"distance": 185.81
				}],
				"path": "116.450360,39.936693;116.450421,39.936692;116.450642,39.936659;116.450973,39.936634;116.451104,39.936633;116.451526,39.936617;116.451897,39.936592;116.452349,39.936597;116.452389,39.936406",
				"start_location": {
					"lng": 116.45036,
					"lat": 39.936693
				},
				"end_location": {
					"lng": 116.452389,
					"lat": 39.936406
				}
			}]
		}]
	}
}
服务级错误码参照
错误码 说明
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/direction/v2/driving?origin=40.01116,116.339303&destination=39.936404,116.452562&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;
    }
}