Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've hit this one too, and i just ran out of it (a quite bastard of a thing, imho). </p> <p>The main problem here is with the asynchronous geocoder.geocode() function: the inline callback in which you are writing your code is being executed at arbitrary time (the time google takes to answer your request), and it has nothing to do with your for cycle.</p> <p>For this reason, your <strong>i</strong> counter variable will often contain the last possible value, because the execution of a short <strong>for</strong> cycle is much faster than an http request to reach the server and come back.</p> <p>Mainly, the solution is combining toghether this </p> <p><a href="https://stackoverflow.com/questions/10555097/gmaps-js-geocode-using-passing-variables-with-asynchronous-geocode-function">GMaps JS Geocode: Using/Passing Variables With Asynchronous Geocode Function?</a></p> <p>with this</p> <p><a href="http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/" rel="nofollow noreferrer">http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/</a></p> <p>as pointed from Ruslan Polutsygan. What made the real trick to me was keep any iterative operation outside the geocoder callback. So, this is the 'core':</p> <pre><code>function geocodeSite(site) { geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': site.place + ', IT'}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { site.position = results[0].geometry.location; addMarker(site); } else { alert("Geocode was not successful for the following reason: " + status); } }); } function addMarker(site) { marker = new google.maps.Marker({ map: map, position: site.position, animation: google.maps.Animation.DROP, title: site.name }); google.maps.event.addListener(marker, 'click', function () { infoWindow.setContent(site.name); infoWindow.open(map, this); }); } </code></pre> <p>and this is how i use it:</p> <pre><code>&lt;script type='text/javascript'&gt; var data = &lt;?php print $geodata?&gt;; for (var i = 0; i &lt; witaly_data.length; i++) { geocodeSite(data[i]); } &lt;/script&gt; </code></pre> <p>where <em>data</em> is a json variable i generated on server side, and it consists of a name and an address (the one we pass to the geocoder).</p> <p>Obviously, you have to initialize your map elsewhere (i do in jquery document.ready()) and declare it as a global variable, like the infoWindow instance aswell.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload