通过百度地图获取公交线路的站点坐标的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>
来自小西山子


猜你喜欢
- 下面有两种方法都可以:import numpy as npa=np.asarray([[10,20],[101,201]])# a=a[:,
- 以一种有意义的方式组织数据可能是一项挑战。有时你需要的可能是一个简单的排序,但是通常你需要做更多,你需要分组来进行分析和统计。幸运的是,SQ
- 引言你在写代码的过程中,有没有遇到过以下问题?已经写好的程序,想看看程序执行的进度?在写代码批量处理文件的时候,如何显示现在处理到第几个文件
- MySQL根据配置文件会限制Server接受的数据包大小。有时候大的插入和更新会受 max_allowed_packet 参数限制,导致写入
- 前言一般的方法此处也不列举了,还是有很多的,如双层循环判断是否相等,或新建数组比较再push等等,需要注意的是,使用splice方法移除元素
- 今天我们用python+tkinter安装带界面的井字棋,效果如图所示。Tkinter 是 Python 的标准 GUI 库。Python
- 1、打开文件得到文件句柄并赋值给一个变量2、通过句柄对文件进行操作3、关闭文件示例文件'你好呀'我是于超嗯再见文件操作基本流
- 本文实例讲述了Python将名称映射到序列元素中的方法。分享给大家供大家参考,具体如下:问题:希望通过名称来访问元素,减少结构中对位置的依赖
- 为方便用ipset 来管理防火墙,写了下面Ipset类来对Ip进行管理#!/usr/bin/env python# coding: utf-
- 前言由于总是出错,记录一下连接MySQL数据库的过程。连接过程1.下载MySQL并安装,这里的版本是8.0.182.下载MySQL的jdbc
- 计数排序找到给定序列的最小值与最大值创建一个长度为最大值-最小值+1的数组,初始化都为0然后遍历原序列,并为数组中索引为当前值-最小值的值+
- 一、旧式的字符串格式化% 操作符参考以下示例:>>> name = "Eric">>>
- 假设我们有一幅图像,图像中的文本被旋转了一个未知的角度。为了对文字进行角度的校正,我们需要完成如下几个步骤:1、检测出图中的文本范围2、计算
- 本文实例为大家分享了vue+moment实现倒计时的具体代码,供大家参考,具体内容如下示例代码<!-- 使用计算属性,传入截止日期 -
- 1.选择数据库 USE刚链接到MySQL时,没有数据库打开供你使用,而我们需要选择一个数据库,才能进行以下的操作。方法:USE语句USE M
- 已经记不得是在哪个网站上看到的了,一般情况下对于验证码的校验,大家很容易写成下面这样: <% If Request.Form(&quo
- 第一步、导入需要的包import osimport scipy.io as sioimport numpy as npimport torc
- 内容摘要:现在InterNet 越来越成为生活中不可或缺的一部分,制作网页的动态语言也越来越多,主要流行的有以下几种,ASP,PH
- 1.前言有时候,我们需要把A库A1表某一部分或全部数据导出到B库B1表中,如果系统运维工程师没打通两个库链接,我们执行T-SQL是处理数据导
- 环境准备Python3.6pip install Django==2.0.1pip install celery==4.1.0pip ins