Google Maps Animated Polyline

CSS GoogleMaps JavaScript Website jQuery
Google Maps Animated Polyline

This demo shows two ways of showing the direction of a Google Maps PolyLine. One method is by moving an icon along the path of the PolyLine. The other method is by drawing another PolyLine on top of the other one section at a time to create an animation.

Download the JavaScript   (Minified)

View source on GitHub

Code Snippets

  1. var map;
  2. var polyline;
  3. var bounds;
  4. var linePartArr = [];
  5.  
  6. //timeout because jquery script is loaded later that this js file on this page
  7. setTimeout(function () {
  8. initializePolylineMap(52.52000, 5.28662);
  9. }, 50);
  10.  
  11. //create the map
  12. function initializePolylineMap(lat, lng) {
  13. //coord for the center of the map
  14. var startpos = new google.maps.LatLng(lat, lng);
  15.  
  16. //map options
  17. var options = {
  18. zoom: 8,
  19. center: startpos,
  20. zoomControl: true,
  21. mapTypeControl: false,
  22. scaleControl: false,
  23. streetViewControl: false,
  24. rotateControl: false,
  25. fullscreenControl: false,
  26. mapTypeId: google.maps.MapTypeId.TERRAIN
  27. };
  28.  
  29. //start the map
  30. map = new google.maps.Map(document.getElementById('map_canvas'), options);
  31.  
  32. //add bounds
  33. bounds = new google.maps.LatLngBounds();
  34.  
  35. //create the polyline
  36. createPolyLine();
  37.  
  38. //animate the polyline drawing
  39. animatePolyline();
  40.  
  41. //animate the icon
  42. animateIcon();
  43.  
  44. //make an array of maps coordinates for the bounds
  45. for (var i = 0; i < lineCoordinates.length; i++) {
  46. var pos = new google.maps.LatLng(lineCoordinates[i].lat, lineCoordinates[i].lng);
  47. bounds.extend(pos);
  48. }
  49.  
  50. //fit the map within the bounds
  51. map.fitBounds(bounds);
  52. }
  53.  
  54.  
  55. //add a polyline to the map
  56. function createPolyLine() {
  57. //create a symbol to animate along the route
  58. var lineSymbol = {
  59. path: google.maps.SymbolPath.CIRCLE,
  60. scale: 8,
  61. fillColor: '#566895',
  62. fillOpacity: 1,
  63. strokeColor: '#282c41',
  64. strokeOpacity: 1,
  65. strokeWeight: 2
  66. };
  67.  
  68. //create a polyline
  69. polyline = new google.maps.Polyline({
  70. path: lineCoordinates,
  71. strokeColor: '#f39e9e',
  72. strokeWeight: 5,
  73. icons: [
  74. {
  75. icon: lineSymbol,
  76. offset: '100%'
  77. },
  78. ],
  79. map: map
  80. });
  81. }
  82.  
  83.  
  84. //animate the icon on the map
  85. function animateIcon() {
  86. var lineOffset = 0;
  87.  
  88. //experiment with the speed based on the length of the line
  89. var iconSpeed = 0.2;
  90.  
  91. //move the icon
  92. setInterval(function () {
  93. lineOffset = (lineOffset + iconSpeed) % 200;
  94. var lineIcon = polyline.get('icons');
  95. lineIcon[0].offset = lineOffset / 2 + '%';
  96. polyline.set('icons', lineIcon);
  97. }, 20);
  98. }
  99.  
  100.  
  101. //animate the drawing of the polyline
  102. function animatePolyline() {
  103. var i = 0;
  104. var pause = false;
  105. var pauseLineRemove = 1500;
  106. var pauseRedrawLine = 1000;
  107.  
  108. //experiment with the speed based on the total parts in the line
  109. var drawSpeed = 50;
  110.  
  111. setInterval(function () {
  112.  
  113. //check if the end of the array is reached
  114. if (i + 1 == lineCoordinates.length && !pause) {
  115. pause = true;
  116.  
  117. //remove all the line parts, optionally with a delay to keep the fully drawn line on the map for a while
  118. setTimeout(function () {
  119. for (var j = 0; j < linePartArr.length; j++) {
  120. linePartArr[j].setMap(null);
  121. }
  122.  
  123. linePartArr = [];
  124. }, pauseLineRemove);
  125.  
  126. //delay the drawing of the next animated line
  127. setTimeout(function () {
  128. pause = false;
  129. i = 0;
  130. }, pauseRedrawLine + pauseLineRemove);
  131. }
  132.  
  133. //create a line part between the current and next coordinate
  134. if (!pause) {
  135. var part = [];
  136. part.push(lineCoordinates[i]);
  137. part.push(lineCoordinates[i + 1]);
  138.  
  139. //create a polyline
  140. var linePart = new google.maps.Polyline({
  141. path: part,
  142. strokeColor: '#ff0000',
  143. strokeOpacity: 1,
  144. strokeWeight: 5,
  145. zIndex: i + 2,
  146. map: map
  147. });
  148.  
  149. //add the polyline to an array
  150. linePartArr.push(linePart);
  151.  
  152. i++;
  153. }
  154.  
  155. }, drawSpeed);
  156. }
  157.  

In this example the Array lineCoordinates with the coordinates of the PolyLine is in the following format.

  1. var lineCoordinates = [
  2. { lat: 53.531003, lng: 6.483307 },
  3. { lat: 53.441118, lng: 6.834869 },
  4. { lat: 53.339565, lng: 6.884308 }
  5. ];