javascript - How to call the function, if the count reaches the limit -
i using google maps , placing map markers using array locations
, want call function clearmarkers()
if i==26
how can this?
code:
var markers = []; var map; var india = new google.maps.latlng(21.9200,77.9000); var image = 'images/pushpins/set1.png'; function initialize() { var mapoptions = { zoom: 5, center: india }; map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); drop(); } function drop() { clearmarkers(); (var = 0; < locations.length; i++) { var city = locations[i][0]; var pro_cat = locations[i][1]; addmarkerwithtimeout(locations[i][2], * 500); getcity(city,pro_cat, * 500); } if (i==26){ clearmarkers(); } } function addmarkerwithtimeout(position, timeout) { window.settimeout(function() { markers.push(new google.maps.marker({ position: position, map: map, icon: image, optimized: false, //animation: google.maps.animation.bounce })); }, timeout); } function clearmarkers() { (var = 0; < markers.length; i++) { markers[i].setmap(null); } markers = []; document.getelementbyid('order_list').innerhtml = ""; } function getcity(city_name, product_cat, timeout){ window.settimeout(function() { var writecity = document.createtextnode(city_name+', '+product_cat); document.getelementbyid("order_list").appendchild(writecity); document.getelementbyid("order_list").appendchild(document.createelement('br')); }, timeout); } google.maps.event.adddomlistener(window, 'load', initialize);
here using loop call function not working
function drop() { clearmarkers(); (var = 0; < locations.length; i++) { if (i==26){ clearmarkers(); } var city = locations[i][0]; var pro_cat = locations[i][1]; addmarkerwithtimeout(locations[i][2], * 500); getcity(city,pro_cat, * 500); } }
the if
statement outside loop i
variable not set , comparison 26 return false unless have global i
variable. change drop function into:
function drop() { clearmarkers(); (var = 0; < locations.length; i++) { var city = locations[i][0]; var pro_cat = locations[i][1]; addmarkerwithtimeout(locations[i][2], * 500); getcity(city,pro_cat, * 500); if (i==26){ clearmarkers(); } } }
the problem expierience bit different. place markers after main function processed. therefore nothing cleared. if remove window.settimout
in addmarkerwithtimeout
should work.
function addmarkerwithtimeout(position, timeout) { markers.push(new google.maps.marker({ position: position, map: map, icon: image, optimized: false, //animation: google.maps.animation.bounce })); }
Comments
Post a Comment