Through Baidu Map to obtain bus line site coordinates JS code _javascript skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.