GMaps V3 InfoWindow - disable the close "x" button
Asked Answered
P

26

42

From what I see, in v2 of GMaps API there was a property "buttons" of the InfoWindow object that one could define in a way that given InfoWindow has no close button:

marker.openInfoWindowHtml("No Close Button",{buttons:{close:{show:4}}});

The above however does not apply for v3. Does anybody know a way to do it? I read about an utility that replaces InfoWindow called InfoBox but it has not been developed for the past 2 years. I'm currently using the latest 3.13 version of Gmaps v3 API.

A workaround with jQuery is acceptable, if there is no better solution.

Penis answered 21/9, 2013 at 13:50 Comment(0)
M
20

Update

Displaying a <div> on top of a google map is straight forward :

example css:

div.info {
    position: absolute;
    z-index: 999;
    width: 200px;
    height: 100px;
    display: none;
    background-color: #fff;
    border: 3px solid #ebebeb;
    padding: 10px;
}

A info class <div> somewhere in the markup :

<div id="myinfo" class="info"><p>I am a div on top of a google map .. </p></div>

Always nice to have a short reference to the div :

var info = document.getElementById('myinfo');

The more tricky part, showing the <div>, how and when - here I just assign a click handler to the map (after it is created) and show the info <div> at mouse location XY inside the map :

google.maps.event.addListener(map, 'click', function(args) {
  var x=args.pixel.x; //we clicked here
  var y=args.pixel.y;

  info.style.left=x+'px';
  info.style.top=y+'px';
  info.style.display='block';
});

What you gain with this is, that the info <div> follows you around on the map, every time you click.

enter image description here

  • You will need more styling so it suits your need, eg so it "looks like an InfoBox", but that should be easy to find out, I am not a librarian :)
  • And maybe later on something to close the info with, but that you didn't want in the first place :)

Original answer

You cant! There is no way to do this in the current v3.13 InfoWindow options.

A workaround is to disable the image containing the X :

<style>
img[src="http://maps.gstatic.com/mapfiles/api-3/images/mapcnt3.png"] {
    display: none;
}
</style>

enter image description here

But this is in no way advisable!

src="http://maps.gstatic.com/mapfiles/api-3/images/mapcnt3.png is just what the infowindow is referring to today. Tomorrow, or in a month, or in a year, this image-reference for sure has changed. As you can see if you search for similar "solutions", made over time - like this. They are all broken today, eg the effort is meaningless.

I think there is extremely good logic in google "refusing" to follow the request for hiding the close-button. If you not need a close-button, what do you need an InfoWindow for anyway? When you are better off just to show a <div> on the map.

Metempsychosis answered 21/9, 2013 at 15:12 Comment(3)
Could you instead hint on how to display a <div> on the map that looks like an InfoBox?Penis
"If you not need a close-button, what do you need an InfoWindow for anyway?" - you can close the infoWindow with the mouseout event...Parlay
In my case there is no image with above src but the style with -webkit-mask-image is present.Deputation
S
38

You can also do it through the css.

.gm-style-iw + div {display: none;}

edit january 2019
as @antmeehan said in the comment,

Google have changed the HTML, and the close button is now a button element rather than a div

So the css code to hide the "x" button is now:

.gm-style-iw + button {display: none;}
Satsuma answered 18/5, 2015 at 0:20 Comment(6)
Worked for me and it seems to be the the best and easies solutionFidelia
Still somewhat subject to Google changing how things are implemented - depends on the info window class staying the same, and close button being the immediate next div after it. But at least its simple.Kiefer
Google have changed the HTML, and the close button is now a button element rather than a div.Crapulous
Thanks for this. one question tho, why did you use + in your declaration? It works by declaring .gm-style-iw button without the + sign. Actually it should be .gm-style-iw > button because if you have other custom buttons within the box, it'd be hidden too. Placing it as an answer and referring to this for future users.Pentstemon
The + operator will select all <button> elements that are placed immediately after elements that are of class gm-style-iw. The > operator will select all <button> elements where the parent element contains the class .gm-style-iw. I think both may accomplish the same goal.Satsuma
I added !important , because button style is directly on element, so you need to add it to get priority for your CSSHartsfield
M
20

