I'm trying to get a better understanding of how to use javascript with Google's Geocoder API.
Here is the full set-up: http://jsfiddle.net/25gybgds/
$("#btn").click(function () {
var geocoder = new google.maps.Geocoder();
var finaltable = document.getElementById("datatable");
alert("Row count: " + count);
var i;
for (i = 1; i < 20; i++) {
var fullAddress = finaltable.rows[i].cells[0].innerHTML + ", " + finaltable.rows[i].cells[1].innerHTML;
getLatLong(fullAddress, i);
}
function getLatLong(addr, llrow) {
var latCell = finaltable.rows[llrow].cells[2];
var lonCell = finaltable.rows[llrow].cells[3];
geocoder.geocode({
'address': addr
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
latCell.innerHTML = String(results[0].geometry.location.lat());
lonCell.innerHTML = String(results[0].geometry.location.lng());
} else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT){
latCell.innerHTML = "OVER";
lonCell.innerHTML = "OVER";
}else {
alert("Something got wrong " + status);
latCell.innerHTML = "ERR";
lonCell.innerHTML = "ERR";
}
});
}
});
I've been exploring to use the setTimeout() function to no avail, and I keep getting the OVER_QUERY_LIMIT error (My full table is 319 rows, but even doing 20 will already cause this problem).
I've tried doing
setTimeout(geocoder.geocode({ // rest of code here}), 3000);
and it still somehow does not allow me to do it.
I've tried searching a lot of solutions in Stackoverflow like this and this, but I can't seem to apply them correctly to my specific case. Can someone point me in the right direction on how to exactly set a timeout for this to ensure that I can use this to populate the every single row (or whether I'm doing something fundamentally wrong?)
Thanks!