Google Places JS API: autocomplete search with setInterval on markers -


i have implemented code returning results places search box, use setinterval there delay in between dropping markers on map. i'm having trouble attempt @ it. appreciated i'm getting frustrated thought simple task! :)

this code works, no setinterval delay:

for (var = 0, place; place = places[i]; i++) {  								      var image = {          url: 'img/marker.png',          size: new google.maps.size(48, 48),          origin: new google.maps.point(0, 0),          anchor: new google.maps.point(24, 48),          scaledsize: new google.maps.size(50, 50)      };        // create marker each place.      var marker = new google.maps.marker({          map: map,          animation: google.maps.animation.drop,          icon: image,          position: place.geometry.location      });        var request = { reference: place.reference };      markers.push(marker); //push markers array.      bounds.extend(place.geometry.location);    }

my attempt @ setinterval, not work:

var = 0;  var place;  place = places[i];  var interval = setinterval(function () {        var image = {          url: 'img/marker.png',          size: new google.maps.size(48, 48),          origin: new google.maps.point(0, 0),          anchor: new google.maps.point(24, 48),          scaledsize: new google.maps.size(50, 50)      };        // create marker each place.      var marker = new google.maps.marker({          map: map,          animation: google.maps.animation.drop,          icon: image,          position: place.geometry.location      });          i++;        var request = { reference: place.reference };      markers.push(marker); //push markers array.      bounds.extend(place.geometry.location);        if (i >= markers.length) clearinterval(interval);    }, 500);

  1. setinterval not proper method, must use settimeout (setinterval runs function again , again, while settimeout runs function once....for each marker)

  2. to achieve desired effect must increment delay(e.g. using i*500)

example:

function initialize() {    var map = new google.maps.map(document.getelementbyid('map-canvas'), {        center: new google.maps.latlng(52.520006, 13.404953),        zoom: 22      }),      service = new google.maps.places.placesservice(map),      markers = [],      bounds = new google.maps.latlngbounds(map.getcenter());            service.radarsearch({      types: ['restaurant'],      location: map.getcenter(),      radius: 5000    }, function(places, status) {      if (status === google.maps.places.placesservicestatus.ok) {        var image = {          url: 'http://maps.gstatic.com/mapfiles/markers2/dd-via.png'        };        (var = 0, place; place = places[i]; i++) {            settimeout((function(place) {            return function() {              bounds.extend(place.geometry.location);              map.fitbounds(bounds);                var marker = new google.maps.marker({                map: map,                animation: google.maps.animation.drop,                icon: image,                position: place.geometry.location              });              markers.push(marker);            }          })(place), 250 * i)          }        }    });  }    google.maps.event.adddomlistener(window, 'load', initialize);
html,  body,  #map-canvas {    height: 100%;    margin: 0px;    padding: 0px;  }
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places"></script>  <div id="map-canvas"></div>


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -