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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:22:12+00:00 2026-05-16T23:22:12+00:00

im fairly new to the Google Maps V3 API and i’m running into an

  • 0

im fairly new to the Google Maps V3 API and i’m running into an issue where all infowindows have the same data, the code im using iterates through an array of marker details and outputs the marker plus an infowindow for each marker. This is my code:

    <script type="text/javascript">

(function () {
google.maps.Map.prototype.markers = new Array();
google.maps.Map.prototype.addMarker = function(marker) {
this.markers[this.markers.length] = marker;
};})();

function initialize() {

var latlng = new google.maps.LatLng(37.0691, -8.0312);

    var myOptions = {
    zoom: 12,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    var house = 'images/house.png';

    var a = new Array();

    var t =  new Object();
    t.name = "Casa De Cova"
    t.lat =  37.0691
    t.lng =  -8.0312
    t.ico = house
    t.info = 'Casa De Cova Villa'
    a[0] = t;

    var t =  new Object();
    t.name = "Casa Jaquinta"
    t.lat =  37.0450
    t.lng =  -8.0442
    t.ico = house
    t.info = 'Jaquinta Villa'
    a[1] = t;

    var t =  new Object();
    t.name = "Faro Aiport"
    t.lat =  37.016187
    t.lng =  -7.970581
    t.ico = 'images/plane.png'
    t.info = 'The main Airport'
    a[2] = t;


    for (var i = 0; i < a.length; i++) 
    {
        var latlng = new google.maps.LatLng(a[i].lat, a[i].lng);
        map.addMarker(createMarker(a[i].name,latlng,a[i].ico,a[i].info));
    }
}

var counter = 1;

function createMarker(name, latlng, icon, info) 
{
    var marker = new google.maps.Marker({position: latlng, map: map, icon: icon, title: name});

    infowindow = new google.maps.InfoWindow({            
        content: info
    });

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

    document.getElementById(counter).onclick = function() {
        infowindow.open(map, marker);
    }

    counter++;
    return marker;
}
</script>

The issue is sort of fixed if a ‘var’ declaration is made before ‘infowindow.open(map,marker);’ if this is done then i can see each window with its respective data but then when one infowindow is opened the previous one will not close.

the site is here: http://www.danhall.co.uk/algarve/map.php

I’m a bit lost, any help much appreciated.

  • 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-16T23:22:13+00:00Added an answer on May 16, 2026 at 11:22 pm

    Definitely use the var declaration for the infowindow; otherwise infowindow is global and you’re overwriting it every time you call createMarker().

    After that, you just need to close all open infowindows every time you show one. Here’s how I’ve solved this before:

    ...
    var counter = 1,
        infowindows = [];
    
    function closeInfoWindows()
    {
        var i = infowindows.length;
        while(i--)
        {
            infowindows[i].close();
        }
    }
    
    function createMarker(name, latlng, icon, info) 
    {
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            icon: icon,
            title: name
        });
    
        var infowindow = new google.maps.InfoWindow({            
            content: info
        });
    
        infowindows.push(infowindow);
    
        google.maps.event.addListener(marker, 'click', function ()
        {
            infowindow.open(map,marker);
        });
    
        document.getElementById(counter).onclick = function ()
        {
            infowindow.open(map, marker);
        }
    
        counter++;
        return marker;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.