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 6609867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:46:06+00:00 2026-05-25T19:46:06+00:00

Okay, so I realize I’m the 100th person asking a question about this, but

  • 0

Okay, so I realize I’m the 100th person asking a question about this, but even after researching and trying different things for days now, I can’t figure it out.
I have a function that will create markers on a google map. I will pass this function the coordinates as well as the HTML that will be displayed in the infoWindow that should be attached to each marker.

The problem that so many other people have is that even in my super simple example the content of the infoWindow is always the last content set for any infoWindow instead of the content set when creating a specific marker.

How can I fix this?

Here’s my code:

var somerandomcounter = 0;

function addMarkerNew(){
    markers[somerandomcounter] = new GMarker(new GLatLng(52.3666667+somerandomcounter,9.7166667+somerandomcounter),{title: somerandomcounter});
    map.addOverlay(markers[somerandomcounter]);

    var marker = markers[somerandomcounter];

    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml("<b>"+somerandomcounter+"</b>");   
    });

somerandomcounter++;
}
  • 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-25T19:46:07+00:00Added an answer on May 25, 2026 at 7:46 pm

    The issue here is variable scope. Let’s break it down:

    // variable is in the global scope
    var somerandomcounter = 0;
    
    function addMarkerNew(){
        // now we're in the addMarkerNew() scope.
        // somerandomcounter still refers to the global scope variable
        // ... (some code elided)
        var marker = markers[somerandomcounter];
    
        GEvent.addListener(marker, "click", function() {
            // now we're in the click handler scope.
            // somerandomcounter *still* refers to the global scope variable.
            // When you increment the variable in the global scope,
            // the change will still be reflected here
            marker.openInfoWindowHtml("<b>"+somerandomcounter+"</b>");   
        });
    
        // increment the global scope variable
        somerandomcounter++;
    }
    

    The easiest way to fix this is to pass the somerandomcounter variable to one of the functions as an argument – this will keep the reference in the click handler pointing to the locally scoped variable. Here are two ways to do this:

    1. Pass the counter as an argument to addMarkerNew:

      // variable is in the global scope
      var somerandomcounter = 0;
      
      function addMarkerNew(counter){
          // now we're in the addMarkerNew() scope.
          // counter is in the local scope
          // ...
          var marker = markers[counter];
      
          GEvent.addListener(marker, "click", function() {
              // now we're in the click handler scope.
              // counter *still* refers to the local addMarkerNew() variable
              marker.openInfoWindowHtml("<b>"+somerandomcounter+"</b>");   
          });
      }
      
      // call the function, incrementing the global variable as you do so
      addMarkerNew(somerandomcounter++);
      
    2. Make a new function to attach the click handler, and pass the counter into that function:

      // variable is in the global scope
      var somerandomcounter = 0;
      
      // make a new function to attach the handler
      function attachClickHandler(marker, counter) {
          // now we're in the attachClickHandler() scope.
          // counter is a locally scope variable
          GEvent.addListener(marker, "click", function() {
              // now we're in the click handler scope.
              // counter refers to the local variable in 
              // the attachClickHandler() scope
              marker.openInfoWindowHtml("<b>"+counter+"</b>");
          });
      }
      
      function addMarkerNew(){
          // now we're in the addMarkerNew() scope.
          // somerandomcounter still refers to the global scope variable
          // ...
          var marker = markers[somerandomcounter];
      
          // attach the click handler
          attachClickHandler(marker, somerandomcounter)
      
          // increment the global scope variable
          somerandomcounter++;
      } 
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, this is probably a very basic question; but, I'm just getting back in
I realize this is a ridiculous request, but what I'm trying to do is
I have a best practices question. I realize this is subjective but wanted to
Okay this question is very simple: I have a facebook page, and a website.
Okay, I feel a bit foolish for having to ask this but I guess
After all the answers to my last question about fine-tuning turned out to be
Okay, so this is not the first time I've had this problem, but it
So I am primarily a C# and Java developer but I suppose this question
Okay, so first, I have searched for this everywhere but it seems like every
I realize there's already been several questions like this, but I think my case

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.