Update

Displaying a <div> on top of a google map is straight forward :

example css:

div.info {
    position: absolute;
    z-index: 999;
    width: 200px;
    height: 100px;
    display: none;
    background-color: #fff;
    border: 3px solid #ebebeb;
    padding: 10px;
}

A info class <div> somewhere in the markup :

<div id="myinfo" class="info"><p>I am a div on top of a google map .. </p></div>

Always nice to have a short reference to the div :

var info = document.getElementById('myinfo');

The more tricky part, showing the <div>, how and when - here I just assign a click handler to the map (after it is created) and show the info <div> at mouse location XY inside the map :

google.maps.event.addListener(map, 'click', function(args) {
  var x=args.pixel.x; //we clicked here
  var y=args.pixel.y;

  info.style.left=x+'px';
  info.style.top=y+'px';
  info.style.display='block';
});

What you gain with this is, that the info <div> follows you around on the map, every time you click.

enter image description here

  • You will need more styling so it suits your need, eg so it "looks like an InfoBox", but that should be easy to find out, I am not a librarian :)
  • And maybe later on something to close the info with, but that you didn't want in the first place :)

Original answer

You cant! There is no way to do this in the current v3.13 InfoWindow options.

A workaround is to disable the image containing the X :

<style>
img[src="http://maps.gstatic.com/mapfiles/api-3/images/mapcnt3.png"] {
    display: none;
}
</style>

enter image description here

But this is in no way advisable!

src="http://maps.gstatic.com/mapfiles/api-3/images/mapcnt3.png is just what the infowindow is referring to today. Tomorrow, or in a month, or in a year, this image-reference for sure has changed. As you can see if you search for similar "solutions", made over time - like this. They are all broken today, eg the effort is meaningless.

I think there is extremely good logic in google "refusing" to follow the request for hiding the close-button. If you not need a close-button, what do you need an InfoWindow for anyway? When you are better off just to show a <div> on the map.

Metempsychosis answered 21/9, 2013 at 15:12 Comment(3)
Could you instead hint on how to display a <div> on the map that looks like an InfoBox?Penis
"If you not need a close-button, what do you need an InfoWindow for anyway?" - you can close the infoWindow with the mouseout event...Parlay
In my case there is no image with above src but the style with -webkit-mask-image is present.Deputation
D
17

if using jquery just add this

$(".gm-style-iw").next("div").hide();
Denticulation answered 15/10, 2013 at 8:47 Comment(1)
you can do this with pure CSS, just use the + symbol to get next sibling: .gm-style-iw + div { display: none; }Anandrous
W
16

To extend on Louis Moore's answer, you can also center the text after removing the close button:

.gm-style-iw + div {display: none;}
.gm-style-iw {text-align:center;}

Without Centering:

With Centering:

Westmorland answered 16/1, 2017 at 15:7 Comment(1)
This is really the correct and most effective answer by far. But honestly why doesn't google just support hiding it.Sulphurbottom
U
12

Thanks Tushar, but also you need put this code in event handler

google.maps.event.addListener(infowindow, 'domready', function(){
    $(".gm-style-iw").next("div").hide();
});
    
Udder answered 21/2, 2014 at 7:31 Comment(0)
P
8

It should be .gm-style-iw > button to avoid other custom buttons we might have within the box to be hidden too:

.gm-style-iw > button {
  display: none !important;
}
Pentstemon answered 2/8, 2019 at 4:53 Comment(1)
Thank a lot, save my day. :)Chaille
A
7

I used the answer given by Tushar Gaurav, but expanded it a little...

$(".gm-style-iw:contains(" + infoText + ")").css("left", function() {
    return ($(this).parent().width() - $(this).width()) / 2;
}).next("div").remove();

