index.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  7. <title>轨迹回放</title>
  8. <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
  9. <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
  10. <style>
  11. html, body, #container {
  12. height: 100%;
  13. width: 100%;
  14. }
  15. .input-card .btn{
  16. margin-right: 1.2rem;
  17. width: 9rem;
  18. }
  19. .input-card .btn:last-child{
  20. margin-right: 0;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div id="container"></div>
  26. <div class="input-card">
  27. <h4>轨迹回放控制</h4>
  28. <div class="input-item">
  29. <input type="button" class="btn" value="开始动画" id="start" onclick="startAnimation()"/>
  30. <input type="button" class="btn" value="暂停动画" id="pause" onclick="pauseAnimation()"/>
  31. </div>
  32. <div class="input-item">
  33. <input type="button" class="btn" value="继续动画" id="resume" onclick="resumeAnimation()"/>
  34. <input type="button" class="btn" value="停止动画" id="stop" onclick="stopAnimation()"/>
  35. </div>
  36. </div>
  37. <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=093a59e5f88f22a7fcee1f4708b98878"></script>
  38. <script>
  39. var marker, lineArr = [
  40. // [116.478935,39.997761],[116.478939,39.997825],[116.478912,39.998549],[116.478912,39.998549],[116.478998,39.998555],[116.478998,39.998555],[116.479282,39.99856],[116.479658,39.998528],[116.480151,39.998453],[116.480784,39.998302],[116.480784,39.998302],[116.481149,39.998184],[116.481573,39.997997],[116.481863,39.997846],[116.482072,39.997718],[116.482362,39.997718],[116.483633,39.998935],[116.48367,39.998968],[116.484648,39.999861]
  41. ];
  42. var map = new AMap.Map("container", {
  43. resizeEnable: true,
  44. // center: [116.397428, 39.90923],
  45. zoom: 17
  46. });
  47. marker = new AMap.Marker({
  48. map: map,
  49. // position: [116.478935,39.997761],
  50. icon: "https://webapi.amap.com/images/car.png",
  51. offset: new AMap.Pixel(-26, -13),
  52. autoRotation: true,
  53. angle:-90,
  54. });
  55. // 绘制轨迹
  56. var polyline = new AMap.Polyline({
  57. map: map,
  58. // path: lineArr,
  59. showDir:true,
  60. strokeColor: "#28F", //线颜色
  61. // strokeOpacity: 1, //线透明度
  62. strokeWeight: 6, //线宽
  63. // strokeStyle: "solid" //线样式
  64. });
  65. var passedPolyline = new AMap.Polyline({
  66. map: map,
  67. // path: lineArr,
  68. strokeColor: "#AF5", //线颜色
  69. // strokeOpacity: 1, //线透明度
  70. strokeWeight: 6, //线宽
  71. // strokeStyle: "solid" //线样式
  72. });
  73. marker.on('moving', function (e) {
  74. //marker.setPosition(new AMap.LngLat(e.passedPath[0].lat,e.passedPath[0].lng));
  75. // passedPolyline.setPath(e.passedPath);
  76. });
  77. map.setFitView();
  78. function startAnimation () {
  79. // test();
  80. // marker.moveAlong(lineArr, 200);
  81. // marker.moveTo([116.478912,39.998549],200)
  82. // marker.moveTo([116.478998,39.998555],200)
  83. }
  84. function pauseAnimation () {
  85. marker.pauseMove();
  86. }
  87. function resumeAnimation () {
  88. marker.resumeMove();
  89. }
  90. function stopAnimation () {
  91. marker.stopMove();
  92. }
  93. // AMap.event.addListener(marker, 'dragging', function(e){
  94. // var lat = e.lnglat.lat,
  95. // lng = e.lnglat.lng;
  96. // marker.setPosition(new AMap.LngLat(lng,lat));
  97. //
  98. // });
  99. setInterval("test()",1000);
  100. var i=0;
  101. var test2=new Array();
  102. function test() {
  103. map.setZoom(map.getZoom());
  104. $.ajax({
  105. url:'/web/map/getLocation',
  106. // data:{'user_id':"{$user_id}"},
  107. success:function (res) {
  108. // res[i];
  109. // a=new AMap.LngLat(res[0][0],res[0][1])
  110. // console.log()
  111. // map.setCenter(new AMap.LngLat(res[0].lat,res[0].lat));
  112. //passedPolyline.setPath(res);
  113. // console.log(res[i]);
  114. // console.log(new AMap.LngLat(res[0][0],res[0][1]));
  115. if(res!=null){
  116. if(i<res.length){
  117. console.log(res[i]);
  118. //marker.setPosition(new AMap.LngLat(res[i].lng,res[i].lat));
  119. if(i==0){
  120. marker.setPosition(res[0]);
  121. }
  122. console.log(res.length-1);
  123. // map.setCenter(new AMap.LngLat(res[i].lng,res[i].lat));
  124. map.setCenter(res[i]);
  125. //marker.moveTo([res[i].lng,res[i].lat],1000);
  126. marker.moveTo([res[i][0],res[i][1]],200);
  127. test2.push(res[i]);
  128. passedPolyline.setPath(test2);
  129. i+=1;
  130. }
  131. }
  132. }
  133. })
  134. }
  135. </script>
  136. </body>
  137. </html>