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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:10:22+00:00 2026-06-10T18:10:22+00:00

I’m working on a problem involving Google Maps API and infowindows. When I click

  • 0

I’m working on a problem involving Google Maps API and infowindows. When I click on a marker, an infowindow pops up, and I am trying to make it so that when that happens, the lat/lng for that marker is displayed in the infowindow. The lat/lng in this case being stored in the stops array:

<script type="text/javascript"
  src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript">
    var map;
    var marker;
    var infowindow;   //Global infowindow created
    function initialize() {
        var latlng = new google.maps.LatLng(37.784, -122.408);
        var stops = new Array();
        stops = [
     new google.maps.LatLng(37.7655, -122.4525899),
     new google.maps.LatLng(37.7649999, -122.45656),
     new google.maps.LatLng(37.7643, -122.4608199),

     ];
        var myOptions = {
             zoom: 10,
             mapTypeId: google.maps.MapTypeId.ROADMAP,
             center: new google.maps.LatLng(37.7655, -122.4525899)

        };
        map = new google.maps.Map(document.getElementById("content"),
    myOptions);
        infowindow = new google.maps.InfoWindow({   //infowindow options set
            maxWidth: 355
        });

        var i = 0;
        for (i < 0; i < stops.length; i++) {
            marker = new google.maps.Marker({
                position:stops[i],
                map: map,
                title: "Stop Coords: "+ stops[i]
            });
            var len=stops.length;
            popupDirections(marker, len, stops);
        }
    }

    function popupDirections(marker, len,stops) {
        //this function created listener listens for click on a marker
        google.maps.event.addListener(marker, 'click', function () {
            for (var i = 0; i < len; i++)
            { infowindow.setContent("Stop coords: " + stops[i]); } //sets the content of your global infowindow to string "Tests: "
            infowindow.open(map, marker); //then opens the infowindow at the marker
        });
    }

Now, when I hover the mouse over each marker, the correct lat/lng is shown, but when I click on the marker and the infowindow pops up, I keep getting the lat/lng of stops[2]. I am not sure what the problem is, my knowledge of JS is pretty limited, I was fortunate to find a helpful post earlier on here that showed how to create infowindows.

  • 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-10T18:10:24+00:00Added an answer on June 10, 2026 at 6:10 pm

    In my opinion the best way to tackle your problem is to give each marker the Custom Object Property stopCoords:

    for (i < 0; i < stops.length; i++) {
        marker = new google.maps.Marker({
            position: stops[i],
            map: map,
            title: "Stop Coords: " + stops[i],
            stopCoords: stops[i]
        });
        var len = stops.length;
        popupDirections(marker, len, stops);
    }
    

    Then, in the popupDirections function, refer back to it (UPDATE or use marker.position instead. But the point is, you can make stopCoords be any value you wish).

        for (var i = 0; i < len; i++) {
            infowindow.setContent("Stop coords: " + marker.stopCoords);
        } 
    

    DEMO http://jsfiddle.net/yV6xv/580/

    I’ll throw some buzzwords to explain why your original code doesn’t work: has to do with variable scope and closures.

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.