That will remove the X from an infowindow with the text in infoText, and then recenter the text as it's off-center after removing the close button.

Just adding this for anyone else who stumbles across this page as I did.

Am answered 19/12, 2013 at 17:9 Comment(1)
I like this answer since it allows to change a specific infowindow and not just all. For unknown reason the text is not really centered but can be done with css: text-align: centerSingle
E
6
closeBoxURL: ""

as stated before doesn't apply for InfoWIndow. This is an option on the InfoBox.

You may for example use this CSS workaround to remove the X, and the surrounding clickable button:

.gm-style-iw + div {
  display: none;
}

And as davidkonrad said. This is a workaround on the code as it is today. It will likely be changed.

Exegetic answered 15/1, 2014 at 2:34 Comment(0)
O
4

My own way (without Jquery) and with realign to the center of the infoWindow:

var content = document.querySelector('.gm-style-iw');
content.parentNode.removeChild(content.nextElementSibling);
content.style.setProperty('width', 'auto', 'important');
content.style.setProperty('right', content.style.left, 'important');
content.style.setProperty('text-align', 'center', 'important');
Obola answered 10/11, 2013 at 18:32 Comment(0)
K
3

This works

.gm-style-iw > button {display: none !important;}
Kutaisi answered 28/4, 2019 at 17:21 Comment(0)
S
3

Anno domini 2021 this work better for me

button.gm-ui-hover-effect {
  display: none !important;
}
Surprint answered 5/2, 2021 at 22:55 Comment(1)
Still working in early Q2 2024 👏Entrust
A
2

My solution:

            google.maps.event.addListener(marker, 'click', function() {

            var wnd = new google.maps.InfoWindow({
                content: "<table id='viewObject' >...</table>"
            });

            google.maps.event.addListener(wnd, 'domready', function(){

                $("#viewObject").parent().parent().next().remove();
            });

            wnd.open(map, marker);
        });
Ageless answered 16/1, 2014 at 20:15 Comment(1)
Thanks, it works if you only want to remove the x only for some of the info windowsKilligrew
D
2
=============  HTML  ==============
<agm-map #gm [latitude]="lat" [longitude]="lng" #AgmMap [fitBounds]="true">
 <agm-marker
          (mouseOver)="onMouseOver(infoWindow, gm)"
          (mouseOut)="onMouseOut(infoWindow, gm)"
        >

         <div id="test">
            <agm-info-window #infoWindow></agm-info-window>
          </div>
        </agm-marker>
      </agm-map>

=============  TS  ==============

onMouseOver(infoWindow, gm) {
console.log(infoWindow);
if (gm.lastOpen != null) {
 gm.lastOpen.close();
}
gm.lastOpen = infoWindow;
infoWindow.open();
setTimeout(() => {
  var x = document.getElementsByClassName('gm-ui-hover-effect')[0].remove();
}, 10);
}
onMouseOut(infoWindow, gm) {
gm.lastOpen = infoWindow;
// infoWindow.close();

}

Deportation answered 23/5, 2019 at 13:42 Comment(0)
J
2
const infowindow = new google.maps.InfoWindow({
  content: address,
  headerDisabled: true
});
Jose answered 14/6 at 14:18 Comment(1)
Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation?Gandy
M
1

You can use this. This will not hide other images like zoom control, pan control etc. on dom ready of info window you can use this.

google.maps.event.addListener(infoWindow, 'domready', function () {

      document.querySelector(".gm-style-iw").nextElementSibling.style.display = "none";
});
Mirador answered 16/4, 2014 at 18:17 Comment(0)
G
1

I can't find correct answer in here, so I fixed it myself. It will work well.

google.maps.event.addListener(infowindow, 'domready', function(){
      $(".gm-style-iw").parent().find("button").removeAttr("style").hide();
});
Gatecrasher answered 21/1, 2019 at 19:37 Comment(0)
C
1

