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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:55:33+00:00 2026-05-15T15:55:33+00:00

Maybe I’m going completely off track with what I’m trying to do, but I’m

  • 0

Maybe I’m going completely off track with what I’m trying to do, but I’m tempted just to go back to version 2 because I got that working easily (but I would like to be mobile-friendly).

I’m trying to generate a few markers, and to save code, I’ve put the marker generation into a for loop, which loops through an array of markers (there are actual values instead of (long,lat,x,x,x) in the real array).

Is something just going completely over my head?

 function initialize() {
    var latlng = new google.maps.LatLng(25, 15);
    var myOptions = {
      zoom: 2,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


var myMarker = new Array();
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 
myMarker[0] = new Array(long,lat,x,x,x); 

for(var i = 0; i < myMarker.length; i++) {
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(myMarker[i](1), myMarker[i](2)), 
        map: map, 
        title: myMarker[i](5)
      });
    var infowindow = new google.maps.InfoWindow({content: myMarker[i](5)}); 
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
    });
}

}

This code works when I make the markers individually.

  • 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-15T15:55:34+00:00Added an answer on May 15, 2026 at 3:55 pm

    The problem is that all those listener functions are sharing the same “map” and “marker” values. You need to wrap them up in another function:

    (function(map, marker) {
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.open(map,marker);
      });
    })(map, marker);
    

    The “map” and “marker” variables change on each iteration through the loop. Those listener functions don’t have a copy of them unless you explicitly set something up to do that, like I did in that example. Thus, all the listeners end up working on the last thing the loop touched!

    edit — actually now that I read over the code again, “map” looks like it’s not changing, so it can probably be taken out of my setup (doesn’t hurt anything though).

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

Sidebar

Related Questions

Maybe I just don't know .NET well enough yet, but I have yet to
Maybe I'm just thinking about this too hard, but I'm having a problem figuring
Maybe that's only possible with SQLITE but is there also a trick to let
maybe I am being stooped... but the fact is that I am a bit
maybe a few others already asked that but I couldn't find any answer to
maybe this is an old question but i'm trying as a personal experiment to
Maybe this is something I am just missing, but is there any way to
Maybe it's just me, but I cannot understand the documentation regarding Junit test integration
Maybe easy question but I don't know how to summarize it that I would
Maybe this is just obvious to everyone but can someone explain where XOR (or

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.