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

  • SEARCH
  • Home
  • 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 6716247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:42:38+00:00 2026-05-26T08:42:38+00:00

I have created a function that will create a markers array with places obtained

  • 0

I have created a function that will create a markers array with places obtained by using the Places API. The type parameter is used for the place type, and the icon is used for the marker icon.

function setPOIMarkerArray(type, icon) {

var array = new Array();

var request = {
    location: map.getCenter(),
    radius: '2000',
    types: [type]
};

var service = new google.maps.places.PlacesService(map);
service.search(request, function(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {  
        for (i in results) {
            place = results[i];

            var marker = new google.maps.Marker({
                icon: icon,
                position: new google.maps.LatLng(place.geometry.location.lat(), place.geometry.location.lng())
            });

            google.maps.event.addListener(marker, 'click', function() {
                var content = '<img class="left-arrow-grocery" src="/img/leftArrow.png"><div class="grocery-infobox">' + place.name + '<br/>' + place.vicinity + '</div>';
                ib.setContent(content);
                ib.open(map, marker);
            });

            array.push(marker);

        }

        return array;       

    }

});
}

When I call this code using the $(function(){});, in which I set up my map and other stuff, I call var groceryStores = setPOIMarkerArray('grocery_or_supermarket','grocery.png')
but the groceryStores array ends up being empty if I console.log(groceryStores)

If I console.log(array) inside the setPOIMarkerArray it does show the results.

I believe this is due to the fact that the code doesn’t wait for the response to come back from the google API. What can I do to fix this, or what is the problem? Thanks

  • 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-05-26T08:42:38+00:00Added an answer on May 26, 2026 at 8:42 am

    The search function is performed asynchronously, so the array will only have the marker added to it after the response is received. The browser performs this request in the background and doesn’t wait for it to respond, that is why the array is empty directly after the function is called.

    EDIT: You don’t need to do anything to overcome it. It’s doing what it’s supposed to do. However, if you want to be notified when the response is returned from the search, you can add a function call in your callback.

    Example: Display an alert when a marker is added.

    function marker_added() { alert('marker added'); }

    //callback code here...
    array.push(marker);
    marker_added();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a function that takes a SQL command and produces output that
I have created a function in PHP that calls a webservice and parses through
How do I create a Ruby function that does not have an explicit number
I have a function that processes a given vector, but may also create such
I have three classes that all have a static function called 'create'. I would
Does anyone have a working class or function to create the hashed email that
I have this class that have a function to load other classes and create
I have created a function that loops through a set of returned JSON. I
We've got some API integrations that will periodically create shipments for orders. What I'd
I have created an experimental fast rectangular object tracking system; it will be used

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.