Note that there are some explanatory texts on larger screens.

plurals
  1. POReturn from a callback function in Javascript
    primarykey
    data
    text
    <p>I am new to Javascript coding and have looked for this information in many places but haven't found one working solution, hence I am posting this question here. </p> <p>In a for loop I am trying to request POIs in a region (leg of the route) using -</p> <pre><code>service.search(request, callback); </code></pre> <p>where request includes parameters like location and type of POI queried. I have implemented the callback function as below -</p> <pre><code>function callback(myResults, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { alert('myResults: ' + myResults.length); // entire remaining code here, where I do some processing on the POIs. } } </code></pre> <p>This code is working and at each leg of the route I get to see the POIs, and also number of POIs is displayed using alert.</p> <p>My issue is that I do not want to process the "myResult" here for each leg, but collect all the results for the entire route (made up of many legs) and then do the processing at once. </p> <ol> <li><p>I first thought of creating a global variable ("myPOIs") and append "myResults" at each leg and once the entire route is parsed, then to process this myPOIs, but I am not able (or I dont know how) to append this myResults to a global variable myPOIs.</p></li> <li><p>The other option, I was thinking of is getting a return (myResults) from the callback function and then collecting (appending) all the results. </p></li> </ol> <p>The only difference between 1 and 2 is that, in 1, its a global variable I will be appending myResults to within the if condition, and in 2, if the if condition is true then return the myResults to the service.search and then collect the results there. </p> <p>I tried using myPOIs.push(myResults) and a few other options, but when I check the myPOIs.length it is always null. I really dont know how to get the myResults out of the callback function.</p> <p>Any suggestion/ throughts/ help will be very useful for me. </p> <p>And I want to use only javascript and nothing else for this.</p> <p>Thanks a lot in advance, axs</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. 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