zoukankan      html  css  js  c++  java
  • 【百度地图API】如何制作公交线路的搜索?如331路

    摘要:

      从A点到B点的公交导航大家都知道怎么做了,那么单独查询331路公交车的公交路线,如何制作呢?我们一起来学习一下~

    -----------------------------------------------------------------------------------------------------------------

    一、创建地图和网页样式

    两句话建立地图:

    var map = new BMap.Map("container");
    map.centerAndZoom(
    new BMap.Point(116.322, 40.003), 12);

      

    然后把网页结构搭建好。有一张图片,一个文本框,一个按钮。还有一张地图。

    CSS样式我就直接给出了,这里就不多说了。大家可以去W3Cschool学习~~这是我最爱的网站:http://www.w3school.com.cn/css/index.asp

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>搜索331路公交</title>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
    </head>
    <body>
    <p><img src="http://map.baidu.com/img/logo-map.gif" /><span style="display:inline-block;200px;">&nbsp;</span><input type="text" value="331" id="busId" />路公交&nbsp;<input type="button" value="查询" onclick="busSearch();" /></p>
    <div style="clear:both">&nbsp;</div>
    <div style="float:left;600px;height:500px;border:1px solid gray" id="container"></div>
    <div id="results" style="float:left;300px;height:500px;font-size:13px;"></div>
    </body>
    </html>
    <script type="text/javascript">
    var map = new BMap.Map("container");
    map.centerAndZoom(
    new BMap.Point(116.322, 40.003), 12);
    </script>

      

    效果图:

    二、公交线路接口

    先来看看百度地图API给出的接口类参考:

    使用构造函数,创建一个公交线路的查询。并在renderOptions里设置查询成功后,结果的展示。

    var busline = new BMap.BusLineSearch(map,{
    renderOptions:{map:map,panel:
    "results"},
    onGetBusListComplete:
    function(result){
    if(result) {
    var fstLine = result.getBusListItem(0);//获取第一个公交列表显示到map上
    busline.getBusLine(fstLine);
    }
    }
    });

      

    创建一个函数,获取到文本框中输入的“331”路公交车,(还可以输入104,或者987,注意,只能支持数字)然后,执行查询。

    function busSearch(){
    var busName = document.getElementById("busId").value;
    busline.getBusList(busName);
    }

      

    效果图:

    全部源代码:

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>搜索331路公交</title>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
    </head>
    <body>
    <p><img src="http://map.baidu.com/img/logo-map.gif" /><span style="display:inline-block;200px;">&nbsp;</span><input type="text" value="331" id="busId" />路公交&nbsp;<input type="button" value="查询" onclick="busSearch();" /></p>
    <div style="clear:both">&nbsp;</div>
    <div style="float:left;600px;height:500px;border:1px solid gray" id="container"></div>
    <div id="results" style="float:left;300px;height:500px;font-size:13px;"></div>
    </body>
    </html>
    <script type="text/javascript">
    var map = new BMap.Map("container");
    map.centerAndZoom(
    new BMap.Point(116.322, 40.003), 12);

    var busline = new BMap.BusLineSearch(map,{
    renderOptions:{map:map,panel:
    "results"},
    onGetBusListComplete:
    function(result){
    if(result) {
    var fstLine = result.getBusListItem(0);//获取第一个公交列表显示到map上
    busline.getBusLine(fstLine);
    }
    }
    });
    function busSearch(){
    var busName = document.getElementById("busId").value;
    busline.getBusList(busName);
    }
    </script>

      

  • 相关阅读:
    xapian的使用
    Andriod 环境配置以及第一个Android Application Project
    2013Esri全球用户大会之ArcGIS for Server&Portal for ArcGIS
    window server 2012 更改密钥 更改系统序列号
    持续集成之路——数据访问层的单元测试(续)
    多项式相乘与相加演示
    hdu 1847 博弈基础题 SG函数 或者规律2种方法
    solaris之cpu
    Android音效SoundPool问题:soundpool 1 not retry
    poj1845-Sumdiv
  • 原文地址:https://www.cnblogs.com/milkmap/p/2178553.html
Copyright © 2011-2022 走看看