Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:12:46+00:00 2026-06-06T19:12:46+00:00

I am new to Javascript coding and have looked for this information in many

  • 0

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.

In a for loop I am trying to request POIs in a region (leg of the route) using –

service.search(request, callback);

where request includes parameters like location and type of POI queried. I have implemented the callback function as below –

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.
    }   
}

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.

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.

  1. 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.

  2. The other option, I was thinking of is getting a return (myResults) from the callback function and then collecting (appending) all the results.

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.

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.

Any suggestion/ throughts/ help will be very useful for me.

And I want to use only javascript and nothing else for this.

Thanks a lot in advance,
axs

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:12:49+00:00Added an answer on June 6, 2026 at 7:12 pm

    Assuming you are calling the search something like this:

    for (var i = 0; i < legs.length; i++) {
        service.search(legs[i], callback);
    }
    

    then you can do this by keeping track of all the results, and the number of times you’ve been called back:

    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 < legs.length; i++) {
        service.search(legs[i], callback);
    }
    

    Where processPlaces is some function you’ve written to process the complete array of PlaceResult objects.

    Now the three var statements there are not 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:

    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 < legs.length; i++) {
            service.search(legs[i], callback);
        }
    }
    

    NB: The allPlaces.push.apply line performs an in-place concatenation of one array to another. You could also write:

    allPlaces = allPlaces.concat(legPlaces)
    

    at this point.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to the whole JavaScript and jQuery coding but I'm currently doing this
New to javascript, but I'm sure this is easy. Unfortunately, most of the google
im new at javascript and i can get this slider to work but not
I'm quite new to javascript but have undertaken a task to get better aquainted
am new to coding in javascript and am presently working on a web page
I am quite experienced with coding in Javascript, but there's still one thing I
Hi I'm very new to web development but have a coding background. I'm trying
I'm new at javascript and while there are many more complex solutions, I don't
I'm pretty new to Javascript, so bear with me if this is obvious. Basically
I'm new to javascript/jquery and I've done some poking around the web, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.