通过百度地图获取公交线路的站点坐标的js代码

时间:2024-04-18 09:28:14 

最近做百度地图的模拟数据,需要获取某条公交线路沿途站点的坐标信息,貌似百度没有现成的API,因此做了一个模拟页面,工具而已,IE6/7/8不支持


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>获取公交站点坐标</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">公交线路:</label><input type="text" value="521" id="busId" /><input type="button" id="btn-search" value="查询" /></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;//此时的结果并不包含坐标信息,所以getCoordinate函数不能在此调用。通过跟踪变量,坐标是在onGetBusListComplete之后才被百度的包添加进来的
busline.getBusLine(result.getBusListItem(0));
}
},
// api文档中一共有四个回调,除了onGetBusListComplete和onBusLineHtmlSet之外,还有onBusListHtmlSet和onGetBusLineComplete,
// 经过测试只有在onBusLineHtmlSet这一步(线路格式化完毕)的时候,才会将坐标添加到tempVar中
// 所以上面busline.getBusLine(result.getBusListItem(0));是必须的,不然没有办法获得坐标列表
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>


获取反向线路的话就把var stations = result['0']._stations;改为var stations = result[xx]._stations;整理了一下:


<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>获取公交站点坐标</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">公交线路:</label><input type="text" value="581" id="busId" /><input type="button" id="btn-search" value="查询" /></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>


来自小西山子

标签:百度地图,公交线路
0
投稿

猜你喜欢

  • sql server查询语句阻塞优化性能

    2024-01-25 00:44:52
  • JS实现移动端判断上拉和下滑功能

    2023-07-13 22:05:21
  • python计算机视觉OpenCV库实现实时摄像头人脸检测示例

    2022-05-17 22:41:50
  • 我的栅格系统实现

    2008-09-21 13:50:00
  • mysql中写判断语句的方法总结

    2024-01-21 15:17:30
  • sqlserver 存储过程动态参数调用实现代码

    2011-10-24 19:41:22
  • Python sorted函数详解(高级篇)

    2021-08-14 15:58:28
  • 微信小程序radio组件使用详解

    2024-04-19 10:41:44
  • 五种方法解决 Web2.0设计中的匹配度

    2007-09-22 10:58:00
  • 登陆成功后自动计算秒数执行跳转

    2023-10-10 19:47:59
  • Mootools 1.2教程(22)——同时进行多个形变动画

    2008-12-29 14:11:00
  • 在ASP编程中nothing代表什么意思?

    2011-04-15 10:47:00
  • Python详解复杂CSV文件处理方法

    2021-04-05 11:12:02
  • Bootstrap图片轮播效果详解

    2023-08-24 20:31:55
  • 超详细汇总21个值得收藏的mysql优化实践

    2024-01-17 21:01:18
  • mysql多表join时候update更新数据的方法

    2024-01-15 12:56:37
  • python的函数形参和返回值你了解吗

    2021-10-26 05:49:28
  • MySQL实例讲解子查询的使用

    2024-01-17 05:38:27
  • Python接口自动化之浅析requests模块get请求

    2021-12-07 07:20:45
  • django 快速启动数据库客户端程序的方法示例

    2023-07-31 09:31:59
  • asp之家 网络编程 m.aspxhome.com