通過(guò)百度地圖獲取公交線(xiàn)路的站點(diǎn)坐標(biāo)的js代碼
更新時(shí)間:2012年05月11日 15:44:34 作者:
通過(guò)百度地圖獲取公交線(xiàn)路的站點(diǎn)坐標(biāo)的js代碼,需要的朋友可以參考下
最近做百度地圖的模擬數(shù)據(jù),需要獲取某條公交線(xiàn)路沿途站點(diǎn)的坐標(biāo)信息,貌似百度沒(méi)有現(xiàn)成的API,因此做了一個(gè)模擬頁(yè)面,工具而已,IE6/7/8不支持
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>獲取公交站點(diǎn)坐標(biāo)</title>
<style type="text/css">
html,body{ height: 100%;}
#results,#coordinate{ display: inline-block; width: 45%; min-height: 200px; border:1px solid #e4e4e4; vertical-align: top;}
</style>
<script src="http://api.map.baidu.com/api?v=1.3" type="text/javascript"></script>
</head>
<body>
<p><label for="busId">公交線(xiàn)路:</label><input type="text" value="521" id="busId" /><input type="button" id="btn-search" value="查詢(xún)" /></p>
<div id="results"></div>
<div id="coordinate"></div>
<script type="text/javascript">
(function(){
var tempVar;
var busline = new BMap.BusLineSearch('武漢',{
renderOptions:{panel:"results"},
onGetBusListComplete: function(result){
if(result) {
tempVar = result;//此時(shí)的結(jié)果并不包含坐標(biāo)信息,所以getCoordinate函數(shù)不能在此調(diào)用。通過(guò)跟蹤變量,坐標(biāo)是在onGetBusListComplete之后才被百度的包添加進(jìn)來(lái)的
busline.getBusLine(result.getBusListItem(0));
}
},
// api文檔中一共有四個(gè)回調(diào),除了onGetBusListComplete和onBusLineHtmlSet之外,還有onBusListHtmlSet和onGetBusLineComplete,
// 經(jīng)過(guò)測(cè)試只有在onBusLineHtmlSet這一步(線(xiàn)路格式化完畢)的時(shí)候,才會(huì)將坐標(biāo)添加到tempVar中
// 所以上面busline.getBusLine(result.getBusListItem(0));是必須的,不然沒(méi)有辦法獲得坐標(biāo)列表
onBusLineHtmlSet : function(){
try{
getCoordinate(tempVar);
}catch(e){
}
}
});
function getCoordinate(result){
var coordinate = document.getElementById("coordinate");
var stations = result['0']._stations;
var html = [];
stations.forEach(function(item){
html.push('<li>' + item.name + ' ' + item.position.lng + ' ' + item.position.lat + '</li>');
});
coordinate.innerHTML = '<ul>' + html.join('') + '</ul>';
}
document.getElementById('btn-search').onclick = function(){
busline.getBusList(document.getElementById("busId").value);
}
})();
</script>
</body>
</html>
獲取反向線(xiàn)路的話(huà)就把var stations = result['0']._stations;改為var stations = result[xx]._stations;整理了一下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>獲取公交站點(diǎn)坐標(biāo)</title>
<style type="text/css">
html,body{ height: 100%;}
#results,#coordinate{ display: inline-block; width: 45%; min-height: 200px; border:1px solid #e4e4e4; vertical-align: top;}
</style>
<script src="http://api.map.baidu.com/api?v=1.3" type="text/javascript"></script>
</head>
<body>
<p><label for="busId">公交線(xiàn)路:</label><input type="text" value="581" id="busId" /><input type="button" id="btn-search" value="查詢(xún)" /></p>
<div id="results"></div>
<div id="coordinate"></div>
<script type="text/javascript">
var global = {};
global.tempVar = {};
global.index = 0;
global.lineNo = 0;
var busline = new BMap.BusLineSearch('武漢',{
renderOptions:{panel:"results"},
onGetBusListComplete: function(result){
if(result) {
global.tempVar = result;
}
},
onBusLineHtmlSet : function(){
try{
getCoordinate(global.tempVar);
}catch(e){
}
}
});
function $$(id){
return document.getElementById(id);
}
function getCoordinate(result){
var coordinate = $$("coordinate");
var stations = result[global.index]._stations;
var html = [];
stations.forEach(function(item,index){
html.push('<li>' + global.lineNo + '#' + global.index + '#' + index + '#' + item.name + '#' + item.position.lng + '#' + item.position.lat + '</li>');
});
coordinate.innerHTML = '<ul>' + html.join('') + '</ul>';
}
$$('btn-search').onclick = function(){
global.lineNo = $$("busId").value;
busline.getBusList(global.lineNo);
}
$$('results').addEventListener('click',function(event){
var target = event.target;
if('a' == target.tagName.toLowerCase() && 'dt' == target.parentNode.tagName.toLowerCase()){
event.preventDefault();
var tempHtml = target.parentNode.innerHTML;
var indexOfValue = tempHtml.indexOf('_selectBusListItem(');
global.index = - ( - tempHtml.substring(indexOfValue + '_selectBusListItem('.length,indexOfValue + '_selectBusListItem('.length + 1) );
busline.getBusLine(global.tempVar.getBusListItem(global.index));
}
},false);
</script>
</body>
</html>
來(lái)自小西山子
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>獲取公交站點(diǎn)坐標(biāo)</title>
<style type="text/css">
html,body{ height: 100%;}
#results,#coordinate{ display: inline-block; width: 45%; min-height: 200px; border:1px solid #e4e4e4; vertical-align: top;}
</style>
<script src="http://api.map.baidu.com/api?v=1.3" type="text/javascript"></script>
</head>
<body>
<p><label for="busId">公交線(xiàn)路:</label><input type="text" value="521" id="busId" /><input type="button" id="btn-search" value="查詢(xún)" /></p>
<div id="results"></div>
<div id="coordinate"></div>
<script type="text/javascript">
(function(){
var tempVar;
var busline = new BMap.BusLineSearch('武漢',{
renderOptions:{panel:"results"},
onGetBusListComplete: function(result){
if(result) {
tempVar = result;//此時(shí)的結(jié)果并不包含坐標(biāo)信息,所以getCoordinate函數(shù)不能在此調(diào)用。通過(guò)跟蹤變量,坐標(biāo)是在onGetBusListComplete之后才被百度的包添加進(jìn)來(lái)的
busline.getBusLine(result.getBusListItem(0));
}
},
// api文檔中一共有四個(gè)回調(diào),除了onGetBusListComplete和onBusLineHtmlSet之外,還有onBusListHtmlSet和onGetBusLineComplete,
// 經(jīng)過(guò)測(cè)試只有在onBusLineHtmlSet這一步(線(xiàn)路格式化完畢)的時(shí)候,才會(huì)將坐標(biāo)添加到tempVar中
// 所以上面busline.getBusLine(result.getBusListItem(0));是必須的,不然沒(méi)有辦法獲得坐標(biāo)列表
onBusLineHtmlSet : function(){
try{
getCoordinate(tempVar);
}catch(e){
}
}
});
function getCoordinate(result){
var coordinate = document.getElementById("coordinate");
var stations = result['0']._stations;
var html = [];
stations.forEach(function(item){
html.push('<li>' + item.name + ' ' + item.position.lng + ' ' + item.position.lat + '</li>');
});
coordinate.innerHTML = '<ul>' + html.join('') + '</ul>';
}
document.getElementById('btn-search').onclick = function(){
busline.getBusList(document.getElementById("busId").value);
}
})();
</script>
</body>
</html>
獲取反向線(xiàn)路的話(huà)就把var stations = result['0']._stations;改為var stations = result[xx]._stations;整理了一下:
復(fù)制代碼 代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>獲取公交站點(diǎn)坐標(biāo)</title>
<style type="text/css">
html,body{ height: 100%;}
#results,#coordinate{ display: inline-block; width: 45%; min-height: 200px; border:1px solid #e4e4e4; vertical-align: top;}
</style>
<script src="http://api.map.baidu.com/api?v=1.3" type="text/javascript"></script>
</head>
<body>
<p><label for="busId">公交線(xiàn)路:</label><input type="text" value="581" id="busId" /><input type="button" id="btn-search" value="查詢(xún)" /></p>
<div id="results"></div>
<div id="coordinate"></div>
<script type="text/javascript">
var global = {};
global.tempVar = {};
global.index = 0;
global.lineNo = 0;
var busline = new BMap.BusLineSearch('武漢',{
renderOptions:{panel:"results"},
onGetBusListComplete: function(result){
if(result) {
global.tempVar = result;
}
},
onBusLineHtmlSet : function(){
try{
getCoordinate(global.tempVar);
}catch(e){
}
}
});
function $$(id){
return document.getElementById(id);
}
function getCoordinate(result){
var coordinate = $$("coordinate");
var stations = result[global.index]._stations;
var html = [];
stations.forEach(function(item,index){
html.push('<li>' + global.lineNo + '#' + global.index + '#' + index + '#' + item.name + '#' + item.position.lng + '#' + item.position.lat + '</li>');
});
coordinate.innerHTML = '<ul>' + html.join('') + '</ul>';
}
$$('btn-search').onclick = function(){
global.lineNo = $$("busId").value;
busline.getBusList(global.lineNo);
}
$$('results').addEventListener('click',function(event){
var target = event.target;
if('a' == target.tagName.toLowerCase() && 'dt' == target.parentNode.tagName.toLowerCase()){
event.preventDefault();
var tempHtml = target.parentNode.innerHTML;
var indexOfValue = tempHtml.indexOf('_selectBusListItem(');
global.index = - ( - tempHtml.substring(indexOfValue + '_selectBusListItem('.length,indexOfValue + '_selectBusListItem('.length + 1) );
busline.getBusLine(global.tempVar.getBusListItem(global.index));
}
},false);
</script>
</body>
</html>
來(lái)自小西山子
您可能感興趣的文章:
相關(guān)文章
JavaScript中從setTimeout與setInterval到AJAX異步
這篇文章主要介紹了JavaScript中從setTimeout與setInterval到AJAX異步,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02js 綁定帶參數(shù)的事件以及手動(dòng)觸發(fā)事件
js 綁定帶參數(shù)的事件以及手動(dòng)觸發(fā)事件,需要的朋友可以參考下。2010-04-04javasciprt下jquery函數(shù)$.post執(zhí)行無(wú)響應(yīng)的解決方法
這篇文章主要介紹了javasciprt下jquery函數(shù)$.post執(zhí)行無(wú)響應(yīng)的解決方法,需要的朋友可以參考下2014-03-03總結(jié)分享10 個(gè)超棒的 JavaScript 簡(jiǎn)寫(xiě)技巧
這篇文章主要總結(jié)分享10 個(gè)超棒的 JavaScript 簡(jiǎn)寫(xiě)技巧,有合并數(shù)組、克隆數(shù)組、解構(gòu)賦值、模板字面量等技巧,需要的朋友可以參考一下2022-06-06JS DOMReady事件的六種實(shí)現(xiàn)方法總結(jié)
下面小編就為大家?guī)?lái)一篇JS DOMReady事件的六種實(shí)現(xiàn)方法總結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11