Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming you are calling the search something like this:</p> <pre><code>for (var i = 0; i &lt; legs.length; i++) { service.search(legs[i], callback); } </code></pre> <p>then you can do this by keeping track of all the results, and the number of times you've been called back:</p> <pre><code>var callbacksOutstanding = legs.length; var allPlaces = []; var callback = function(legPlaces, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { allPlaces.push.apply(allPlaces, legPlaces); } if (--callbacksOustanding === 0) { processPlaces(allPlaces); } }; for (var i = 0; i &lt; legs.length; i++) { service.search(legs[i], callback); } </code></pre> <p>Where <code>processPlaces</code> is some function you've written to process the complete array of <code>PlaceResult</code> objects.</p> <p>Now the three <code>var</code> statements there are <em>not</em> global variables if this is all wrapped in a function (as it should be), because in Javascript you can and should define functions inside other functions. And if you do so, the inner functions will have access to variables in the outer functions. So for me this might look like this:</p> <pre><code>function findAndProcessPlaces(legs) { var callbacksOutstanding = legs.length; var allPlaces = []; var callback = function(legPlaces, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { allPlaces.push.apply(allPlaces, legPlaces); } if (--callbacksOustanding === 0) { processPlaces(allPlaces); } }; for (var i = 0; i &lt; legs.length; i++) { service.search(legs[i], callback); } } </code></pre> <p>NB: The <code>allPlaces.push.apply</code> line performs an in-place concatenation of one array to another. You could also write:</p> <pre><code>allPlaces = allPlaces.concat(legPlaces) </code></pre> <p>at this point.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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