Google Maps: Auto close open InfoWindows?












97















On my site, I'm using Google Maps API v3 to place house markers on the map.



The InfoWindows stay open unless you explicitly click the close icon. Meaning, you can have 2+ InfoWindows open at a time if you hover over the map marker.



Question: How do I make it so that only the current active InfoWindow is open and all other InfoWindows are closed? Meaning, no more than 1 InfoWindow will be open at a time?










share|improve this question

























  • As for me it's better to create just one infowindow and update it (it's content and etc), open and close and eveything. But I am pretty sure this approach isn't always applicable.

    – andrii
    Aug 21 '11 at 12:58
















97















On my site, I'm using Google Maps API v3 to place house markers on the map.



The InfoWindows stay open unless you explicitly click the close icon. Meaning, you can have 2+ InfoWindows open at a time if you hover over the map marker.



Question: How do I make it so that only the current active InfoWindow is open and all other InfoWindows are closed? Meaning, no more than 1 InfoWindow will be open at a time?










share|improve this question

























  • As for me it's better to create just one infowindow and update it (it's content and etc), open and close and eveything. But I am pretty sure this approach isn't always applicable.

    – andrii
    Aug 21 '11 at 12:58














97












97








97


26






On my site, I'm using Google Maps API v3 to place house markers on the map.



The InfoWindows stay open unless you explicitly click the close icon. Meaning, you can have 2+ InfoWindows open at a time if you hover over the map marker.



Question: How do I make it so that only the current active InfoWindow is open and all other InfoWindows are closed? Meaning, no more than 1 InfoWindow will be open at a time?










share|improve this question
















On my site, I'm using Google Maps API v3 to place house markers on the map.



The InfoWindows stay open unless you explicitly click the close icon. Meaning, you can have 2+ InfoWindows open at a time if you hover over the map marker.



Question: How do I make it so that only the current active InfoWindow is open and all other InfoWindows are closed? Meaning, no more than 1 InfoWindow will be open at a time?







javascript google-maps google-maps-api-3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 26 '11 at 15:41









Cyril Gandon

13.1k105598




13.1k105598










asked Feb 8 '10 at 17:37









TedTed

503165




503165













  • As for me it's better to create just one infowindow and update it (it's content and etc), open and close and eveything. But I am pretty sure this approach isn't always applicable.

    – andrii
    Aug 21 '11 at 12:58



















  • As for me it's better to create just one infowindow and update it (it's content and etc), open and close and eveything. But I am pretty sure this approach isn't always applicable.

    – andrii
    Aug 21 '11 at 12:58

















As for me it's better to create just one infowindow and update it (it's content and etc), open and close and eveything. But I am pretty sure this approach isn't always applicable.

– andrii
Aug 21 '11 at 12:58





As for me it's better to create just one infowindow and update it (it's content and etc), open and close and eveything. But I am pretty sure this approach isn't always applicable.

– andrii
Aug 21 '11 at 12:58












10 Answers
10






active

oldest

votes


















142














There is a close() function for InfoWindows. Just keep track of the last opened window, and call the close function on it when a new window is created.



This demo has the functionality you're looking for. I found it in the Maps API V3 demo gallery.






share|improve this answer



















  • 4





    Upvoted for the suggestion of keeping track of the last opened window only. Looks like a no-brainer, but people forget those things...

    – Rémi Breton
    Jul 19 '12 at 17:45






  • 1





    I've just used the exact same technique. Thanks Chris. It was necessary for me, because I am using an array of InfoWindow objects instead of just one that cycles through and grabs the pertinent info. Each InfoWindow has its own separately updating information, so I found this technique quite useful.

    – InterfaceGuy
    Jul 25 '12 at 21:56











  • very nice easy and good demo example

    – Anup
    Jul 9 '15 at 5:31






  • 1





    the "this demo" link is broken

    – Brendan Whiting
    Apr 13 '18 at 22:51



















56














alternative solution for this with using many infowindows:
save prev opened infowindow in a variable and then close it when new window opened



var prev_infowindow =false; 
...
base.attachInfo = function(marker, i){
var infowindow = new google.maps.InfoWindow({
content: 'yourmarkerinfocontent'
});

google.maps.event.addListener(marker, 'click', function(){
if( prev_infowindow ) {
prev_infowindow.close();
}

prev_infowindow = infowindow;
infowindow.open(base.map, marker);
});
}





share|improve this answer





















  • 2





    Like this one. Simple to understand and implement

    – Aamir Afridi
    Apr 8 '14 at 12:01






  • 4





    Forgive my naivety, but WTF is base?

    – wordsforthewise
    Apr 17 '16 at 7:15











  • I don't understand why it's not the default behavior in Google maps V3 ...

    – MrWashinton
    Jun 29 '16 at 9:10











  • Best and simplest solution I've found so far. thank you!

    – Irteza Asad
    Jul 30 '18 at 10:17





















26














//assuming you have a map called 'map'
var infowindow = new google.maps.InfoWindow();

var latlng1 = new google.maps.LatLng(0,0);
var marker1 = new google.maps.Marker({position:latlng1, map:map});
google.maps.event.addListener(marker1, 'click',
function(){
infowindow.close();//hide the infowindow
infowindow.setContent('Marker #1');//update the content for this marker
infowindow.open(map, marker1);//"move" the info window to the clicked marker and open it
}
);
var latlng2 = new google.maps.LatLng(10,10);
var marker2 = new google.maps.Marker({position:latlng2, map:map});
google.maps.event.addListener(marker2, 'click',
function(){
infowindow.close();//hide the infowindow
infowindow.setContent('Marker #2');//update the content for this marker
infowindow.open(map, marker2);//"move" the info window to the clicked marker and open it
}
);


This will "move" the info window around to each clicked marker, in effect closing itself, then reopening (and panning to fit the viewport) in its new location. It changes its contents before opening to give the desired effect. Works for n markers.






share|improve this answer





















  • 1





    Quick note: repeated calls to infowindow.open() are sufficient; the window doesn't need to be closed first.

    – Eric Nguyen
    Aug 23 '11 at 20:44






  • 3





    @Eric, while you're technically correct, I've noticed a bug that sometimes causes info windows to show up in their last position. Forcing closed first defeats said bug.

    – Joel Mellon
    May 18 '12 at 22:50



















