S.Honderdos
S.Honderdos

Reputation: 55

Delay marker drop api v3

Lets start with that I have no idea what I'm doing. Not really done any js before this. I try to make the markers drop with a little delay in between them. I've done my research and tried several articles but I can't make them work. Here is my code:

<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map-canvas { height: 100% }
    </style>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=KEY&sensor=false">
    </script>
    <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(52.365642,5.181599),
          zoom: 13,
          zoomControl: false,
          scaleControl: false,
          scrollwheel: false,
          disableDoubleClickZoom: true,
          disableDefaultUI: true,
          mapTypeId: google.maps.MapTypeId.ROADMAP,

          styles: [{"stylers":[{ "saturation": -29 },{ "visibility": "simplified" }]},
          {"elementType": "labels", "stylers": [{ "visibility": "off" }]}]
        }; 

        var map = new google.maps.Map(document.getElementById("map-canvas"),
            mapOptions);

        setMarkers(map, locaties);
      }

      var locaties = [
        ['Bondi en zo', 52.365642, 5.181599],
        ['Coogee en zo', 52.365642, 5.182599],
        ['Cronulla en zo', 52.365642, 5.183599],
        ['Manly en zo', 52.365642, 5.184599],
        ['Maroubra en zo', 52.365642, 5.185599]
      ];


      function drop() {
        for (var i =0; i < locaties.length; i++) {
          setTimeout(function() {
            setMarkers();
          }, i * 200);
        }
      }

      function setMarkers(map, locations) {

        for (var i = 0; i < locations.length; i++) {
          var klant = locations[i];
          var myLatLng = new google.maps.LatLng(klant[1], klant[2]);
          var marker = new google.maps.Marker({
              position: myLatLng,
              map: map,
              animation: google.maps.Animation.DROP
          });
        }
      }

      google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

ps, yeah i've changed the key

Upvotes: 1

Views: 2148

Answers (1)

1Mayur
1Mayur

Reputation: 3485

Change your functions like this, now the drop function creates a interval and drops the marker and your setMarkers function iterates the locations and calls drop function

      function drop(map, myLatLng) {
          setTimeout(function() {
            var marker = new google.maps.Marker({
              position: myLatLng,
              map: map,
              animation: google.maps.Animation.DROP
            });
          }, i * 200);
      }

      function setMarkers(map, locations) {
        for (var i = 0; i < locations.length; i++) {
          var klant = locations[i];
          var myLatLng = new google.maps.LatLng(klant[1], klant[2]);
          drop(map, myLatLng);
        }
      }

PS: please dont just copy paste code from here and there, understand first what they are trying to explain and thanks for changing the key ;)

Upvotes: 3

Related Questions