Google Maps API v3: Custom styles for infowindow

JavascriptGoogle Maps

Javascript Problem Overview


I've tried many of the examples from the google maps reference and from other stackoverflow questions, but I haven't been able to get custom styles on my infowindow.

I am using something very similar to what was done in this other stackoverflow answer

Here it is working/editable: http://jsfiddle.net/3VMPL/

In particular, I would like to have square corners instead of the rounded.

Javascript Solutions


Solution 1 - Javascript

Update: Refer to this answer for the state of the infobox source code migration to github.


How about using the Infobox plugin rather than using the usual Infowindow? I put up a complete example in a jsfiddle example here. The main thing about the infobox is that you can create your infobox within your page's html, giving you complete control over how it looks using css (and some javascript options, if necessary). Here's the html for the infobox:

<div class="infobox-wrapper">
	<div id="infobox1">
		Infobox is very easy to customize because, well, it's your code
	</div>
</div>

The idea here is the "infobox-wrapper" div will be hidden (i.e., display:none in your css) and then in your javascript you will initialize an Infobox object and give it a reference to your "infobox" (inside the hidden wrapper) like so:

infobox = new InfoBox({
	content: document.getElementById("infobox1"),
	disableAutoPan: false,
	maxWidth: 150,
	pixelOffset: new google.maps.Size(-140, 0),
	zIndex: null,
	boxStyle: {
                background: "url('http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/examples/tipbox.gif') no-repeat",
                opacity: 0.75,
                width: "280px"
		},
	closeBoxMargin: "12px 4px 2px 2px",
	closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
	infoBoxClearance: new google.maps.Size(1, 1)
});

This is just an example of some of the available options. The only required key is content - the reference to the infobox.

And to open the info window:

google.maps.event.addListener(marker, 'click', function() {
	infobox.open(map, this);
	map.panTo(loc);
});

And to further demonstrate the flexibility of the InfoBox, this jsfiddle has an image with a css transition as the "info box".

And one final tip: Don't use the class "infobox" in your html. It is rather unfortunately used by the plugin when the infobox is actually generated.

Solution 2 - Javascript

If you can't use infobox and need to customise the infowindow you can access it using css. It's not pretty, relies heavily on first:child/last:child psuedo selectors and obviously on whether google ever decides to change their map html structure.

Hopefully the below css can help someone else out in a bind when they're forced to deal with infowindow.

/* white background and box outline */
.gm-style > div:first-child > div + div > div:last-child > div > div:first-child > div
{
    /* we have to use !important because we are overwritng inline styles */
    background-color: transparent !important;
    box-shadow: none !important;
    width: auto !important;
    height: auto !important;
}

/* arrow colour */
.gm-style > div:first-child > div + div > div:last-child > div > div:first-child > div > div > div
{
    background-color: #003366 !important; 
}

/* close button */
.gm-style > div:first-child > div + div > div:last-child > div > div:last-child
{
    margin-right: 5px;
    margin-top: 5px;
}

/* image icon inside close button */
.gm-style > div:first-child > div + div > div:last-child > div > div:last-child > img
{
    display: none;
}

/* positioning of infowindow */
.gm-style-iw
{
    top: 22px !important;
    left: 22px !important;
}

Solution 3 - Javascript

I wanted to have a transparent contact form popup on google map marker click. I made it finally with the help of infobox plugin and this post. That's what I made.

Anyone can download and use the source from here

Solution 4 - Javascript

You can style the infoWindow by styling the .gm-style-iw class.

  #map-canvas {
    .gm-style {
      > div:first-child > div + div > div:last-child > div > div {
        &:first-child > div,
        &:first-child > div > div > div,
        &:last-child,
        &:last-child > img {
          display: none !important;
        }
      }
      .gm-style-iw {
        top: 20px !important;
        left: 130px !important;
        background: #fff;
        padding: 10px;
        height: 40px;
      }
    }
  }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCloudMagickView Question on Stackoverflow
Solution 1 - JavascriptuɥƃnɐʌuopView Answer on Stackoverflow
Solution 2 - JavascriptgiftlistmonkeyView Answer on Stackoverflow
Solution 3 - JavascriptAbu ShumonView Answer on Stackoverflow
Solution 4 - JavascriptIvan ProskuryakovView Answer on Stackoverflow