Google Maps: Auto close open InfoWindows?
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
add a comment |
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
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
add a comment |
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
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
javascript google-maps google-maps-api-3
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
add a comment |
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
add a comment |
10 Answers
10
active
oldest
votes
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.
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
add a comment |
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);
});
}
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
add a comment |
//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.
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
add a comment |
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);
});
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
add a comment |
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.
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
add a comment |
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);
});
}
1
This worked well for me
– ollie
Sep 25 '12 at 16:00
add a comment |
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.
add a comment |
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();
};
...
}
}
Thanx, I needed to close the infowindow when not clicked a marker so just on the map
– VRC
Dec 5 '12 at 10:46
add a comment |
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;
});
add a comment |
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);
}));
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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);
});
}
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
add a comment |
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);
});
}
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
add a comment |
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);
});
}
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);
});
}
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
add a comment |
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
add a comment |
//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.
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
add a comment |
//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.
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
add a comment |
//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.
//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.
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
add a comment |
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
add a comment |
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);
});
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
add a comment |
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);
});
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
add a comment |
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);
});
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);
});
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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);
});
}
1
This worked well for me
– ollie
Sep 25 '12 at 16:00
add a comment |
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);
});
}
1
This worked well for me
– ollie
Sep 25 '12 at 16:00
add a comment |
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);
});
}
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);
});
}
answered Aug 13 '12 at 13:22
MelloGMelloG
7781911
7781911
1
This worked well for me
– ollie
Sep 25 '12 at 16:00
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Sep 18 '13 at 17:07
Mansfield
7,762145795
7,762145795
answered Sep 18 '13 at 16:50
NathanNathan
211
211
add a comment |
add a comment |
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();
};
...
}
}
Thanx, I needed to close the infowindow when not clicked a marker so just on the map
– VRC
Dec 5 '12 at 10:46
add a comment |
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();
};
...
}
}
Thanx, I needed to close the infowindow when not clicked a marker so just on the map
– VRC
Dec 5 '12 at 10:46
add a comment |
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();
};
...
}
}
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();
};
...
}
}
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
add a comment |
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
add a comment |
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;
});
add a comment |
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;
});
add a comment |
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;
});
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;
});
answered Oct 22 '13 at 16:07
P NelsonP Nelson
111
111
add a comment |
add a comment |
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);
}));
add a comment |
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);
}));
add a comment |
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);
}));
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);
}));
edited Jun 1 '15 at 8:56
AlexB
5,175114160
5,175114160
answered Jun 1 '15 at 8:50
SaiSai
126213
126213
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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