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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T01:04:42+00:00 2026-06-08T01:04:42+00:00

I have this code where I display and set all my markers. How can

  • 0

I have this code where I display and set all my markers. How can I add a popup with some information on markers with this code? I add “i” variable on text, but it sets on all markers popup with “test – 723”, where 723 is last value of the “i” variable. What is wrong?

for (var i = 0; i < arraylng.length-1; i++) {
  var marker = new google.maps.Marker({
    position: new google.maps.LatLng(arraylng[i], arraylat[i])
  });
  var infowindow = new google.maps.InfoWindow({
    content: " "
  });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent('test: ' + i + '');
    infowindow.open(map, this);
  });
  markers.push(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-06-08T01:04:43+00:00Added an answer on June 8, 2026 at 1:04 am

    First, change the loop condition to i < arraylng.length. Right now it is not capturing the last element of your array.

    JavaScript variables works with function scope so you need to call a function for each marker listener to create the correct variable references. You can use an anonymous function, as seen here, or define a function for creating the click listener:

    Multiple infoWindows:

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

    Most likely you will not want more than one InfoWindow open at the same time, because it is annoying to have to click on the close x. Then you will only want one InfoWindow object, and set the content as the marker is clicked:

    Single InfoWindow:

    ...
      var infowindow = new google.maps.InfoWindow();
    
      for (var i = 0; i < arraylng.length-1; i++) {
        var marker = new google.maps.Marker({
          position: new google.maps.LatLng(arraylng[i], arraylat[i]),
          map: map
        });
    
        makeInfoWindowEvent(map, infowindow, "test" + i, marker);
    
        markers.push(marker);
      }
    }
    
    function makeInfoWindowEvent(map, infowindow, contentString, marker) {
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(contentString);
        infowindow.open(map, marker);
      });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code which can display drop down in div tag of html.
I have this code which echoes me information from the database: <?php include('db_connect.php'); mysql_query(SET
I have my code to display information from a text file already: $myFile =
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
I have this code to display the data retrieved from DB in accounts_view.php :
I have this code: ie1.link(:text, /Exception:/) It is producing an error message which I
Hey guys, I have this piece of jquery code: <script type=text/javascript > $(function() {
For some reason, when I try to run this code, all I get is
I found this code to display a modal view: - (void)add:(id)sender { // Create
I have that problem, in html I have this code: <div> <div class='category' style=display:block>

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.