Resizing Google Map InfoWindow (Speech Bubble)

The code examples included here have been simplified somewhat as my working system had complexities specific to the application that would not exist normally.

To resize the infoWindow just call the resizeInfoWindow function and pass in the Id to the element that contains the content.

function resizeInfoWindow(contentElementId) {
    // Get the current open infowindow from the map
    var infoWindow = map.getInfoWindow();
   
    // Get the point this infowindow belongs to
    var point = infoWindow.getPoint();
   
    // Get the size of the content block so to make the info window the correct size to hold it
    var contentBlock = document.getElementById(contentElementId);
    var width = contentBlock.getElementWidth();
    var height = contentBlock.getElementHeight();
    infoWindow.reset(point, null, new GSize(width,height));
}

Object.prototype.getElementWidth = function() {
    if (typeof this.clip !== "undefined") {
        return this.clip.width;
    } else {
        if (this.style.pixelWidth) {
            return this.style.pixelWidth;
        } else {
            return this.offsetWidth;
        }
    }
}

Object.prototype.getElementHeight = function() {
    if (typeof this.clip !== "undefined") {
        return this.clip.height;
    } else {
        if (this.style.pixelHeight) {
            return this.style.pixelHeight;
        } else {
            return this.offsetHeight;
        }
    }
}

Google Map - Resize InfoWindow

I have worked with Google Maps on a number of challenging projects, each a little more complex than the previous. The most recent project needed to resize an open infoWindow (speech bubble) based on its content. Sounds easy enough right. Well, it turned out to be less straight forward than you might think.

June 24, 2009