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

  • Home
  • SEARCH
  • 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 8869427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:28:51+00:00 2026-06-14T17:28:51+00:00

I am having trouble with my Google Maps web app, i am trying to

  • 0

I am having trouble with my Google Maps web app, i am trying to add markers to a map and then when the user clicks on that marker, the name of marker appears.

I store information about each marker in an array like so:

var citymap = {};
      citymap['edinburgh'] = {
        center: new google.maps.LatLng(55.934120, -3.226569),
        population: 284,
        image:clueImage,
        color: '#ff0000',
        elementId:'clue1'
      };
       citymap['clue2'] = {
        center: new google.maps.LatLng(55.970783, -3.164594),
        population: 284,
        image:clueImage,
        color: '#ff0000',
        elementId:'clue2'
      }
         citymap['clue4'] = {
        center: new google.maps.LatLng(55.939583, -3.202092),
        population: 284,
        image:clueImage,
        color: '#ff0000',
        elementId:'clue3'
      }
         citymap['clue9'] = {
        center: new google.maps.LatLng(0, 0),
        population: 284,
        image:clueImage,
        color: '#ff0000',
        elementId:'clue1'
      }

I then loop through the array and add the marker to the map:

for (var city in citymap) {

       var playerMarker = new google.maps.Marker({
      position: citymap[city].center,
      map: map,
      icon: citymap[city].image
      });

      google.maps.event.addListener(playerMarker, "click", function() {
      var elementConnected = citymap[city].elementId;
     console.log(elementConnected);
    });

        }

Everything works as intended, however when I click on any of the markers, they all log the value of the last marker in the array. I know this is because thats where the loop finished, but I can not think of a way of making this work.

Help would be very helpful!

  • 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-14T17:28:52+00:00Added an answer on June 14, 2026 at 5:28 pm

    I think this is the universal “closure” problem in Javascript that people have. Someone might give a better way of doing this, but I think you need something like this:

    google.maps.event.addListener(playerMarker, "click", (function() {
        return function () {
            var elementConnected = citymap[city].elementId;
            console.log(elementConnected);
        };
    })(city));
    

    Or if possible, depending on what else you’re doing inside the click event, maybe this:

    google.maps.event.addListener(playerMarker, "click", (function(elementId) {
        return function () {
            var elementConnected = elementId;
            console.log(elementConnected);
        };
    })(citymap[city].elementId));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble to incorporate Google Maps in an adroid app created with
I'm trying to place images on top of Google Maps and I'm having trouble
I'm trying to work with the google maps API and am having some trouble.
I am having trouble rendering a google map infowindow anchored with multiple markers. I
Am trying to add 2-way bindings between event listeners on Google Map V3 markers
I'm having trouble with Internet Explorer when trying to use Google Maps. It works
I'm having trouble with the setMap function with Google Maps API v3 when trying
I'm trying out the new Google App Scripts Data Base and I'm having trouble
I'm having trouble getting my markers to show up using Google Maps API V3.
I installed the Google Maps API V3, but I'm having trouble getting the resize

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.