Track playback with Baidu Map API

Source: Internet
Author: User
Tags polyline

Call Baidu Map API to achieve the path of the track playback function is actually very simple, as long as understand the following points can be:

1. Need to use Polyline method to draw a good road map first

2. Add a callout point with marker

3. Key step, by combining the timer, using the marker created by the callout point instance setposition change the location of the labeling point, to achieve playback function

Code sharing, direct copy to use

[HTML]View Plaincopy
  1. <! DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <Meta name="viewport" content="initial-scale=1.0, User-scalable=no" />
  5. <Meta http-equiv= "content-type" content= "text/html; Charset=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 outset; height:30px; Filter:alpha (opacity=);-moz-opacity:1; Opacity:1; z-index:10000;  Background-color:lightblue;}
  11. #container {height:100%}
  12. </style>
  13. <script type="Text/javascript" src= "http://api.map.baidu.com/api?v=1.5&ak= D2b4558ebed15e52558c6a766c35ee73 "></script>
  14. <script type="Text/javascript">
  15. Get the coordinates of all points
  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. var map; Baidu Map Object
  25. var car; Car icon
  26. var label; Information labels
  27. var CenterPoint;
  28. var timer; Timer
  29. var index = 0;//record played to the first point
  30. var followchk, playbtn, pausebtn, resetbtn; Several control buttons
  31. function init () {
  32. followchk = document.getElementById ("Follow");
  33. playbtn = document.getElementById ("Play");
  34. pausebtn = document.getElementById ("pause");
  35. resetbtn = document.getElementById ("reset");
  36. Initialize the map, select the first point as the starting point
  37. map = new Bmap.map ("container");
  38. Map.centerandzoom (Points[0], 15);
  39. Map.enablescrollwheelzoom ();
  40. Map.addcontrol (New Bmap.navigationcontrol ());
  41. Map.addcontrol (New Bmap.scalecontrol ());
  42. Map.addcontrol (New Bmap.overviewmapcontrol ({isopen:true}));
  43. Get a point for a route via Drivingroute
  44. var driving = new Bmap.drivingroute (map);
  45. Driving.search (New Bmap.point (114.00100, 22.550000), New Bmap.point (113.95100, 22.550000));
  46. Driving.setsearchcompletecallback (function () {
  47. Get all the point on the route
  48. points = driving.getresults (). Getplan (0). Getroute (0). GetPath ();
  49. The screen moves to the middle of the start and end
  50. CenterPoint = new Bmap.point ((POINTS[0].LNG + points[points.length-1].lng)/2, (Points[0].lat + POINTS[POINTS.L  Ength-1].lat)/2);
  51. Map.panto (CenterPoint);
  52. Connect All Points
  53. Map.addoverlay (New Bmap.polyline (points, {strokecolor: "Black", Strokeweight:5, strokeopacity:1}));
  54. Show small car
  55. label = new Bmap.label ("", {offset:new bmap.size (-20,-20)});
  56. car = new Bmap.marker (Points[0]);
  57. Car.setlabel (label);
  58. Map.addoverlay (car);
  59. Light action button
  60. playbtn.disabled = false;
  61. resetbtn.disabled = false;
  62. });
  63. }
  64. function Play () {
  65. playbtn.disabled = true;
  66. pausebtn.disabled = false;
  67. var point = Points[index];
  68. if (index > 0) {
  69. Map.addoverlay (New Bmap.polyline ([Points[index-1], point], {strokecolor: "Red", Strokeweight:1, strokeopacity:1}));
  70. }
  71. Label.setcontent ("Longitude:" + point.lng + "<br> Latitude:" + Point.lat);
  72. Car.setposition (point);
  73. index++;
  74. if (followchk.checked) {
  75. Map.panto (point);
  76. }
  77. if (index < points.length) {
  78. timer = Window.settimeout ("Play (" + Index + ")", 200);
  79. } else {
  80. playbtn.disabled = true;
  81. pausebtn.disabled = true;
  82. Map.panto (point);
  83. }
  84. }
  85. function Pause () {
  86. playbtn.disabled = false;
  87. pausebtn.disabled = true;
  88. if (timer) {
  89. Window.cleartimeout (timer);
  90. }
  91. }
  92. function Reset () {
  93. followchk.checked = false;
  94. playbtn.disabled = false;
  95. pausebtn.disabled = true;
  96. if (timer) {
  97. Window.cleartimeout (timer);
  98. }
  99. index = 0;
  100. Car.setposition (Points[0]);
  101. Map.panto (CenterPoint);
  102. }
  103. </Script>
  104. </head>
  105. <body onload="init ();" >
  106. <div id="Controller" align="center">
  107.         <input < span class= "attribute" >id= "follow"  type=" checkbox "><span style= "FONT-SIZE:12PX;" > screen follow </span> </input>  
  108.         < Input id= "play"  type=< Span class= "Attribute-value" > "button"  value= "play"  onclick= "play ();"  disabled />  
  109.         <input < span class= "attribute" >id= "pause"  type=" button " value=" pause " onclick= "pause ();"  disabled />  
  110. <input id="reset" type="button" value="reset" onclick="reset ()" Disabled />
  111. </div>
  112. <div id="container"></div>
  113. </body>
  114. </html>

Track playback with Baidu Map API

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.