最近做百度地图的模拟数据,需要获取某条公交线路沿途站点的坐标信息,貌似百度没有现成的API,因此做了一个模拟页面,工具而已,IE6/7/8不支持 复制代码 代码如下: 获取公交站点坐标 <BR>html,body{ height: 100%;} <BR>#results,#coordinate{ display: inline-block; width: 45%; min-height: 200px; border:1px solid #e4e4e4; vertical-align: top;} <BR> 公交线路: <BR>(function(){ <BR>var tempVar; <BR>var busline = new BMap.BusLineSearch('武汉',{ <BR>renderOptions:{panel:"results"}, <BR>onGetBusListComplete: function(result){ <BR>if(result) { <BR>tempVar = result;//此时的结果并不包含坐标信息,所以getCoordinate函数不能在此调用。通过跟踪变量,坐标是在onGetBusListComplete之后才被百度的包添加进来的 <BR>busline.getBusLine(result.getBusListItem(0)); <BR>} <BR>}, <BR>// api文档中一共有四个回调,除了onGetBusListComplete和onBusLineHtmlSet之外,还有onBusListHtmlSet和onGetBusLineComplete, <BR>// 经过测试只有在onBusLineHtmlSet这一步(线路格式化完毕)的时候,才会将坐标添加到tempVar中 <BR>// 所以上面busline.getBusLine(result.getBusListItem(0));是必须的,不然没有办法获得坐标列表 <BR>onBusLineHtmlSet : function(){ <BR>try{ <BR>getCoordinate(tempVar); <BR>}catch(e){ <BR>} <BR>} <BR>}); <BR>function getCoordinate(result){ <BR>var coordinate = document.getElementById("coordinate"); <BR>var stations = result['0']._stations; <BR>var html = []; <BR>stations.forEach(function(item){ <BR>html.push('<li>' + item.name + ' ' + item.position.lng + ' ' + item.position.lat + ''); <BR>}); <BR>coordinate.innerHTML = '<ul>' + html.join('') + ''; <BR>} <BR>document.getElementById('btn-search').onclick = function(){ <BR>busline.getBusList(document.getElementById("busId").value); <BR>} <BR>})(); <BR> 获取反向线路的话就把var stations = result['0']._stations;改为var stations = result[xx]._stations;整理了一下: 复制代码 代码如下: 获取公交站点坐标 <BR>html,body{ height: 100%;} <BR>#results,#coordinate{ display: inline-block; width: 45%; min-height: 200px; border:1px solid #e4e4e4; vertical-align: top;} <BR> 公交线路: <BR>var global = {}; <BR>global.tempVar = {}; <BR>global.index = 0; <BR>global.lineNo = 0; <BR>var busline = new BMap.BusLineSearch('武汉',{ <BR>renderOptions:{panel:"results"}, <BR>onGetBusListComplete: function(result){ <BR>if(result) { <BR>global.tempVar = result; <BR>} <BR>}, <BR>onBusLineHtmlSet : function(){ <BR>try{ <BR>getCoordinate(global.tempVar); <BR>}catch(e){ <BR>} <BR>} <BR>}); <BR>function $$(id){ <BR>return document.getElementById(id); <BR>} <BR>function getCoordinate(result){ <BR>var coordinate = $$("coordinate"); <BR>var stations = result[global.index]._stations; <BR>var html = []; <BR>stations.forEach(function(item,index){ <BR>html.push('<li>' + global.lineNo + '#' + global.index + '#' + index + '#' + item.name + '#' + item.position.lng + '#' + item.position.lat + ''); <BR>}); <BR>coordinate.innerHTML = '<ul>' + html.join('') + ''; <BR>} <BR>$$('btn-search').onclick = function(){ <BR>global.lineNo = $$("busId").value; <BR>busline.getBusList(global.lineNo); <BR>} <BR>$$('results').addEventListener('click',function(event){ <BR>var target = event.target; <BR>if('a' == target.tagName.toLowerCase() && 'dt' == target.parentNode.tagName.toLowerCase()){ <BR>event.preventDefault(); <BR>var tempHtml = target.parentNode.innerHTML; <BR>var indexOfValue = tempHtml.indexOf('_selectBusListItem('); <BR>global.index = - ( - tempHtml.substring(indexOfValue + '_selectBusListItem('.length,indexOfValue + '_selectBusListItem('.length + 1) ); <BR>busline.getBusLine(global.tempVar.getBusListItem(global.index)); <BR>} <BR>},false); <BR> 来自小西山子