.gm-style-iw + button {display: none !important;}

Coumarone answered 24/1, 2019 at 15:19 Comment(1)
What's the difference from this answer?Isabel
S
0

I couldn't get any of the $(".gm-style-iw").next("div").hide(); answers to work even when calling the code after the DOM was loaded, since there was a delay between the code being called and the info window being created. What I did was create an interval that runs until it finds the info window and removes the "X" element. If there's a better way please tell me.

var removeCloseButton = setInterval(
    function()
    {
        if ($(".gm-style-iw").length)
        {
            $(".gm-style-iw").next("div").remove();
            clearInterval(removeCloseButton);
        }
    },
    10
);
Sinless answered 15/4, 2015 at 21:5 Comment(0)
P
0

Using Archers method I need to do

$(".gm-style-iw").css("left", function() { //remove close and recenter
  return ($(this).parent().width() - $(this).find(">:first-child").width()) / 2;
}).next("div").remove();
Pearlstein answered 11/10, 2015 at 14:2 Comment(0)
K
0
google.maps.event.addListener(infowindow, 'domready', function() {

    var iwOuter = jQuery('.gm-style-iw');

    // Reference to the div that groups the close button elements.
    var iwCloseBtn = iwOuter.next();

    // Apply the desired effect to the close button
    iwCloseBtn.remove()
});

As there is no option to hide this by API parameter, you have to find the element by targeting the content window and remove it.

Hope this helps :)

Kaliningrad answered 10/10, 2017 at 10:39 Comment(0)
J
0

var infoWindows = new google.maps.InfoWindow({ headerDisabled: true, });

Jose answered 28/6 at 18:57 Comment(0)
I
-1

in jQuery's gmap3 plugin this can be accomplished using

google.maps.event.addListener($('#map').gmap3({get:{name:"infowindow"}}), 'domready', function(){
    $(".gm-style-iw").next("div").remove();
});
Ixtle answered 7/5, 2014 at 8:58 Comment(0)
W
-1

May 2021 : Add this in your CSS:

button.gm-ui-hover-effect {
   visibility: hidden;
}

This worked perfectly with Maps API v3.43

Wilbourn answered 1/5, 2021 at 6:3 Comment(0)
P
-2

Inspect it - it's a div with a class of close on it - you can target it with CSS and set it to display: none;

Penult answered 21/9, 2013 at 13:54 Comment(0)
E
-3

You can just use the option

closeBoxURL : ""

http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/reference.html

From Google Documentation

closeBoxURL | string | The URL of the image representing the close box. Note: The default is the URL for Google's standard close box. Set this property to "" if no close box is required.

Example

   var infowindow = new google.maps.InfoWindow({
                          closeBoxURL: "",
                          closeBoxMargin : ""

                        });
Erhard answered 23/5, 2014 at 13:41 Comment(5)
Err, just ready the convoluted answer above. No idea how it's marked as correct. This is the simple and correct answer! Nice one!Lymphatic
I did exactly the same as you... "WTF did I just read" was my closest thoughts... Sadly the user "nan" above correctly answered as well but was down voted with the correct answer. I upvoted him and added my own spice to the answer.Erhard
infowindow != infoboxPentheus
funny thing, if you google for "infobox close button hide", it brings you exactly to this page and this is the only solution that works lolWizard
also didnt work for me. i think its don't work fo version 3.20Pressmark
A
-4

You can set "closeBoxURL" property of the Info Window to "" and it will make the button disappear.

var infowindow = new google.maps.InfoWindow({
    content: contentString,
    closeBoxURL: ""
});
Addicted answered 3/1, 2014 at 21:33 Comment(3)
why did I get -1 to my answer?Addicted
yes, not a valid option: developers.google.com/maps/documentation/javascript/…Dunfermline
@Addicted It's not working for me.. I have triedPrevailing

© 2022 - 2024 — McMap. All rights reserved.