Skip to content Skip to sidebar Skip to footer

Settimeout Function Not Working When Looping Through Markers On Gmaps Api

I am trying to draw 50 markers in a row with 2 seconds in between each marker. The first marker gets drawn in GPS position (x, y) and then two seconds later, the second marker gets

Solution 1:

setTimeout is working fine. You are delaying all your markers by 2 seconds (the delay isn't between them).

var map;
var longi = 174.7660981;

functioninitialize(){

    var mapProp = {
        //map properties
    };

    //create map object
    map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

    //loop through setTimeout/Marker constructorfor(i = 0; i < 50; i++){
        window.setTimeout(function(){
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(-36.8606873, longi),
                map: map
            });
            longi += 0.0004;
        }, 2000*i);
    }
}

google.maps.event.addDomListener(window, "load", initialize);

var map;
var longi = 174.7660981;

functioninitialize(){

    var mapProp = {
        center: {lat:0, lng:0},
      zoom: 15
    };

    //create map object
    map = new google.maps.Map(document.getElementById("googleMap"), mapProp);

    //loop through setTimeout/Marker constructorfor(i = 0; i < 50; i++){
        window.setTimeout(function(){
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(-36.8606873, longi),
                map: map
            });
            longi += 0.0004;
          map.setCenter(marker.getPosition());
        }, 2000*i);
    }
}

google.maps.event.addDomListener(window, "load", initialize);
body, html, #googleMap {
  height: 100%;
  width: 100%;
  }
<scriptsrc="https://maps.googleapis.com/maps/api/js"></script><divid="googleMap"></div>

Post a Comment for "Settimeout Function Not Working When Looping Through Markers On Gmaps Api"