0

I tried to use setTimeout inside this each loop: $("#dropdown option").each(function () but some reason not working, here is mu code:

function initialize() {
    var map = new google.maps.Map(document.getElementById("map"));
    var geocoder = new google.maps.Geocoder();
    $("#dropdown").change(function () {
        address = $("#dropdown :selected")[0].text;
        geocodeAddress(address, geocoder, map);
    });
    var address = $("#dropdown :selected")[0].text;
    $("#dropdown option").each(function ()
    {
        setTimeout(function () {
            geocodeAddress($(this).text() + ' ,Montenegro', geocoder, map);
        }, 1000);
    });
    geocodeAddress(address, geocoder, map);
}
google.maps.event.addDomListener(window, "load", initialize);

function geocodeAddress(address, geocoder, resultsMap) {
    document.getElementById('info').innerHTML = address;
    geocoder.geocode({
        'address': address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            console.log(google.maps.GeocoderStatus)
        }

        if (status === google.maps.GeocoderStatus.OK) {
            resultsMap.fitBounds(results[0].geometry.viewport);
            var marker = new google.maps.Marker({
                map: resultsMap,
                position: results[0].geometry.location
            });
            document.getElementById('info').innerHTML += "<br>" + results[0].geometry.location.toUrlValue(6);
        } else {
            alert('Geocode was not successful for the following reason: ' + status);
        }
    });
}

Here is fiddle: http://jsfiddle.net/E2TFh/13/

Anyone know what is problem?

Vladimir Djukic
  • 2,042
  • 7
  • 29
  • 60
  • possible duplicate of [OVER_QUERY_LIMIT in Google Maps API v3: How do I pause/delay in Javascript to slow it down?](http://stackoverflow.com/questions/11792916/over-query-limit-in-google-maps-api-v3-how-do-i-pause-delay-in-javascript-to-sl) – geocodezip Apr 26 '16 at 19:47
  • possible duplicate of [Settimeout to avoid over_query-limit](http://stackoverflow.com/questions/12721287/settimeout-to-avoid-over-query-limit) – geocodezip Apr 26 '16 at 19:48
  • I tried that solutions didn't work. If you see in my code I added `setTimeout` function, but not working.... – Vladimir Djukic Apr 26 '16 at 19:48
  • All your setTimeout calls are firing at the same time, just delaying the issue (by 1 second). – geocodezip Apr 26 '16 at 19:52
  • Please provide a list of addresses required to reproduce the issue. – geocodezip Apr 26 '16 at 19:54
  • Here is fiddle: http://jsfiddle.net/E2TFh/13/ – Vladimir Djukic Apr 26 '16 at 20:04
  • You do see that all the coordinates are the same in the output don't you. This isn't just a problem with OVER_QUERY_LIMIT... – geocodezip Apr 26 '16 at 20:59

1 Answers1

1
  1. you need to get function closure on the address to be geocoded
  2. you need to change the delay so all the timeouts don't expire at the same time.
var timeout = 0;
$("#dropdown option").each(function ()
{
    // update timeout, so don't all expire at the same time
    timeout = timeout+1000;
    // get function closure on the address for this entry
    var address = $(this).text();
    console.log(address);
    setTimeout(function () {
        geocodeAddress(address + ' ,Montenegro', geocoder, map);
    }, timeout);
});

updated fiddle

code snippet:

function initialize() {
  var map = new google.maps.Map(document.getElementById("map"));
  var geocoder = new google.maps.Geocoder();
  $("#dropdown").change(function() {
    address = $("#dropdown :selected")[0].text;
    geocodeAddress(address, geocoder, map, true);
  });
  var address = $("#dropdown :selected")[0].text;
  var timeout = 0;
  $("#dropdown option").each(function() {
    timeout = timeout + 1000;
    var address = $(this).text();
    console.log(address);
    setTimeout(function() {
      geocodeAddress(address + ' ,Montenegro', geocoder, map, false);
    }, timeout);
  });
  geocodeAddress(address, geocoder, map, true);
}
google.maps.event.addDomListener(window, "load", initialize);

function geocodeAddress(address, geocoder, resultsMap, fitbounds) {
  document.getElementById('info').innerHTML = address;
  geocoder.geocode({
    'address': address
  }, function(results, status) {

    console.log(results);
    if (status === google.maps.GeocoderStatus.OK) {
      if (fitbounds) resultsMap.fitBounds(results[0].geometry.viewport);
      var marker = new google.maps.Marker({
        map: resultsMap,
        position: results[0].geometry.location
      });
      document.getElementById('info').innerHTML += "<br>" + results[0].geometry.location.toUrlValue(6);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<select id="dropdown">
  <option value="1" selected>Montenegro</option>
  <option value="2">Berane</option>
  <option value="3">Podgorica</option>
  <option value="4">Kolasin</option>
  <option value="5">Kotor</option>
  <option value="6">Bar</option>
  <option value="7">Ulcinj</option>
  <option value="8">Bijelo Polje</option>
  <option value="9">Rozaje</option>
  <option value="10">Mojkovac</option>
  <option value="11">Niksic</option>
  <option value="12">Danilovgrad</option>
  <option value="13">Budva</option>
  <option value="14">Sutomore</option>
  <option value="15">Plav</option>
  <option value="16">Gusinje</option>
  <option value="17">Andrijevica</option>
  <option value="18">Herceg Novi</option>
  <option value="19">Tivat</option>
  <option value="20">Zabljak</option>
</select>
<div id="info"></div>
<div class="map-container" style="width:100%;height: 500px">
  <div id="map" style="width:100%;height: 500px"></div>
  <!--the google map is loaded already-->
</div>
geocodezip
  • 158,664
  • 13
  • 220
  • 245