公交路线规划
百度地图 官方文档
根据起点和终点检索符合条件的公共交通方案,融入出行策略(少换乘、地铁优先等)支持大陆区域的同城及跨城路线规划,交通方式支持公交、地铁、火车、飞机、大巴
基本说明:
接口地址:http://api.map.baidu.com/direction/v2/transit
返回格式:json/xml
请求方式:get
请求示例:http://api.map.baidu.com/direction/v2/transit?origin=40.05,116.30&destination=31.222,121.50&ak=您的AK
请求参数说明:
名称 类型 必填 说明
origin string 必填 起点,"double
destination string 必填 终点
coord_type string 选填 起终点的坐标类型
tactics_intercity int 选填 跨城公交换乘策略,0 时间短,1 出发早,2 价格低
trans_type_intercity int 选填 跨城交通方式策略,0 火车优先,1 飞机优先,2 大巴优先
ret_coordtype string 选填 返回值的坐标类型
output string 选填 输出类型
page_size int 选填 返回每页几条路线
page_index int 选填 返回第几页
ak string 必填 用户申请注册的key,自v2开始参数修改为“ak”,之前版本参数为“key”申请ak 扫码关注公众号
sn string 选填 若用户所用ak的校验方式为sn校验时该参数必须
callback string 选填 将json格式的返回值通过callback函数返回以实现jsonp功能
timestamp string 选填 时间戳
返回参数说明:
名称 类型 说明
见json返回示例 - -
JSON返回示例:
{
	"status": 0,
	"type": 2,
	"result": {
		"origin": {
			"city_id": 131,
			"city_name": "北京市",
			"location": {
				"lng": 116.3081418168,
				"lat": 40.056871561538
			}
		},
		"destination": {
			"city_id": 289,
			"city_name": "上海市",
			"location": {
				"lng": 121.50581834502,
				"lat": 31.222960154925
			}
		},
		"total": 66,
		"routes": [{
			"distance": 32975,
			"duration": 22661,
			"arrive_time": "2019-09-04 23:40:00",
			"price": 558,
			"price_detail": [],
			"steps": [
				[{
					"distance": 792,
					"duration": 675,
					"instructions": "步行792米",
					"path": "116.30825859651,40.056899174073;116.30812385069,40.057182201905;116.30803402013,40.057410003934;116.30786334209,40.057768963149;116.30771961321,40.058045084326;116.30771063015,40.058072696381;116.30767469793,40.058176241489;116.30772859626,40.058196950492;116.30774656237,40.05821075649;116.30774656237,40.058238368478;116.30771961321,40.058307398398;116.30771063015,40.058348816317;116.30768368099,40.058362622284;116.30765673182,40.058376428248;116.30762978266,40.058376428248;116.30727046045,40.05830049541;116.30728842656,40.05839713719;116.30733334184,40.058486875862;116.30734232489,40.058555905529;116.30799808791,40.058693964651;116.30875266454,40.058859635225;116.30953419034,40.059052917048;116.31086368249,40.059356644512;116.31104334359,40.059398061787;116.31136673358,40.059467090524;116.3114026658,40.059473993393;116.31142961496,40.059480896262;116.31151046246,40.059494701998;116.31156436079,40.059501604865;116.31160029301,40.059501604865;116.31163622523,40.059487799131;116.31168114051,40.059508507732;116.31174402189,40.059536119189;116.31182486939,40.05939115891;116.31189673383,40.059260004103;116.31188775077,40.059260004103;116.31214825937,40.05939115891;116.31214825937,40.05939115891;116.31226503909,40.05942567329;116.31226503909,40.05942567329;116.31261537824,40.058907955733;116.31265131046,40.058921761586",
					"traffic_condition": [],
					"start_location": {
						"lng": 116.3081418168,
						"lat": 40.056871561538
					},
					"end_location": {
						"lng": 116.31266029351,
						"lat": 40.058921761586
					},
					"vehicle_info": {
						"type": 5,
						"detail": null
					}
				}, {
					"distance": 408,
					"duration": 342,
					"instructions": "步行408米",
					"path": "116.38514456511,39.871178412708;116.38513558206,39.871164568587;116.38518948039,39.870707711024;116.38518948039,39.870707711024;116.38580931119,39.870728477343;116.38580931119,39.870728477343;116.3858272773,39.870555424488;116.3858272773,39.870534658116;116.3858272773,39.870534658116;116.38678846419,39.869911664003;116.38678846419,39.869904741814;116.3865279556,39.869766297889;116.38553981954,39.869316353188",
					"traffic_condition": [],
					"start_location": {
						"lng": 116.38515354817,
						"lat": 39.871178412708
					},
					"end_location": {
						"lng": 116.3850277854,
						"lat": 39.870437748295
					},
					"vehicle_info": {
						"type": 5,
						"detail": null
					}
				}],
				[{
					"distance": 0,
					"duration": 16560,
					"instructions": "北京南站乘坐G9到达上海虹桥站 ",
					"path": "116.38548592121,39.871275321474;117.06727285411,39.062754661525;116.89802311298,36.67526195637;118.80433520181,31.975021252677;121.32699266715,31.200545723645",
					"traffic_condition": [],
					"start_location": {
						"lng": 116.38548592121,
						"lat": 39.871275321474
					},
					"end_location": {
						"lng": 121.32699266715,
						"lat": 31.200545723645
					},
					"vehicle_info": {
						"type": 1,
						"detail": {
							"name": "G9",
							"price": 553,
							"departure_station": "北京南站",
							"arrive_station": "上海虹桥站",
							"departure_time": "012:00",
							"arrive_time": "016:36",
							"start_info": {
								"start_name": "北京南站",
								"start_city": "北京",
								"start_time": "2019-09-04 12:00:00"
							},
							"end_info": {
								"end_name": "上海虹桥站",
								"end_city": "上海",
								"end_time": "2019-09-04 16:36:00"
							},
							"booking": "12306"
						}
					}
				}],
				[{
					"distance": 220,
					"duration": 182,
					"instructions": "步行220米",
					"path": "121.32722622659,31.201835297277;121.32712741298,31.201835297277;121.32712741298,31.201356535485;121.32728012492,31.200630666166;121.32728012492,31.200630666166;121.32728012492,31.200584333891;121.32728012492,31.200298617688;121.32728910797,31.200298617688;121.32788198961,31.200329505968;121.32790893877,31.200267729398",
					"traffic_condition": [],
					"start_location": {
						"lng": 121.32720826048,
						"lat": 31.199935679633
					},
					"end_location": {
						"lng": 121.32790893877,
						"lat": 31.200275451471
					},
					"vehicle_info": {
						"type": 5,
						"detail": null
					}
				}, {
					"distance": 16549,
					"duration": 1740,
					"instructions": "虹桥火车站(出口)乘地铁10号线(新江湾城方向)经过13站到老西门站",
					"path": "121.32790893877,31.200275451471;121.32920249871,31.200321783899;121.33134046582,31.200260007324;121.33272385631,31.200236841097;121.33272385631,31.200236841097;121.33347843294,31.200244563173;121.33752080773,31.200198230707;121.33792504521,31.200159620301;121.34975572878,31.197935634062;121.35230691642,31.197588131443;121.35368132385,31.197603576031;121.35368132385,31.197603576031;121.35445386659,31.197619020616;121.35673556259,31.19749546386;121.36282607395,31.196846788214;121.36522454966,31.196676896231;121.36952743305,31.196383445717;121.37146777295,31.196128606367;121.3744591303,31.195981880366;121.37489031695,31.19596643551;121.37489031695,31.19596643551;121.37531252053,31.195950990652;121.37710913155,31.195943268222;121.3786003187,31.196174940846;121.3808011672,31.196877677632;121.38246303239,31.197595853737;121.38383743982,31.198306302101;121.38673896662,31.200043788988;121.38673896662,31.200043788988;121.39124846028,31.20271559483;121.3953267673,31.20387386848;121.3970065986,31.204491608569;121.39844388742,31.205070736203;121.39844388742,31.205070736203;121.4020281264,31.206530121959;121.40273778775,31.206645945251;121.40354626271,31.206599615951;121.40594473843,31.206082270547;121.41066982541,31.204630599528;121.41066982541,31.204630599528;121.41265508058,31.20402058212;121.41402050496,31.203557275105;121.41640101456,31.202846866563;121.41802694753,31.202537991604;121.41844915112,31.202507104052;121.41844915112,31.202507104052;121.4188533886,31.202460772705;121.42462949303,31.202167340308;121.42653390071,31.202182784141;121.42749508761,31.20229861281;121.42875271532,31.202846866563;121.42875271532,31.202846866563;121.42948034279,31.203186627842;121.430657123,31.203765763545;121.43191475072,31.204437556474;121.43440305698,31.205572643923;121.44008933086,31.207974042097;121.44055644972,31.208197963547;121.44055644972,31.208197963547;121.44293695933,31.20943338261;121.45077018337,31.21443666314;121.45077018337,31.21443666314;121.45157865833,31.214930799797;121.45481255817,31.216289662156;121.458208153,31.217679387349;121.46147798505,31.219146297115;121.46313086719,31.220258044994;121.46404713881,31.220783032457;121.46447832546,31.22098376218;121.46447832546,31.22098376218;121.46618510592,31.22170947374;121.46676900451,31.221856159435;121.4671912081,31.221925642053;121.46828714082,31.221995124619;121.47186239675,31.22208776796;121.47409019441,31.2222267328;121.47465612688,31.2222267328;121.47526697463,31.222134089596;121.47628205986,31.221794397065;121.47757561979,31.221531906538;121.47833917947,31.221439262646;121.4794351122,31.221493304928;121.48036036687,31.221755795563;121.48180663874,31.222242173325;121.48180663874,31.222242173325;121.48309121562,31.222674506991;121.48579511521,31.223516007865;121.4890379981,31.22458138344;121.4894781678,31.224674024221;121.49037647331,31.224859305508",
					"traffic_condition": [],
					"start_location": {
						"lng": 121.32790893877,
						"lat": 31.200275451471
					},
					"end_location": {
						"lng": 121.49037647331,
						"lat": 31.224859305508
					},
					"vehicle_info": {
						"type": 3,
						"detail": {
							"name": "地铁10号线",
							"type": 1,
							"stop_num": 13,
							"on_station": "虹桥火车站(出口)",
							"off_station": "老西门站",
							"first_time": "05:55",
							"last_time": "22:30"
						}
					}
				}, {
					"distance": 104,
					"duration": 86,
					"instructions": "站内换乘 步行104米",
					"path": "121.49036749025,31.224851585462;121.49037647331,31.224836145367;121.48946918474,31.224658584097",
					"traffic_condition": [],
					"start_location": {
						"lng": 121.49037647331,
						"lat": 31.224859305508
					},
					"end_location": {
						"lng": 121.4894781678,
						"lat": 31.224674024221
					},
					"vehicle_info": {
						"type": 5,
						"detail": null
					}
				}, {
					"distance": 870,
					"duration": 120,
					"instructions": "老西门站乘地铁8号线(沈杜公路方向)经过1站到陆家浜路站",
					"path": "121.4894781678,31.224674024221;121.4894781678,31.224674024221;121.4895769814,31.224496462643;121.49046630386,31.222882952867;121.49102325328,31.221732634654;121.49210121989,31.219262104802;121.49273003374,31.217362840647",
					"traffic_condition": [],
					"start_location": {
						"lng": 121.4894781678,
						"lat": 31.224674024221
					},
					"end_location": {
						"lng": 121.49273003374,
						"lat": 31.217362840647
					},
					"vehicle_info": {
						"type": 3,
						"detail": {
							"name": "地铁8号线",
							"type": 1,
							"stop_num": 1,
							"on_station": "老西门站",
							"off_station": "陆家浜路站",
							"first_time": "06:03",
							"last_time": "23:03"
						}
					}
				}, {
					"distance": 9,
					"duration": 7,
					"instructions": "站内换乘",
					"path": "121.49272105069,31.217362840647;121.49264918625,31.2173937233;121.49264918625,31.2173937233",
					"traffic_condition": [],
					"start_location": {
						"lng": 121.49273003374,
						"lat": 31.217362840647
					},
					"end_location": {
						"lng": 121.49264918625,
						"lat": 31.217401443961
					},
					"vehicle_info": {
						"type": 5,
						"detail": null
					}
				}, {
					"distance": 1506,
					"duration": 180,
					"instructions": "陆家浜路站乘地铁9号线(曹路方向)经过1站到小南门站(2口)",
					"path": "121.49264918625,31.217401443961;121.49264918625,31.217401443961;121.49496681446,31.217656225432;121.49863190094,31.218096300884;121.50059919001,31.218351080464;121.50354563208,31.218798873196;121.50390495429,31.219030489285;121.50413851372,31.219431955817;121.50489309035,31.22265134631",
					"traffic_condition": [],
					"start_location": {
						"lng": 121.49264918625,
						"lat": 31.217401443961
					},
					"end_location": {
						"lng": 121.50489309035,
						"lat": 31.22265134631
					},
					"vehicle_info": {
						"type": 3,
						"detail": {
							"name": "地铁9号线",
							"type": 1,
							"stop_num": 1,
							"on_station": "陆家浜路站",
							"off_station": "小南门站",
							"first_time": "06:12",
							"last_time": "00:16"
						}
					}
				}, {
					"distance": 212,
					"duration": 178,
					"instructions": "步行212米",
					"path": "121.50488410729,31.222643626081;121.50488410729,31.222643626081;121.5048032598,31.222265334108;121.5048032598,31.222265334108;121.50521648033,31.222195851742;121.5052434295,31.222195851742;121.5052434295,31.222195851742;121.5052434295,31.222319375912;121.5052434295,31.222319375912;121.50502783618,31.222327096167;121.50509071756,31.22255098329;121.50509970062,31.22260502493;121.50512664978,31.222890673076;121.50514461589,31.22300647613",
					"traffic_condition": [],
					"start_location": {
						"lng": 121.50489309035,
						"lat": 31.22265134631
					},
					"end_location": {
						"lng": 121.50581834502,
						"lat": 31.222960154925
					},
					"vehicle_info": {
						"type": 5,
						"detail": null
					}
				}]
			]
		}],
		"taxi": null
	},
	"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/direction/v2/transit?origin=40.056878,116.30815&destination=31.222965,121.505821&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;
    }
}