12














My solution.



var map;
var infowindow = new google.maps.InfoWindow();
...
function createMarker(...) {
var marker = new google.maps.Marker({
...,
descrip: infowindowHtmlContent
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setOptions({
content: this.descrip,
maxWidth:300
});
infowindow.open(map, marker);
});





share|improve this answer





















  • 2





    That's really elegant - only using a single infowindow and changing the content avoids having to keep track of / close the previous one.

    – Nick F
    Jun 27 '14 at 11:28











  • This solution is very elegant indeed and works like a charm. +1

    – Sebastian Breit
    May 25 '16 at 18:02











  • Very good solution !!! Thanks!

    – SachaDee
    Nov 19 '18 at 14:38



















9














From this link http://www.svennerberg.com/2009/09/google-maps-api-3-infowindows/:




Teo: The easiest way to do this is to
just have one instance of the
InfoWindow object that you reuse over
and over again. That way when you
click a new marker the infoWindow is
“moved” from where it’s currently at,
to point at the new marker.



Use its setContent method to load it
with the correct content.







share|improve this answer
























  • I don't believe this applies since I'm using Google Maps v3 API

    – Ted
    Feb 8 '10 at 17:40











  • Plus, the article you linked too does not demo more than 1 marker

    – Ted
    Feb 8 '10 at 17:42











  • I've used a single infowindow in the same fashion for several sites. Click one, the open one closes automatically.

    – Keith Adler
    Feb 8 '10 at 18:04






  • 3





    How do you associate multiple marks with a single InfoWindow?

    – Ted
    Feb 8 '10 at 18:13



















5














There is a easier way besides using the close() function. if you create a variable with the InfoWindow property it closes automatically when you open another.



var info_window;
var map;
var chicago = new google.maps.LatLng(33.84659, -84.35686);

function initialize() {
var mapOptions = {
center: chicago,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

info_window = new google.maps.InfoWindow({
content: 'loading'
)};

createLocationOnMap('Location Name 1', new google.maps.LatLng(33.84659, -84.35686), '<p><strong>Location Name 1</strong><br/>Address 1</p>');
createLocationOnMap('Location Name 2', new google.maps.LatLng(33.84625, -84.36212), '<p><strong>Location Name 1</strong><br/>Address 2</p>');

}

function createLocationOnMap(titulo, posicao, conteudo) {
var m = new google.maps.Marker({
map: map,
animation: google.maps.Animation.DROP,
title: titulo,
position: posicao,
html: conteudo
});

google.maps.event.addListener(m, 'click', function () {
info_window.setContent(this.html);
info_window.open(map, this);
});
}





share|improve this answer



















  • 1





    This worked well for me

    – ollie
    Sep 25 '12 at 16:00



















2














How about -



google.maps.event.addListener(yourMarker, 'mouseover', function () {
yourInfoWindow.open(yourMap, yourMarker);

});

google.maps.event.addListener(yourMarker, 'mouseout', function () {
yourInfoWindow.open(yourMap, yourMarker);

});


Then you can just hover over it and it will close itself.






share|improve this answer

































    1














    var map;
    var infowindow;
    ...
    function createMarker(...) {
    var marker = new google.maps.Marker({...});
    google.maps.event.addListener(marker, 'click', function() {
    ...
    if (infowindow) {
    infowindow.close();
    };
    infowindow = new google.maps.InfoWindow({
    content: contentString,
    maxWidth: 300
    });
    infowindow.open(map, marker);
    }
    ...
    function initialize() {
    ...
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    ...
    google.maps.event.addListener(map, 'click', function(event) {
    if (infowindow) {
    infowindow.close();
    };
    ...
    }
    }





    share|improve this answer
























    • Thanx, I needed to close the infowindow when not clicked a marker so just on the map

      – VRC
      Dec 5 '12 at 10:46



















    1














    I stored a variable at the top to keep track of which info window is currently open, see below.



    var currentInfoWin = null;
    google.maps.event.addListener(markers[counter], 'click', function() {
    if (currentInfoWin !== null) {
    currentInfoWin.close(map, this);
    }
    this.infoWin.open(map, this);
    currentInfoWin = this.infoWin;
    });





    share|improve this answer































      -1














      var contentString = "Location: " + results[1].formatted_address;    
      google.maps.event.addListener(marker,'click', (function(){
      infowindow.close();
      infowindow = new google.maps.InfoWindow({
      content: contentString
      });
      infowindow.open(map, marker);
      }));





      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2223574%2fgoogle-maps-auto-close-open-infowindows%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        10 Answers
        10






        active

        oldest

        votes








        10 Answers
        10






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        142














        There is a close() function for InfoWindows. Just keep track of the last opened window, and call the close function on it when a new window is created.



        This demo has the functionality you're looking for. I found it in the Maps API V3 demo gallery.






        share|improve this answer



















        • 4





          Upvoted for the suggestion of keeping track of the last opened window only. Looks like a no-brainer, but people forget those things...

          – Rémi Breton
          Jul 19 '12 at 17:45






        • 1





          I've just used the exact same technique. Thanks Chris. It was necessary for me, because I am using an array of InfoWindow objects instead of just one that cycles through and grabs the pertinent info. Each InfoWindow has its own separately updating information, so I found this technique quite useful.

          – InterfaceGuy
          Jul 25 '12 at 21:56











        • very nice easy and good demo example

          – Anup
          Jul 9 '15 at 5:31






        • 1





          the "this demo" link is broken

          – Brendan Whiting
          Apr 13 '18 at 22:51
















        142














        There is a close() function for InfoWindows. Just keep track of the last opened window, and call the close function on it when a new window is created.



        This demo has the functionality you're looking for. I found it in the Maps API V3 demo gallery.






        share|improve this answer



















        • 4





          Upvoted for the suggestion of keeping track of the last opened window only. Looks like a no-brainer, but people forget those things...

          – Rémi Breton
          Jul 19 '12 at 17:45






        • 1





          I've just used the exact same technique. Thanks Chris. It was necessary for me, because I am using an array of InfoWindow objects instead of just one that cycles through and grabs the pertinent info. Each InfoWindow has its own separately updating information, so I found this technique quite useful.

          – InterfaceGuy
          Jul 25 '12 at 21:56











        • very nice easy and good demo example

          – Anup
          Jul 9 '15 at 5:31






        • 1





          the "this demo" link is broken

          – Brendan Whiting
          Apr 13 '18 at 22:51














        142












        142








        142







        There is a close() function for InfoWindows. Just keep track of the last opened window, and call the close function on it when a new window is created.



        This demo has the functionality you're looking for. I found it in the Maps API V3 demo gallery.






        share|improve this answer













        There is a close() function for InfoWindows. Just keep track of the last opened window, and call the close function on it when a new window is created.



        This demo has the functionality you're looking for. I found it in the Maps API V3 demo gallery.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 8 '10 at 18:54









        Chris BChris B

        12.6k52938




        12.6k52938








        • 4





          Upvoted for the suggestion of keeping track of the last opened window only. Looks like a no-brainer, but people forget those things...

          – Rémi Breton
          Jul 19 '12 at 17:45






        • 1





          I've just used the exact same technique. Thanks Chris. It was necessary for me, because I am using an array of InfoWindow objects instead of just one that cycles through and grabs the pertinent info. Each InfoWindow has its own separately updating information, so I found this technique quite useful.

          – InterfaceGuy
          Jul 25 '12 at 21:56











        • very nice easy and good demo example

          – Anup
          Jul 9 '15 at 5:31






        • 1





          the "this demo" link is broken

          – Brendan Whiting
          Apr 13 '18 at 22:51














        • 4





          Upvoted for the suggestion of keeping track of the last opened window only. Looks like a no-brainer, but people forget those things...

          – Rémi Breton
          Jul 19 '12 at 17:45






        • 1





          I've just used the exact same technique. Thanks Chris. It was necessary for me, because I am using an array of InfoWindow objects instead of just one that cycles through and grabs the pertinent info. Each InfoWindow has its own separately updating information, so I found this technique quite useful.

          – InterfaceGuy
          Jul 25 '12 at 21:56











        • very nice easy and good demo example

          – Anup
          Jul 9 '15 at 5:31






        • 1





          the "this demo" link is broken

          – Brendan Whiting
          Apr 13 '18 at 22:51








        4




        4





        Upvoted for the suggestion of keeping track of the last opened window only. Looks like a no-brainer, but people forget those things...

        – Rémi Breton
        Jul 19 '12 at 17:45





        Upvoted for the suggestion of keeping track of the last opened window only. Looks like a no-brainer, but people forget those things...

        – Rémi Breton
        Jul 19 '12 at 17:45




        1




        1





        I've just used the exact same technique. Thanks Chris. It was necessary for me, because I am using an array of InfoWindow objects instead of just one that cycles through and grabs the pertinent info. Each InfoWindow has its own separately updating information, so I found this technique quite useful.

        – InterfaceGuy
        Jul 25 '12 at 21:56





        I've just used the exact same technique. Thanks Chris. It was necessary for me, because I am using an array of InfoWindow objects instead of just one that cycles through and grabs the pertinent info. Each InfoWindow has its own separately updating information, so I found this technique quite useful.

        – InterfaceGuy
        Jul 25 '12 at 21:56













        very nice easy and good demo example

        – Anup
        Jul 9 '15 at 5:31





        very nice easy and good demo example

        – Anup
        Jul 9 '15 at 5:31




        1




        1





        the "this demo" link is broken

        – Brendan Whiting
        Apr 13 '18 at 22:51





        the "this demo" link is broken

        – Brendan Whiting
        Apr 13 '18 at 22:51













        56














        alternative solution for this with using many infowindows:
        save prev opened infowindow in a variable and then close it when new window opened



        var prev_infowindow =false; 
        ...
        base.attachInfo = function(marker, i){
        var infowindow = new google.maps.InfoWindow({
        content: 'yourmarkerinfocontent'
        });

        google.maps.event.addListener(marker, 'click', function(){
        if( prev_infowindow ) {
        prev_infowindow.close();
        }

        prev_infowindow = infowindow;
        infowindow.open(base.map, marker);
        });
        }





        share|improve this answer





















        • 2





          Like this one. Simple to understand and implement

          – Aamir Afridi
          Apr 8 '14 at 12:01






        • 4





          Forgive my naivety, but WTF is base?

          – wordsforthewise
          Apr 17 '16 at 7:15











        • I don't understand why it's not the default behavior in Google maps V3 ...

          – MrWashinton
          Jun 29 '16 at 9:10











        • Best and simplest solution I've found so far. thank you!

          – Irteza Asad
          Jul 30 '18 at 10:17


















        56














        alternative solution for this with using many infowindows:
        save prev opened infowindow in a variable and then close it when new window opened



        var prev_infowindow =false; 
        ...
        base.attachInfo = function(marker, i){
        var infowindow = new google.maps.InfoWindow({
        content: 'yourmarkerinfocontent'
        });

        google.maps.event.addListener(marker, 'click', function(){
        if( prev_infowindow ) {
        prev_infowindow.close();
        }

        prev_infowindow = infowindow;
        infowindow.open(base.map, marker);
        });
        }





        share|improve this answer





















        • 2





          Like this one. Simple to understand and implement

          – Aamir Afridi
          Apr 8 '14 at 12:01






        • 4





          Forgive my naivety, but WTF is base?

          – wordsforthewise
          Apr 17 '16 at 7:15











        • I don't understand why it's not the default behavior in Google maps V3 ...

          – MrWashinton
          Jun 29 '16 at 9:10











        • Best and simplest solution I've found so far. thank you!

          – Irteza Asad
          Jul 30 '18 at 10:17
















        56












        56








        56







        alternative solution for this with using many infowindows:
        save prev opened infowindow in a variable and then close it when new window opened



        var prev_infowindow =false; 
        ...
        base.attachInfo = function(marker, i){
        var infowindow = new google.maps.InfoWindow({
        content: 'yourmarkerinfocontent'
        });

        google.maps.event.addListener(marker, 'click', function(){
        if( prev_infowindow ) {
        prev_infowindow.close();
        }

        prev_infowindow = infowindow;
        infowindow.open(base.map, marker);
        });
        }





        share|improve this answer















        alternative solution for this with using many infowindows:
        save prev opened infowindow in a variable and then close it when new window opened



        var prev_infowindow =false; 
        ...
        base.attachInfo = function(marker, i){
        var infowindow = new google.maps.InfoWindow({
        content: 'yourmarkerinfocontent'
        });

        google.maps.event.addListener(marker, 'click', function(){
        if( prev_infowindow ) {
        prev_infowindow.close();
        }

        prev_infowindow = infowindow;
        infowindow.open(base.map, marker);
        });
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Apr 8 '14 at 12:01









        Aamir Afridi

        5,61333637




        5,61333637










        answered Feb 28 '12 at 11:55









        mikemike

        56142




        56142








        • 2





          Like this one. Simple to understand and implement

          – Aamir Afridi
          Apr 8 '14 at 12:01






        • 4





          Forgive my naivety, but WTF is base?

          – wordsforthewise
          Apr 17 '16 at 7:15











        • I don't understand why it's not the default behavior in Google maps V3 ...

          – MrWashinton
          Jun 29 '16 at 9:10











        • Best and simplest solution I've found so far. thank you!

          – Irteza Asad
          Jul 30 '18 at 10:17
















        • 2





          Like this one. Simple to understand and implement

          – Aamir Afridi
          Apr 8 '14 at 12:01






        • 4





          Forgive my naivety, but WTF is base?

          – wordsforthewise
          Apr 17 '16 at 7:15











        • I don't understand why it's not the default behavior in Google maps V3 ...

          – MrWashinton
          Jun 29 '16 at 9:10











        • Best and simplest solution I've found so far. thank you!

          – Irteza Asad
          Jul 30 '18 at 10:17










        2




        2





        Like this one. Simple to understand and implement

        – Aamir Afridi
        Apr 8 '14 at 12:01





        Like this one. Simple to understand and implement

        – Aamir Afridi
        Apr 8 '14 at 12:01




        4




        4





        Forgive my naivety, but WTF is base?

        – wordsforthewise
        Apr 17 '16 at 7:15





        Forgive my naivety, but WTF is base?

        – wordsforthewise
        Apr 17 '16 at 7:15













        I don't understand why it's not the default behavior in Google maps V3 ...

        – MrWashinton
        Jun 29 '16 at 9:10





        I don't understand why it's not the default behavior in Google maps V3 ...

        – MrWashinton
        Jun 29 '16 at 9:10













        Best and simplest solution I've found so far. thank you!

        – Irteza Asad
        Jul 30 '18 at 10:17







        Best and simplest solution I've found so far. thank you!

        – Irteza Asad
        Jul 30 '18 at 10:17













        26














        //assuming you have a map called 'map'
        var infowindow = new google.maps.InfoWindow();

        var latlng1 = new google.maps.LatLng(0,0);
        var marker1 = new google.maps.Marker({position:latlng1, map:map});
        google.maps.event.addListener(marker1, 'click',
        function(){
        infowindow.close();//hide the infowindow
        infowindow.setContent('Marker #1');//update the content for this marker
        infowindow.open(map, marker1);//"move" the info window to the clicked marker and open it
        }
        );
        var latlng2 = new google.maps.LatLng(10,10);
        var marker2 = new google.maps.Marker({position:latlng2, map:map});
        google.maps.event.addListener(marker2, 'click',
        function(){
        infowindow.close();//hide the infowindow
        infowindow.setContent('Marker #2');//update the content for this marker
        infowindow.open(map, marker2);//"move" the info window to the clicked marker and open it
        }
        );


        This will "move" the info window around to each clicked marker, in effect closing itself, then reopening (and panning to fit the viewport) in its new location. It changes its contents before opening to give the desired effect. Works for n markers.






        share|improve this answer





















        • 1





          Quick note: repeated calls to infowindow.open() are sufficient; the window doesn't need to be closed first.

          – Eric Nguyen
          Aug 23 '11 at 20:44






        • 3





          @Eric, while you're technically correct, I've noticed a bug that sometimes causes info windows to show up in their last position. Forcing closed first defeats said bug.

          – Joel Mellon
          May 18 '12 at 22:50
















        26














        //assuming you have a map called 'map'
        var infowindow = new google.maps.InfoWindow();

        var latlng1 = new google.maps.LatLng(0,0);
        var marker1 = new google.maps.Marker({position:latlng1, map:map});
        google.maps.event.addListener(marker1, 'click',
        function(){
        infowindow.close();//hide the infowindow
        infowindow.setContent('Marker #1');//update the content for this marker
        infowindow.open(map, marker1);//"move" the info window to the clicked marker and open it
        }
        );
        var latlng2 = new google.maps.LatLng(10,10);
        var marker2 = new google.maps.Marker({position:latlng2, map:map});
        google.maps.event.addListener(marker2, 'click',
        function(){
        infowindow.close();//hide the infowindow
        infowindow.setContent('Marker #2');//update the content for this marker
        infowindow.open(map, marker2);//"move" the info window to the clicked marker and open it
        }
        );


        This will "move" the info window around to each clicked marker, in effect closing itself, then reopening (and panning to fit the viewport) in its new location. It changes its contents before opening to give the desired effect. Works for n markers.






        share|improve this answer





















        • 1





          Quick note: repeated calls to infowindow.open() are sufficient; the window doesn't need to be closed first.

          – Eric Nguyen
          Aug 23 '11 at 20:44






        • 3





          @Eric, while you're technically correct, I've noticed a bug that sometimes causes info windows to show up in their last position. Forcing closed first defeats said bug.

          – Joel Mellon
          May 18 '12 at 22:50














        26












        26








        26







        //assuming you have a map called 'map'
        var infowindow = new google.maps.InfoWindow();

        var latlng1 = new google.maps.LatLng(0,0);
        var marker1 = new google.maps.Marker({position:latlng1, map:map});
        google.maps.event.addListener(marker1, 'click',
        function(){
        infowindow.close();//hide the infowindow
        infowindow.setContent('Marker #1');//update the content for this marker
        infowindow.open(map, marker1);//"move" the info window to the clicked marker and open it
        }
        );
        var latlng2 = new google.maps.LatLng(10,10);
        var marker2 = new google.maps.Marker({position:latlng2, map:map});
        google.maps.event.addListener(marker2, 'click',
        function(){
        infowindow.close();//hide the infowindow
        infowindow.setContent('Marker #2');//update the content for this marker
        infowindow.open(map, marker2);//"move" the info window to the clicked marker and open it
        }
        );


        This will "move" the info window around to each clicked marker, in effect closing itself, then reopening (and panning to fit the viewport) in its new location. It changes its contents before opening to give the desired effect. Works for n markers.






        share|improve this answer















        //assuming you have a map called 'map'
        var infowindow = new google.maps.InfoWindow();

        var latlng1 = new google.maps.LatLng(0,0);
        var marker1 = new google.maps.Marker({position:latlng1, map:map});
        google.maps.event.addListener(marker1, 'click',
        function(){
        infowindow.close();//hide the infowindow
        infowindow.setContent('Marker #1');//update the content for this marker
        infowindow.open(map, marker1);//"move" the info window to the clicked marker and open it
        }
        );
        var latlng2 = new google.maps.LatLng(10,10);
        var marker2 = new google.maps.Marker({position:latlng2, map:map});
        google.maps.event.addListener(marker2, 'click',
        function(){
        infowindow.close();//hide the infowindow
        infowindow.setContent('Marker #2');//update the content for this marker
        infowindow.open(map, marker2);//"move" the info window to the clicked marker and open it
        }
        );


        This will "move" the info window around to each clicked marker, in effect closing itself, then reopening (and panning to fit the viewport) in its new location. It changes its contents before opening to give the desired effect. Works for n markers.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Oct 25 '10 at 20:53

























        answered Sep 18 '10 at 23:56









        Joel MellonJoel Mellon

        2,39711819




        2,39711819








        • 1





          Quick note: repeated calls to infowindow.open() are sufficient; the window doesn't need to be closed first.

          – Eric Nguyen
          Aug 23 '11 at 20:44






        • 3





          @Eric, while you're technically correct, I've noticed a bug that sometimes causes info windows to show up in their last position. Forcing closed first defeats said bug.

          – Joel Mellon
          May 18 '12 at 22:50














        • 1





          Quick note: repeated calls to infowindow.open() are sufficient; the window doesn't need to be closed first.

          – Eric Nguyen
          Aug 23 '11 at 20:44






        • 3





          @Eric, while you're technically correct, I've noticed a bug that sometimes causes info windows to show up in their last position. Forcing closed first defeats said bug.

          – Joel Mellon
          May 18 '12 at 22:50








        1




        1





        Quick note: repeated calls to infowindow.open() are sufficient; the window doesn't need to be closed first.

        – Eric Nguyen
        Aug 23 '11 at 20:44





        Quick note: repeated calls to infowindow.open() are sufficient; the window doesn't need to be closed first.

        – Eric Nguyen
        Aug 23 '11 at 20:44




        3




        3





        @Eric, while you're technically correct, I've noticed a bug that sometimes causes info windows to show up in their last position. Forcing closed first defeats said bug.

        – Joel Mellon
        May 18 '12 at 22:50





        @Eric, while you're technically correct, I've noticed a bug that sometimes causes info windows to show up in their last position. Forcing closed first defeats said bug.

        – Joel Mellon
        May 18 '12 at 22:50











        12














        My solution.



        var map;
        var infowindow = new google.maps.InfoWindow();
        ...
        function createMarker(...) {
        var marker = new google.maps.Marker({
        ...,
        descrip: infowindowHtmlContent
        });
        google.maps.event.addListener(marker, 'click', function() {
        infowindow.setOptions({
        content: this.descrip,
        maxWidth:300
        });
        infowindow.open(map, marker);
        });





        share|improve this answer





















        • 2





          That's really elegant - only using a single infowindow and changing the content avoids having to keep track of / close the previous one.

          – Nick F
          Jun 27 '14 at 11:28











        • This solution is very elegant indeed and works like a charm. +1

          – Sebastian Breit
          May 25 '16 at 18:02











        • Very good solution !!! Thanks!

          – SachaDee
          Nov 19 '18 at 14:38
















        12














        My solution.



        var map;
        var infowindow = new google.maps.InfoWindow();
        ...
        function createMarker(...) {
        var marker = new google.maps.Marker({
        ...,
        descrip: infowindowHtmlContent
        });
        google.maps.event.addListener(marker, 'click', function() {
        infowindow.setOptions({
        content: this.descrip,
        maxWidth:300
        });
        infowindow.open(map, marker);
        });





        share|improve this answer





















        • 2





          That's really elegant - only using a single infowindow and changing the content avoids having to keep track of / close the previous one.

          – Nick F
          Jun 27 '14 at 11:28











        • This solution is very elegant indeed and works like a charm. +1

          – Sebastian Breit
          May 25 '16 at 18:02











        • Very good solution !!! Thanks!

          – SachaDee
          Nov 19 '18 at 14:38














        12












        12








        12







        My solution.



        var map;
        var infowindow = new google.maps.InfoWindow();
        ...
        function createMarker(...) {
        var marker = new google.maps.Marker({
        ...,
        descrip: infowindowHtmlContent
        });
        google.maps.event.addListener(marker, 'click', function() {
        infowindow.setOptions({
        content: this.descrip,
        maxWidth:300
        });
        infowindow.open(map, marker);
        });





        share|improve this answer















        My solution.



        var map;
        var infowindow = new google.maps.InfoWindow();
        ...
        function createMarker(...) {
        var marker = new google.maps.Marker({
        ...,
        descrip: infowindowHtmlContent
        });
        google.maps.event.addListener(marker, 'click', function() {
        infowindow.setOptions({
        content: this.descrip,
        maxWidth:300
        });
        infowindow.open(map, marker);
        });






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 '18 at 14:37









        SachaDee

        8,12321527




        8,12321527










        answered Nov 8 '13 at 10:22









        kaidojkaidoj

        12325




        12325








        • 2





          That's really elegant - only using a single infowindow and changing the content avoids having to keep track of / close the previous one.

          – Nick F
          Jun 27 '14 at 11:28











        • This solution is very elegant indeed and works like a charm. +1

          – Sebastian Breit
          May 25 '16 at 18:02











        • Very good solution !!! Thanks!

          – SachaDee
          Nov 19 '18 at 14:38














        • 2





          That's really elegant - only using a single infowindow and changing the content avoids having to keep track of / close the previous one.

          – Nick F
          Jun 27 '14 at 11:28











        • This solution is very elegant indeed and works like a charm. +1

          – Sebastian Breit
          May 25 '16 at 18:02











        • Very good solution !!! Thanks!

          – SachaDee
          Nov 19 '18 at 14:38








        2




        2





        That's really elegant - only using a single infowindow and changing the content avoids having to keep track of / close the previous one.

        – Nick F
        Jun 27 '14 at 11:28





        That's really elegant - only using a single infowindow and changing the content avoids having to keep track of / close the previous one.

        – Nick F
        Jun 27 '14 at 11:28













        This solution is very elegant indeed and works like a charm. +1

        – Sebastian Breit
        May 25 '16 at 18:02





        This solution is very elegant indeed and works like a charm. +1

        – Sebastian Breit
        May 25 '16 at 18:02













        Very good solution !!! Thanks!

        – SachaDee
        Nov 19 '18 at 14:38





        Very good solution !!! Thanks!

        – SachaDee
        Nov 19 '18 at 14:38











        9














        From this link http://www.svennerberg.com/2009/09/google-maps-api-3-infowindows/:




        Teo: The easiest way to do this is to
        just have one instance of the
        InfoWindow object that you reuse over
        and over again. That way when you
        click a new marker the infoWindow is
        “moved” from where it’s currently at,
        to point at the new marker.



        Use its setContent method to load it
        with the correct content.







        share|improve this answer
























        • I don't believe this applies since I'm using Google Maps v3 API

          – Ted
          Feb 8 '10 at 17:40











        • Plus, the article you linked too does not demo more than 1 marker

          – Ted
          Feb 8 '10 at 17:42











        • I've used a single infowindow in the same fashion for several sites. Click one, the open one closes automatically.

          – Keith Adler
          Feb 8 '10 at 18:04






        • 3





          How do you associate multiple marks with a single InfoWindow?

          – Ted
          Feb 8 '10 at 18:13
















        9














        From this link http://www.svennerberg.com/2009/09/google-maps-api-3-infowindows/:




        Teo: The easiest way to do this is to
        just have one instance of the
        InfoWindow object that you reuse over
        and over again. That way when you
        click a new marker the infoWindow is
        “moved” from where it’s currently at,
        to point at the new marker.



        Use its setContent method to load it
        with the correct content.







        share|improve this answer
























        • I don't believe this applies since I'm using Google Maps v3 API

          – Ted
          Feb 8 '10 at 17:40











        • Plus, the article you linked too does not demo more than 1 marker

          – Ted
          Feb 8 '10 at 17:42











        • I've used a single infowindow in the same fashion for several sites. Click one, the open one closes automatically.

          – Keith Adler
          Feb 8 '10 at 18:04






        • 3





          How do you associate multiple marks with a single InfoWindow?

          – Ted
          Feb 8 '10 at 18:13














        9












        9








        9







        From this link http://www.svennerberg.com/2009/09/google-maps-api-3-infowindows/:




        Teo: The easiest way to do this is to
        just have one instance of the
        InfoWindow object that you reuse over
        and over again. That way when you
        click a new marker the infoWindow is
        “moved” from where it’s currently at,
        to point at the new marker.



        Use its setContent method to load it
        with the correct content.







        share|improve this answer













        From this link http://www.svennerberg.com/2009/09/google-maps-api-3-infowindows/:




        Teo: The easiest way to do this is to
        just have one instance of the
        InfoWindow object that you reuse over
        and over again. That way when you
        click a new marker the infoWindow is
        “moved” from where it’s currently at,
        to point at the new marker.



        Use its setContent method to load it
        with the correct content.








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 8 '10 at 17:40









        Keith AdlerKeith Adler

        14.3k18104176




        14.3k18104176













        • I don't believe this applies since I'm using Google Maps v3 API

          – Ted
          Feb 8 '10 at 17:40











        • Plus, the article you linked too does not demo more than 1 marker

          – Ted
          Feb 8 '10 at 17:42











        • I've used a single infowindow in the same fashion for several sites. Click one, the open one closes automatically.

          – Keith Adler
          Feb 8 '10 at 18:04






        • 3





          How do you associate multiple marks with a single InfoWindow?

          – Ted
          Feb 8 '10 at 18:13



















        • I don't believe this applies since I'm using Google Maps v3 API

          – Ted
          Feb 8 '10 at 17:40











        • Plus, the article you linked too does not demo more than 1 marker

          – Ted
          Feb 8 '10 at 17:42











        • I've used a single infowindow in the same fashion for several sites. Click one, the open one closes automatically.

          – Keith Adler
          Feb 8 '10 at 18:04






        • 3





          How do you associate multiple marks with a single InfoWindow?

          – Ted
          Feb 8 '10 at 18:13

















        I don't believe this applies since I'm using Google Maps v3 API

        – Ted
        Feb 8 '10 at 17:40





        I don't believe this applies since I'm using Google Maps v3 API

        – Ted
        Feb 8 '10 at 17:40













        Plus, the article you linked too does not demo more than 1 marker

        – Ted
        Feb 8 '10 at 17:42





        Plus, the article you linked too does not demo more than 1 marker

        – Ted
        Feb 8 '10 at 17:42













        I've used a single infowindow in the same fashion for several sites. Click one, the open one closes automatically.

        – Keith Adler
        Feb 8 '10 at 18:04





        I've used a single infowindow in the same fashion for several sites. Click one, the open one closes automatically.

        – Keith Adler
        Feb 8 '10 at 18:04




        3




        3





        How do you associate multiple marks with a single InfoWindow?

        – Ted
        Feb 8 '10 at 18:13





        How do you associate multiple marks with a single InfoWindow?

        – Ted
        Feb 8 '10 at 18:13











        5














        There is a easier way besides using the close() function. if you create a variable with the InfoWindow property it closes automatically when you open another.



        var info_window;
        var map;
        var chicago = new google.maps.LatLng(33.84659, -84.35686);

        function initialize() {
        var mapOptions = {
        center: chicago,
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        info_window = new google.maps.InfoWindow({
        content: 'loading'
        )};

        createLocationOnMap('Location Name 1', new google.maps.LatLng(33.84659, -84.35686), '<p><strong>Location Name 1</strong><br/>Address 1</p>');
        createLocationOnMap('Location Name 2', new google.maps.LatLng(33.84625, -84.36212), '<p><strong>Location Name 1</strong><br/>Address 2</p>');

        }

        function createLocationOnMap(titulo, posicao, conteudo) {
        var m = new google.maps.Marker({
        map: map,
        animation: google.maps.Animation.DROP,
        title: titulo,
        position: posicao,
        html: conteudo
        });

        google.maps.event.addListener(m, 'click', function () {
        info_window.setContent(this.html);
        info_window.open(map, this);
        });
        }





        share|improve this answer



















        • 1





          This worked well for me

          – ollie
          Sep 25 '12 at 16:00
















        5














        There is a easier way besides using the close() function. if you create a variable with the InfoWindow property it closes automatically when you open another.



        var info_window;
        var map;
        var chicago = new google.maps.LatLng(33.84659, -84.35686);

        function initialize() {
        var mapOptions = {
        center: chicago,
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        info_window = new google.maps.InfoWindow({
        content: 'loading'
        )};

        createLocationOnMap('Location Name 1', new google.maps.LatLng(33.84659, -84.35686), '<p><strong>Location Name 1</strong><br/>Address 1</p>');
        createLocationOnMap('Location Name 2', new google.maps.LatLng(33.84625, -84.36212), '<p><strong>Location Name 1</strong><br/>Address 2</p>');

        }

        function createLocationOnMap(titulo, posicao, conteudo) {
        var m = new google.maps.Marker({
        map: map,
        animation: google.maps.Animation.DROP,
        title: titulo,
        position: posicao,
        html: conteudo
        });

        google.maps.event.addListener(m, 'click', function () {
        info_window.setContent(this.html);
        info_window.open(map, this);
        });
        }





        share|improve this answer



















        • 1





          This worked well for me

          – ollie
          Sep 25 '12 at 16:00














        5












        5








        5







        There is a easier way besides using the close() function. if you create a variable with the InfoWindow property it closes automatically when you open another.



        var info_window;
        var map;
        var chicago = new google.maps.LatLng(33.84659, -84.35686);

        function initialize() {
        var mapOptions = {
        center: chicago,
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        info_window = new google.maps.InfoWindow({
        content: 'loading'
        )};

        createLocationOnMap('Location Name 1', new google.maps.LatLng(33.84659, -84.35686), '<p><strong>Location Name 1</strong><br/>Address 1</p>');
        createLocationOnMap('Location Name 2', new google.maps.LatLng(33.84625, -84.36212), '<p><strong>Location Name 1</strong><br/>Address 2</p>');

        }

        function createLocationOnMap(titulo, posicao, conteudo) {
        var m = new google.maps.Marker({
        map: map,
        animation: google.maps.Animation.DROP,
        title: titulo,
        position: posicao,
        html: conteudo
        });

        google.maps.event.addListener(m, 'click', function () {
        info_window.setContent(this.html);
        info_window.open(map, this);
        });
        }





        share|improve this answer













        There is a easier way besides using the close() function. if you create a variable with the InfoWindow property it closes automatically when you open another.



        var info_window;
        var map;
        var chicago = new google.maps.LatLng(33.84659, -84.35686);

        function initialize() {
        var mapOptions = {
        center: chicago,
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        info_window = new google.maps.InfoWindow({
        content: 'loading'
        )};

        createLocationOnMap('Location Name 1', new google.maps.LatLng(33.84659, -84.35686), '<p><strong>Location Name 1</strong><br/>Address 1</p>');
        createLocationOnMap('Location Name 2', new google.maps.LatLng(33.84625, -84.36212), '<p><strong>Location Name 1</strong><br/>Address 2</p>');

        }

        function createLocationOnMap(titulo, posicao, conteudo) {
        var m = new google.maps.Marker({
        map: map,
        animation: google.maps.Animation.DROP,
        title: titulo,
        position: posicao,
        html: conteudo
        });

        google.maps.event.addListener(m, 'click', function () {
        info_window.setContent(this.html);
        info_window.open(map, this);
        });
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 13 '12 at 13:22









        MelloGMelloG

        7781911




        7781911








        • 1





          This worked well for me

          – ollie
          Sep 25 '12 at 16:00














        • 1





          This worked well for me

          – ollie
          Sep 25 '12 at 16:00








        1




        1





        This worked well for me

        – ollie
        Sep 25 '12 at 16:00





        This worked well for me

        – ollie
        Sep 25 '12 at 16:00











        2














        How about -



        google.maps.event.addListener(yourMarker, 'mouseover', function () {
        yourInfoWindow.open(yourMap, yourMarker);

        });

        google.maps.event.addListener(yourMarker, 'mouseout', function () {
        yourInfoWindow.open(yourMap, yourMarker);

        });


        Then you can just hover over it and it will close itself.






        share|improve this answer






























          2














          How about -



          google.maps.event.addListener(yourMarker, 'mouseover', function () {
          yourInfoWindow.open(yourMap, yourMarker);

          });

          google.maps.event.addListener(yourMarker, 'mouseout', function () {
          yourInfoWindow.open(yourMap, yourMarker);

          });


          Then you can just hover over it and it will close itself.






          share|improve this answer




























            2












            2








            2







            How about -



            google.maps.event.addListener(yourMarker, 'mouseover', function () {
            yourInfoWindow.open(yourMap, yourMarker);

            });

            google.maps.event.addListener(yourMarker, 'mouseout', function () {
            yourInfoWindow.open(yourMap, yourMarker);

            });


            Then you can just hover over it and it will close itself.






            share|improve this answer















            How about -



            google.maps.event.addListener(yourMarker, 'mouseover', function () {
            yourInfoWindow.open(yourMap, yourMarker);

            });

            google.maps.event.addListener(yourMarker, 'mouseout', function () {
            yourInfoWindow.open(yourMap, yourMarker);

            });


            Then you can just hover over it and it will close itself.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 18 '13 at 17:07









            Mansfield

            7,762145795




            7,762145795










            answered Sep 18 '13 at 16:50









            NathanNathan

            211




            211























                1














                var map;
                var infowindow;
                ...
                function createMarker(...) {
                var marker = new google.maps.Marker({...});
                google.maps.event.addListener(marker, 'click', function() {
                ...
                if (infowindow) {
                infowindow.close();
                };
                infowindow = new google.maps.InfoWindow({
                content: contentString,
                maxWidth: 300
                });
                infowindow.open(map, marker);
                }
                ...
                function initialize() {
                ...
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                ...
                google.maps.event.addListener(map, 'click', function(event) {
                if (infowindow) {
                infowindow.close();
                };
                ...
                }
                }





                share|improve this answer
























                • Thanx, I needed to close the infowindow when not clicked a marker so just on the map

                  – VRC
                  Dec 5 '12 at 10:46
















                1














                var map;
                var infowindow;
                ...
                function createMarker(...) {
                var marker = new google.maps.Marker({...});
                google.maps.event.addListener(marker, 'click', function() {
                ...
                if (infowindow) {
                infowindow.close();
                };
                infowindow = new google.maps.InfoWindow({
                content: contentString,
                maxWidth: 300
                });
                infowindow.open(map, marker);
                }
                ...
                function initialize() {
                ...
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                ...
                google.maps.event.addListener(map, 'click', function(event) {
                if (infowindow) {
                infowindow.close();
                };
                ...
                }
                }





                share|improve this answer
























                • Thanx, I needed to close the infowindow when not clicked a marker so just on the map

                  – VRC
                  Dec 5 '12 at 10:46














                1












                1








                1







                var map;
                var infowindow;
                ...
                function createMarker(...) {
                var marker = new google.maps.Marker({...});
                google.maps.event.addListener(marker, 'click', function() {
                ...
                if (infowindow) {
                infowindow.close();
                };
                infowindow = new google.maps.InfoWindow({
                content: contentString,
                maxWidth: 300
                });
                infowindow.open(map, marker);
                }
                ...
                function initialize() {
                ...
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                ...
                google.maps.event.addListener(map, 'click', function(event) {
                if (infowindow) {
                infowindow.close();
                };
                ...
                }
                }





                share|improve this answer













                var map;
                var infowindow;
                ...
                function createMarker(...) {
                var marker = new google.maps.Marker({...});
                google.maps.event.addListener(marker, 'click', function() {
                ...
                if (infowindow) {
                infowindow.close();
                };
                infowindow = new google.maps.InfoWindow({
                content: contentString,
                maxWidth: 300
                });
                infowindow.open(map, marker);
                }
                ...
                function initialize() {
                ...
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                ...
                google.maps.event.addListener(map, 'click', function(event) {
                if (infowindow) {
                infowindow.close();
                };
                ...
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 5 '11 at 17:14









                D KD K

                22124




                22124













                • Thanx, I needed to close the infowindow when not clicked a marker so just on the map

                  – VRC
                  Dec 5 '12 at 10:46



















                • Thanx, I needed to close the infowindow when not clicked a marker so just on the map

                  – VRC
                  Dec 5 '12 at 10:46

















                Thanx, I needed to close the infowindow when not clicked a marker so just on the map

                – VRC
                Dec 5 '12 at 10:46





                Thanx, I needed to close the infowindow when not clicked a marker so just on the map

                – VRC
                Dec 5 '12 at 10:46











                1














                I stored a variable at the top to keep track of which info window is currently open, see below.



                var currentInfoWin = null;
                google.maps.event.addListener(markers[counter], 'click', function() {
                if (currentInfoWin !== null) {
                currentInfoWin.close(map, this);
                }
                this.infoWin.open(map, this);
                currentInfoWin = this.infoWin;
                });





                share|improve this answer




























                  1














                  I stored a variable at the top to keep track of which info window is currently open, see below.



                  var currentInfoWin = null;
                  google.maps.event.addListener(markers[counter], 'click', function() {
                  if (currentInfoWin !== null) {
                  currentInfoWin.close(map, this);
                  }
                  this.infoWin.open(map, this);
                  currentInfoWin = this.infoWin;
                  });





                  share|improve this answer


























                    1












                    1








                    1







                    I stored a variable at the top to keep track of which info window is currently open, see below.



                    var currentInfoWin = null;
                    google.maps.event.addListener(markers[counter], 'click', function() {
                    if (currentInfoWin !== null) {
                    currentInfoWin.close(map, this);
                    }
                    this.infoWin.open(map, this);
                    currentInfoWin = this.infoWin;
                    });





                    share|improve this answer













                    I stored a variable at the top to keep track of which info window is currently open, see below.



                    var currentInfoWin = null;
                    google.maps.event.addListener(markers[counter], 'click', function() {
                    if (currentInfoWin !== null) {
                    currentInfoWin.close(map, this);
                    }
                    this.infoWin.open(map, this);
                    currentInfoWin = this.infoWin;
                    });






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Oct 22 '13 at 16:07









                    P NelsonP Nelson

                    111




                    111























                        -1














                        var contentString = "Location: " + results[1].formatted_address;    
                        google.maps.event.addListener(marker,'click', (function(){
                        infowindow.close();
                        infowindow = new google.maps.InfoWindow({
                        content: contentString
                        });
                        infowindow.open(map, marker);
                        }));





                        share|improve this answer






























                          -1














                          var contentString = "Location: " + results[1].formatted_address;    
                          google.maps.event.addListener(marker,'click', (function(){
                          infowindow.close();
                          infowindow = new google.maps.InfoWindow({
                          content: contentString
                          });
                          infowindow.open(map, marker);
                          }));





                          share|improve this answer




























                            -1












                            -1








                            -1







                            var contentString = "Location: " + results[1].formatted_address;    
                            google.maps.event.addListener(marker,'click', (function(){
                            infowindow.close();
                            infowindow = new google.maps.InfoWindow({
                            content: contentString
                            });
                            infowindow.open(map, marker);
                            }));





                            share|improve this answer















                            var contentString = "Location: " + results[1].formatted_address;    
                            google.maps.event.addListener(marker,'click', (function(){
                            infowindow.close();
                            infowindow = new google.maps.InfoWindow({
                            content: contentString
                            });
                            infowindow.open(map, marker);
                            }));






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jun 1 '15 at 8:56









                            AlexB

                            5,175114160




                            5,175114160










                            answered Jun 1 '15 at 8:50









                            SaiSai

                            126213




                            126213






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2223574%2fgoogle-maps-auto-close-open-infowindows%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to change which sound is reproduced for terminal bell?

                                Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                                Can I use Tabulator js library in my java Spring + Thymeleaf project?