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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:07:59+00:00 2026-05-23T01:07:59+00:00

My application plots a bunch of markers on a map read from a MySQL

  • 0

My application plots a bunch of markers on a map read from a MySQL database, and I want to display some content in each of the infowindows. My issue is the infowindows will only display the content of the last entry in my database. Below is my code, where I simply try to display the name of the location. I’ve tried alerting the variables to see if they are indeed displaying the correct content, which they are. I realize it is a javascript closure problem, but I don’t know how to go about fixing this.

var startLat;
var startLng;
var locationName;

<?php foreach($workstations as $workstation):?>
    startLat = "<?php echo $workstation['lat']?>";
    startLng = "<?php echo $workstation['lng']?>";
    locationName = "<?php echo $workstation['name']?>";

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(startLat, startLng),
        map: map,
        icon: "images/purple.png"
    });

    //load all the infowindow content
    var contentString = [
                    '<div id="InfoText" style="margin: 0px; padding: 0px; overflow: hidden; border:0px">',
                    '<div class="tabs"><ul><li><a href="#tab1">General</a></li>',
                    '<li><a href="#tab2">General 2</a></li></ul>',
                    '<div id="tab1">',
                    '<b>' + locationName + '</b> - ' + locationName + '<BR>',
                    '<input id="save" type="submit" value="Reserve Today" onclick="saveStation(addMarker)" />',
                    '</div>',
                    '<div id="tab2">',
                    '</div>',
                    '</div>'
                           ].join('');

    //alert(contentString);
    var infowindow = new google.maps.InfoWindow({content: contentString});

    new google.maps.event.addListener(marker, 'click', (function(marker) {
        return function(){
            infowindow.open(map, marker);
            }
        })(marker));
<?php endforeach;?>
  • 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-23T01:08:00+00:00Added an answer on May 23, 2026 at 1:08 am

    Yes. This is a js closure issue. You have one variable “marker” which all of the maps event listeners are pointing to. Try this instead:

    Place this at the top (before the foreach loop):

    function addMarkerListener( mark, mp, win )
    {
        new google.maps.event.addListener(
              mark, 
              'click', 
              function(){win.open(mp,mark)}
    }
    

    Then, instead of the listener at the end of your code block add this:

    addMarkerListener( marker, map, infowindow );
    

    Basically, what is happening in your version is that js is saying, “Ok, I’m going to create a function and have the marker variable populate it. Got it.” When the time comes to actually call the function, it says, “I’m looking for the variable ‘marker’, what’s in that variable?” Unfortunately, this has been re-assigned in the meanwhile and now it represents the most recent assignment.

    The version here, on the other hand, tells JavaScript, “I have these brand new variables, they only exist here, so don’t look for them anywhere else. When one of them is clicked, then display the value related to it.” When the item is clicked, JS says, “Hmm… this variable was only assigned to be used in this context so I guess I will use that.”

    I could go into variable binding, and there is some exciting Lambda Calculus stuff involved, but that is a bit much for this scope.

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

Sidebar

Related Questions

My Application is recieving data from external hardware and plots it onto a panel
I'm developing an application for data gathering from Internet and creating charts and plots
My application is fed data from an external device. After each data point, there
I am writing an application that retrieves coordinates from a webservice, then plots them
I want to develop a GUI application that allows me to plot candlestick bars,
Application is running in my local system app.config file <add key=dbconn value=Data Source=(local);database=VTech;User ID=sa;Password=sa;
I wrote a simple opengl application in C which plots sin(x). This is my
I have a java application that streams raw data and draws real time plots
Im looking for a solution or some guidelines on how to, from a c++
I'm using Core Plot in an iOS-Application and want to format the steps of

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.