百度地圖API 路線模擬

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
    body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微軟雅黑";}
    </style>
    
    <title>參考線路</title>
</head>
<body>
    <div id="allmap"></div>
</body>
</html>
<script src="./lib/base/base.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=4d06aa5b2c816cc52a1a9cd008808c68"></script>
<script type="text/javascript">
var lon=getQueryString('lon') //目標位置
var lat=getQueryString('lat')
var mylon=getQueryString('mylon')//本身的位置
var mylat=getQueryString('mylat')

    // 百度地圖API功能

    //添加地圖類型控件
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(lon,lat), 15);
 
//增長比例尺
var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角,添加比例尺
    var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默認縮放平移控件
var top_right_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_RIGHT, type: BMAP_NAVIGATION_CONTROL_SMALL}); //右上角
 
map.addControl(top_left_control); //增長比例尺
        map.addControl(top_left_navigation);
        map.addControl(top_right_navigation);

    var myP1 = new BMap.Point(lon,lat); //起點
    var myP2 = new BMap.Point(mylon,mylat); //終點
    var myIcon = new BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/Mario.png", new BMap.Size(32, 70), { //小車圖片
        //offset: new BMap.Size(0, -5), //至關於CSS精靈
        imageOffset: new BMap.Size(0, 0) //圖片的偏移量。爲了是圖片底部中心對準座標點。
     });
    var driving2 = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}}); //駕車實例
    driving2.search(myP1, myP2); //顯示一條公交線路

    window.run = function (){
        var driving = new BMap.DrivingRoute(map); //駕車實例
        driving.search(myP1, myP2);
        driving.setSearchCompleteCallback(function(){
            var pts = driving.getResults().getPlan(0).getRoute(0).getPath(); //經過駕車實例,得到一系列點的數組
            var paths = pts.length; //得到有幾個點

            var carMk = new BMap.Marker(pts[0],{icon:myIcon});
            map.addOverlay(carMk);
            i=0;
            function resetMkPoint(i){
                carMk.setPosition(pts[i]);
                if(i < paths){
                    setTimeout(function(){
                        i++;
                        resetMkPoint(i);
                    },60);
                }
            }
            setTimeout(function(){
                resetMkPoint(5);
            },100)

        });
    }

    setTimeout(function(){
        run();
    },1500);
</script>
相關文章
相關標籤/搜索