Recently do Baidu map simulation data, need to get a bus line along the site coordinates information, seemingly Baidu does not have ready-made API, so do a simulation page, tools, IE6/7/8 not support
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title> get bus site coordinates </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>
<body>
<p><label for= "Busid" > Bus line: </label><input type= "text" value= "521" id= "Busid"/><input Type= "button" id= "Btn-search" value= "Query"/></p>
<div id= "Results" ></div>
<div id= "Coordinate" ></div>
<script type= "Text/javascript" >
(function () {
var TempVar;
var busline = new Bmap.buslinesearch (' Wuhan ', {
Renderoptions:{panel: "Results"},
Ongetbuslistcomplete:function (Result) {
if (result) {
TempVar = result;//The result at this time does not contain coordinate information, so the Getcoordinate function cannot be invoked here. By tracking variables, the coordinates were ongetbuslistcomplete after the Baidu package was added.
Busline.getbusline (Result.getbuslistitem (0));
}
},
There are four callbacks in the API documentation, in addition to Ongetbuslistcomplete and Onbuslinehtmlset, as well as Onbuslisthtmlset and Ongetbuslinecomplete,
The test only adds coordinates to the TempVar when the Onbuslinehtmlset step is completed (the line is formatted)
So above Busline.getbusline (Result.getbuslistitem (0)); it's necessary, otherwise there's no way to get a list of coordinates.
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>
To get the reverse line, the var stations = result[' 0 ']._stations is changed to var stations = result[xx]._stations; sorted out:
Copy Code code as follows:
<! DOCTYPE html>
<meta charset= "Utf-8"/>
<title> get bus site coordinates </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>
<body>
<p><label for= "Busid" > Bus line: </label><input type= "text" value= "581" id= "Busid"/><input Type= "button" id= "Btn-search" value= "Query"/></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 (' Wuhan ', {
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>
From Little Western Hills.