javascript - Trying to use Geocoder for several arrays of 100 addresses each one -
i use javascript program run geocoder latitude , longitude array of addresses 100 , run fine. however, if want process list of addresses more 100, have run program several times, each time filling array 100 addresses or less, until complete list.
i try modify it, , split array in several arrays, each 1 of 100 addresses , run geocoder each array. however, executed last array, maybe because program run geocoder in recursive mode.
is there way run geocode() several arrays?
in example, try process array of 150 addresses, split in 2 arrays, 1 of 100 , other 1 of 50.
here initialize variables:
<script type="text/javascript"> var map = null; var geocoder = null; var addresses = null; /* array used geocode() function */ var current_address = 0; var geocode_results = new array(); var markers = new array(); var infowindows = new array(); var timeouts = 0; var geocodewait = 1000; //wait second betweeen requests function initialize() { var mapoptions = { 'zoom': 1, 'center': new google.maps.latlng(0.0,0.0), 'maptypeid': google.maps.maptypeid.roadmap }; map = new google.maps.map(document.getelementbyid("map_canvas"), mapoptions); geocoder = new google.maps.geocoder(); }
this function split array of addresses in 02 arrays , call geocode() function processes last array
function submitform() { current_address = 0; /* array contain list of 150 addressess */ var temp_addresses = document.getelementbyid("addresses").value.split("\n"); for(var x=0;x<2;x++) /*here try split list in 2 arrays */ { current_address = 0; var ini = x*100; var fin = ini+99; addresses = new array(); /*array reinitialized in each loop */ if (fin > temp_addresses.length) fin = temp_addresses.length; for(var i=ini;i<fin;i++) { if(temp_addresses[i].length>1)addresses.push(temp_addresses[i]); /* here 'addresses' array filled used 'geocode()' function */ } geocode(); /* function recursive executed when 'x' last value */ } }
here geocode() function:
function geocode() { if (current_address<addresses.length && geocoder) { document.getelementbyid("progress").innerhtml = "geocoding " + (current_address+1) + " of " + addresses.length; geocoder.geocode( { 'address': addresses[current_address]}, function(response, status) { geocode_results[current_address] = new array(); geocode_results[current_address]['status'] = status; if (!response || status != google.maps.geocoderstatus.ok) { if(status == google.maps.geocoderstatus.zero_results){ geocode_results[current_address]['lat'] = 0; geocode_results[current_address]['lng'] = 0; current_address++; } else { timeouts++; if(timeouts>6){ alert("you have reached limit of of requests can make google ip address in 1 day, please wait 24 hours continue"); } } } else { timeouts = 0; var top_location = response[0]; var lat = math.round(top_location.geometry.location.lat() * 1000000)/1000000; var lng = math.round(top_location.geometry.location.lng() * 1000000)/1000000; geocode_results[current_address]['lat'] = lat; geocode_results[current_address]['lng'] = lng; geocode_results[current_address]['l_type'] = top_location.geometry.location_type; var marker = markers[current_address] = new google.maps.marker({ position: new google.maps.latlng(lat,lng), map: map, title:"line " + (current_address+1) }); var infowindow = infowindows[current_address] = new google.maps.infowindow({ content: addresses[current_address] + "<br/>latitude:" + lat + "<br/>longitude:" + lng }); google.maps.event.addlistener(markers[current_address], 'click', function() { infowindow.open(map,marker); }); current_address++; } var wait = geocodewait+(timeouts * geocodewait); //if keeps timeing out increase wait time settimeout("geocode()",wait); }); } else { document.getelementbyid("progress").innerhtml = "finished"; displayresults(); document.getelementbyid("progress").innerhtml = ""; fitall(); } } function displayresults() { var response = "" for(var i=0;i<addresses.length;i++) { response += addresses[i] + "\t" + geocode_results[i]['lat'] + "\t" + geocode_results[i]['lng'] + "\n"; } document.getelementbyid("resultados").value += response + "\n"; }
you try changing addresses array local variable instead of global variable.
function submitform() { current_address = 0; /* array contain list of 150 addressess */ var temp_addresses = document.getelementbyid("addresses").value.split("\n"); for(var x=0;x<2;x++) /*here try split list in 2 arrays */ { current_address = 0; var ini = x*100; var fin = ini+99; var addresses = new array(); /*array reinitialized in each loop */ if (fin > temp_addresses.length) fin = temp_addresses.length; for(var i=ini;i<fin;i++) { if(temp_addresses[i].length>1)addresses.push(temp_addresses[i]); /* here 'addresses' array filled used 'geocode()' function */ } geocode(addresses); /* function recursive executed when 'x' last value */ } }
and accept parameter in geocode method
function geocode(address)
you need remove following line top of file
var addresses = null; /* array used geocode() function */
why need split address array of 100 addresses? using array of 150 addresses should work.
instead of iterating on temp_addresses try sending them directly geocode function
geocode(temp_addresses);
Comments
Post a Comment