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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:29:00+00:00 2026-05-14T07:29:00+00:00

I’m not sure why this isn’t working. I don’t have any errors, but what

  • 0

I’m not sure why this isn’t working. I don’t have any errors, but what happens is, no matter what marker I click on, it always clicks the last marker. Im not sure why though because the_marker is set up the same way. How can I fix this?:

(Updated with new jQuery + XML)

$(function(){
    var latlng = new google.maps.LatLng(45.522015,-122.683811);
    var settings = {
        zoom: 15,
        center: latlng,
        disableDefaultUI:true,
        mapTypeId: google.maps.MapTypeId.SATELLITE
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), settings);

    $.get('mapdata.xml',{},function(xml){
        $('location',xml).each(function(i){
            the_marker = new google.maps.Marker({
                title:$(this).find('name').text(),
                map:map,
                clickable:true,
                position:new google.maps.LatLng(
                    parseFloat($(this).find('lat').text()),
                    parseFloat($(this).find('lng').text())
                )
            });
            infowindow = new google.maps.InfoWindow({
                content: $(this).find('description').text()
            });
            new google.maps.event.addListener(the_marker, 'click', function() {
                infowindow.open(map,the_marker);
            });
        });
    });
});
  • 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-14T07:29:01+00:00Added an answer on May 14, 2026 at 7:29 am

    You are having a very common closure problem in the following loop:

    for(x in locations){
       console.log(x);
       infowindow[x] = new google.maps.InfoWindow({content: x});
       marker[x] = new google.maps.Marker({title:locations[x][0],map:map,position:locations[x][2]});
       google.maps.event.addListener(marker[x], 'click', function() {infowindow[x].open(map,marker[x]);});
    }
    

    Variables enclosed in a closure share the same single environment, so by the time the click callbacks are executed, the loop has run its course and the x variable will be left pointing to the last entry.

    You can solve it with even more closures, using a function factory:

    function makeInfoWindowEvent(map, infowindow, marker) {  
       return function() {  
          infowindow.open(map, marker);
       };  
    } 
    
    for(x in locations){
       infowindow[x] = new google.maps.InfoWindow({content: x});
    
       marker[x] = new google.maps.Marker({title: locations[x][0],
                                           map: map, 
                                           position: locations[x][3]});
    
       google.maps.event.addListener(marker[x], 'click', 
                                     makeInfoWindowEvent(map, infowindow[x], marker[x]);
    }
    

    This can be quite a tricky topic, if you are not familiar with how closures work. You may to check out the following Mozilla article for a brief introduction:

    • Working with Closures

    UPDATE:

    Further to the updated question, you should consider the following:

    • First of all, keep in mind that JavaScript does not have block scope. Only functions have scope.

    • When you assign a variable that was not previously declared with the var keyword, it will be declared as a global variable. This is often considered an ugly feature (or flaw) of JavaScript, as it can silently hide many bugs. Therefore this should be avoided. You have two instances of these implied global variables: the_marker and infowindow, and in fact, this is why your program is failing.

    • JavaScript has closures. This means that inner functions have access to the variables and parameters of the outer function. This is why you will be able to access the_marker, infowindow and map from the callback function of the addListener method. However, because your the_marker and infowindow are being treated as global variables, the closure is not working.

    All you need to do is to use the var keyword when you declare them, as in the following example:

    $(function() {
       var latlng = new google.maps.LatLng(45.522015,-122.683811);
    
       var settings = {
          zoom: 15,
          center: latlng,
          disableDefaultUI: true,
          mapTypeId: google.maps.MapTypeId.SATELLITE
       };
    
       var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
    
       $.get('mapdata.xml', {}, function(xml) {
          $('location', xml).each(function(i) {
    
             var the_marker = new google.maps.Marker({
                title: $(this).find('name').text(),
                map: map,
                clickable: true,
                position: new google.maps.LatLng(
                   parseFloat($(this).find('lat').text()),
                   parseFloat($(this).find('lng').text())
                )
             });
    
             var infowindow = new google.maps.InfoWindow({
                content: $(this).find('description').text();
             });
    
             new google.maps.event.addListener(the_marker, 'click', function() {
                infowindow.open(map, the_marker);
             });
          });
       });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
I have a JSP page retrieving data and when single or double quotes are
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I'm looking for suggestions for debugging... If you view this site in Firefox or
I am trying to loop through a bunch of documents I have to put

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.