使⽤百度地图API实现轨迹回放
调⽤百度地图API实现路线的轨迹回放功能其实很简单,只要搞懂以下⼏点即可:
1.需要⽤Polyline⽅法先绘制好路线图
2.⽤Marker添加标注点
3.关键⼀步,通过结合定时器,使⽤Marker创建的标注点实例的tPosition改变标注点位置,实现播放功能
代码分享,直接复制即可使⽤
[html]
1. <!DOCTYPE html>
2. <html>
3. <head>
4. <meta name="viewport" content="initial-scale=1.0, ur-scalable=no" />
5. <meta http-equiv="Content-Type" content="text/html; chart=utf-8" />
6. <title>Track</title>
7. <style type="text/css">财务工作总结
8. html{height:100%}
9. body{height:100%;margin:0px;padding:0px}
10. #controller{width:100%; border-bottom:3px outt; height:30px; filter:alpha(Opacity=100); -moz-opacity:1; opacity:1; z-
index:10000; background-color:lightblue;}
11. #container{height:100%}
12. </style>
13. <script type="text/javascript" src="api./api?v=1.5&ak=D2b4558ebed15e52558c6a766c35ee73"></script>
14. <script type="text/javascript">
15. //获取所有点的坐标
16. var points = [
17. new BMap.Point(114.00100, 22.550000), new BMap.Point(114.00130, 22.550000),
18. new BMap.Point(114.00160, 22.550000), new BMap.Point(114.00200, 22.550000),
19. new BMap.Point(114.00300, 22.550500), new BMap.Point(114.00400, 22.550000),
20. new BMap.Point(114.00500, 22.550000), new BMap.Point(114.00505, 22.549800),
21. new BMap.Point(114.00510, 22.550000), new BMap.Point(114.00515, 22.550000),
新文化运动的意义22. new BMap.Point(114.00525, 22.550400), new BMap.Point(114.00537, 22.549500)
办公室恋情23. ];
24.
25. var map; //百度地图对象
26. var car; //汽车图标
27. var label; //信息标签
28. var centerPoint;
29.
30. var timer; //定时器
31. var index = 0; //记录播放到第⼏个point
32.
33. var followChk, playBtn, pauBtn, retBtn; //⼏个控制按钮
34.
35. function init() {
36. followChk = ElementById("follow");
37. playBtn = ElementById("play");
38. pauBtn = ElementById("pau");
39. retBtn = ElementById("ret");
40.
41. //初始化地图,选取第⼀个点为起始点
42. map = new BMap.Map("container");
43. AndZoom(points[0], 15);
44. ableScrollWheelZoom();
45. map.addControl(new BMap.NavigationControl());
46. map.addControl(new BMap.ScaleControl());
47. map.addControl(new BMap.OverviewMapControl({isOpen: true}));
48.
49. //通过DrivingRoute获取⼀条路线的point
50. var driving = new BMap.DrivingRoute(map);
51. driving.arch(new BMap.Point(114.00100, 22.550000), new BMap.Point(113.95100, 22.550000));
52. driving.tSearchCompleteCallback(function() {
53. //得到路线上的所有point
54. points = Results().getPlan(0).getRoute(0).getPath();
55. //画⾯移动到起点和终点的中间
56. centerPoint = new BMap.Point((points[0].lng + points[points.length - 1].lng) / 2, (points[0].lat + points[points.length - 1].lat) / 2);
57. map.panTo(centerPoint);
58. //连接所有点
59. map.addOverlay(new BMap.Polyline(points, {strokeColor: "black", strokeWeight: 5, strokeOpacity: 1}));
60.
61. //显⽰⼩车⼦
62. label = new BMap.Label("", {offt: new BMap.Size(-20, -20)});
63. car = new BMap.Marker(points[0]);
64. car.tLabel(label);
65. map.addOverlay(car);
66.
67. //点亮操作按钮校园生活作文600字
68. playBtn.disabled = fal;
69. retBtn.disabled = fal;
70. });
71. }
72.
73. function play() {
74. playBtn.disabled = true;
75. pauBtn.disabled = fal;
76.
77. var point = points[index];
78. if(index > 0) {
79. map.addOverlay(new BMap.Polyline([points[index - 1], point], {strokeColor: "red", strokeWeight: 1, strokeOpacity: 1}));
80. }
81. label.tContent("经度: " + point.lng + "<br>纬度: " + point.lat);
82. car.tPosition(point);
83. index++;
84. if(followChk.checked) {
85. map.panTo(point);
86. }
87. if(index < points.length) {
88. timer = window.tTimeout("play(" + index + ")", 200);
89. } el {
90. playBtn.disabled = true;
91. pauBtn.disabled = true;
92. map.panTo(point);
93. }
94. }
95.
96. function pau() {
97. playBtn.disabled = fal;
98. pauBtn.disabled = true;
99.
100. if(timer) {
101. window.clearTimeout(timer);
102. }
103. }
104.
105. function ret() {
106. followChk.checked = fal;
107. playBtn.disabled = fal;
108. pauBtn.disabled = true;
广西高考报名109.甘地自传
110. if(timer) {
111. window.clearTimeout(timer);
112. }
113. index = 0;
114. car.tPosition(points[0]);
115. map.panTo(centerPoint);
妈妈堕落的秘密116. }
117. </script>
118. </head>
119.
120. <body onload="init();">
121. <div id="controller" align="center">
122. <input id="follow" type="checkbox"><span >画⾯跟随</span></input> 123. <input id="play" type="button" value="播放" onclick="play();" disabled />
124. <input id="pau" type="button" value="暂停" onclick="pau();" disabled />
125. <input id="ret" type="button" value="重置" onclick="ret()" disabled />
126. </div>
127. <div id="container"></div>
128. </body>
129. </html>
四川有什么好玩的