当前位置:编程学习 > JAVA >>

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

最近做百度地图的模拟数据,需要获取某条公交线路沿途站点的坐标信息,貌似百度没有现成的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>

 


作者 西山

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,