運動軌跡[轉]

<!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>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=E06eb9d756d0eafc722effb355657b4c"></script>
    <title>車輛運行軌跡測試</title>
<script src="http://c.cnzz.com/core.php"></script></head>
<body>
    <div id="allmap"></div>
</body>
</html>
<script type="text/javascript">
    var startLong = 106.652024;
    var startLat = 26.617221;
    var endLong = 106.652024;
    var endLat = 26.614221;
    
    var startLongR = 106.652024;
    var startLatR = 26.617221;
    var endLongR = 106.652024;
    var endLatR = 26.614221;
    
    

    var linesPoints = null;
    // 百度地圖API功能
    var map = new BMap.Map("allmap");    // 創建Map實例
    map.centerAndZoom(new BMap.Point(106.652024,26.617221), 15);  // 初始化地圖,設置中心點座標和地圖級別
    map.addControl(new BMap.MapTypeControl());   //添加地圖類型控件
    map.setCurrentCity("貴陽");          // 設置地圖顯示的城市 此項是必須設置的
    map.enableScrollWheelZoom(true);     //開啓鼠標滾輪縮放
   
   setInterval(goWay,500);
   var carMk;
   var myIcon = new BMap.Icon("http://sandbox.runjs.cn/uploads/rs/101/wmbvrxnx/kache.png", new BMap.Size(37,25), {imageOffset: new BMap.Size(0, 0)});//卡車
   
   function goWay(){
       startLong = endLong;
       startLat = endLat;
       endLong = getRound(endLong);
       endLat = getRound(endLat);
       
       drawIcon(startLong,startLat,endLong,endLat);
       //drawRedLine();
   }
   
   function getRound(temp){
       var i = Math.round(Math.random()*9+1);
       if(i%2==0){
           return temp + i*0.0001;
       }else{
           return temp - i*0.0001;
       }
   }
   function getRound1(temp){
       var i = Math.round(Math.random()*9+1);
       if(i%2==0){
           return temp + i*0.0002;
       }else{
           return temp - i*0.0002;
       }
   }
   
   function drawGreenLine(startLong,startLat,endLong,endLat){
       var polyline = new BMap.Polyline([
                                          new BMap.Point(startLong,startLat),//起始點的經緯度
                                          new BMap.Point(endLong,endLat)//終止點的經緯度
                                          ], {strokeColor:"green",//設置顏色 
                                          strokeWeight:3, //寬度
                                          strokeOpacity:1});//透明度
       map.addOverlay(polyline);
   }
   
   function drawRedLine(){
       startLongR = endLongR;
       startLatR = endLatR;
       endLongR = getRound1(endLongR);
       endLatR = getRound1(endLatR);
       var polyline1 = new BMap.Polyline([
                                          new BMap.Point(startLongR,startLatR),//起始點的經緯度
                                          new BMap.Point(endLongR,endLatR)//終止點的經緯度
                                          ], {strokeColor:"red",//設置顏色 
                                          strokeWeight:3, //寬度
                                          strokeOpacity:1});//透明度
       map.addOverlay(polyline1);
   }

   function drawIcon(startLong,startLat,endLong,endLat){
       if(carMk){
           map.removeOverlay(carMk);
       }
       carMk = new BMap.Marker(
                   new BMap.Point(endLong,endLat),//起始點的經緯度
                  {icon:myIcon});
       map.addOverlay(carMk);
       drawGreenLine(startLong,startLat,endLong,endLat);
   }
</script>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章