0

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!

Community
  • 1
  • 1
lyk
  • 1,578
  • 5
  • 25
  • 49
  • Use `setInterval` instead of `setTimeout`. – MrUpsidown Oct 02 '15 at 07:12
  • Hi @MrUpsidown, how do i use the setInterval for the for loop in this case? – lyk Oct 02 '15 at 07:29
  • 1
    Your setTimeout-implementation is wrong, 1. you must provide a function and not a function-call as first argument. 2. you must increase the delay Fixed fiddle: http://jsfiddle.net/25gybgds/6/ – Dr.Molle Oct 02 '15 at 13:01
  • @Dr.Molle hi, thank you so much for your help! if you want to post it as an answer i can accept it. thanks again! – lyk Oct 06 '15 at 07:09

0 Answers0