Search code examples
google-mapsgoogle-maps-api-3google-maps-markersgoogle-api-javascript-client

How to move marker slowly in latest google map api


I am building a web application where one flight need to be fly from one place to another. I have build up airports and places aircrafts as marker now, how to move my aircrafts marker from one place to another.


Solution

  • you can achieve it by utilising the following:

      var lineSymbol = {
        path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,
        scale: 3, // change the size
        strokeColor: '#393'
      };
    

    You will have to look into changing this yourself if you need an aeroplane etc..

    You then need to implement the polyline for it to follow:

      // Create the polyline and add the symbol to it via the 'icons' property.
      var line = new google.maps.Polyline({
        path: [{
          lat: 51.4700,
          lng: 0.4543
        }, {
          lat: 50.1109,
          lng: 8.6821
        }, {
            lat: 55.9533,
          lng: 3    
        }, {
          lat: 51.4700,
          lng: 0.4543
        },
        ],
        strokeColor: '#FF0000',
        strokeOpacity: 1.0,
        strokeWeight: 0, // change this value to show / hide the line
        icons: [{
          icon: lineSymbol,
          offset: '100%'
        }],
        map: map
      });
    
      animateCircle(line);
    }
    

    Finally, we need to add the method to animate the symbol on the line:

    function animateCircle(line) {
      var count = 0;
      window.setInterval(function() {
        count = (count + 1) % 200; // change this to 1000 to only show the line once
        var icons = line.get('icons');
        icons[0].offset = (count / 2) + '%';
        line.set('icons', icons);
      }, 50); // change this value to change the speed
    }
    

    JSFIDDLE: https://jsfiddle.net/tu4s6302/3/