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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:47:53+00:00 2026-06-15T07:47:53+00:00

Possible Duplicate: Google Maps API Multiple Markers with Infowindows I am creating a Google

  • 0

Possible Duplicate:
Google Maps API Multiple Markers with Infowindows

I am creating a Google Maps for a school project. Being a JavaScript newb, I got the code from a tutorial I found online. My problem is that I have an array of markers and multiple info windows, but whenever you click on a marker (doesn’t matter which one), the info window only shows the content in the last element. Now I know this problem has been mentioned many times, unfortunately, I was unable to take the solutions mentioned in the other posts and apply it to my work.

here a is a link to the map:
link to google maps

please let me know how to fix my problem. thanks.

<html>
<head>
 <style>
    body { font-family: Helvetica; }
    .map-content h3 { margin: 0; padding: 5px 0 0 0; }
 </style>
 <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true"/> 
 <script>
    // Set the Map variable
    var map;

    function initialize() {
        var myOptions = {
            zoom: 9,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var all = [
            ["Location 1", "Summerdale Rd", "Elon", "NC",
            "27253", "36.150491", "-79.5470544"],
            ["Location 2", "7205 Olmstead Dr", "Burlington", "NC",
            "27215", "36.069974", "-79.548101"],
            ["Location 3", "W Market St", "Graham", "NC",
            "27253", "36.0722225", "-79.4016207"],
            ["Location 4", "Mt Hermon Rock Creek Rd", "Graham", "NC",
            "27253", "35.9826328", "-79.4165216"],
            ["Location 5", "415 Spring Garden St", "Greensboro", "NC",
            "27401", "36.06761", "-79.794984"]
            ];

        var infoWindow = new google.maps.InfoWindow;
        map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

        // Set the center of the map
        var pos = new google.maps.LatLng(36.0621881, -79.5101063);
        map.setCenter(pos);

        function infoCallback(infowindow, marker) {
            return function() {
                infowindow.open(map, marker);
            };
        }

        function setMarkers(map, all) {
            for (var i in all) {
                var name = all[i][0];
                var address = all[i][1];
                var city = all[i][2];
                var state = all[i][3];
                var zip = all[i][4];
                var lat = all[i][5];
                var lng = all[i][6];
                var latlngset;
                latlngset = new google.maps.LatLng(lat, lng);

    /*var marker = new google.maps.Marker({  
    map: map,  title: city,  position: latlngset  
    });*/

                var content = '<div class="map-content"><h3>' + name + '</h3>' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br /><a href="http://maps.google.com/?daddr=' + address + ' ' + city + ', ' + state + ' ' + zip + '" target="_blank">Get Directions</a></div>';

    /*var infowindow = new google.maps.InfoWindow();
    for(var i=0; i<all.length; i++)
    {
    infowindow.setContent(content);
    google.maps.event.addListener(
    marker, 
    'click', 
    infoCallback(infowindow, marker)
    );

    }*/

                var infowindow = new google.maps.InfoWindow();

                for (var i = 0; i < all.length; i++) {
                    var marker = new google.maps.Marker({
                        map: map,
                        title: city,
                        position: latlngset
                    });

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

        // Set all markers in the all variable
        setMarkers(map, all);
    };

    // Initializes the Google Map
    google.maps.event.addDomListener(window, 'load', initialize);
 </script>
 </head>

 <body>
    <div id="map_canvas" style="height: 100%; width: 100%;"></div>
 </body>
</html>
  • 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-15T07:47:55+00:00Added an answer on June 15, 2026 at 7:47 am

    You may try this

    function initialize() {
        var myOptions = {
            zoom: 9,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var all = [
            ["Location 1", "Summerdale Rd", "Elon", "NC",
            "27253", "36.150491", "-79.5470544"],
            ["Location 2", "7205 Olmstead Dr", "Burlington", "NC",
            "27215", "36.069974", "-79.548101"],
            ["Location 3", "W Market St", "Graham", "NC",
            "27253", "36.0722225", "-79.4016207"],
            ["Location 4", "Mt Hermon Rock Creek Rd", "Graham", "NC",
            "27253", "35.9826328", "-79.4165216"],
            ["Location 5", "415 Spring Garden St", "Greensboro", "NC",
            "27401", "36.06761", "-79.794984"]
        ];
        var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
        var infowindow = new google.maps.InfoWindow();
        for (var i = 0; i < all.length; i++) {
            var name = all[i][0], address = all[i][1], city = all[i][2],
            state = all[i][3], zip = all[i][4], lat = all[i][5], lng = all[i][6], 
            latlngset = new google.maps.LatLng(lat, lng);
            var content = '<div class="map-content"><h3>' + name + '</h3>' + address + '<br />' + city + ', ' + state + ' ' + zip + '<br /><a href="http://maps.google.com/?daddr=' + address + ' ' + city + ', ' + state + ' ' + zip + '" target="_blank">Get Directions</a></div>';
            var marker = new google.maps.Marker({
                map: map,
                title: city,
                position: latlngset
            });
            google.maps.event.addListener(marker, 'click', (function(marker, content) {
                return function() {
                    infowindow.setContent(content);
                    infowindow.open(map, marker);
                }
            })(marker, content));
        }
    
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Google Maps JS API v3 - Simple Multiple Marker Example On my
Possible Duplicate: Looping through Markers with Google Maps API v3 Problem I have a
Possible Duplicate: Google Maps API v3: How to remove all markers? I'm trying to
Possible Duplicate: Can Google Maps/Places 'autocomplete' API be used via AJAX? There seems to
Possible Duplicate: Remove marker in Google Maps Api v3 I'm trying to give users
Possible Duplicate: Android:Google Maps API Key Signup : MD5 certification key I'm tryng to
Possible Duplicate: json with google geocoding api json Uncaught SyntaxError: Unexpected token : $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true&callback=?',
Possible Duplicate: How to detect an android device whether it supports google maps API
Possible Duplicate: Google plus api for posting on wall like facebook I have checked
Possible Duplicate: Google +1 Button not W3C compliant My javascript for google plus fails

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.