// I need to

ModernizeUpgrade without breaking it RescueFix what someone else started Build NewTurn an idea into a product Reach More PeopleGet found, get used, include everyone

// Company

Case StudiesExpertiseAbout + CareersResourcesContact Book a call →
≡articleBuild New

Custom Google Info Windows: Updated, Live

Custom Google Maps info windows, updated with a live demo and panToView, which pans the map just enough to show an info window that falls off screen.

Christopher Keefer May 9, 20144 min read

From the Art+Logic archive · written in 2014. The tools may have moved on; the thinking still holds.

April 30, 2014 at 3:22 am Remy says:

Do you have a webpage with the full code (html, js, css) to test how it works? Tia

My dear… Remy? Tia? Both?

Whoever you are, your wish is granted: CGWin Demo

Not content to merely offer a live example of our Custom Google Info Windows, we’ve also updated our implementation with panToView capability - that is, when you click on a marker and the resulting info window is outside the current map bounds, the map will now pan the minimum amount necessary to show the full info window.

It’s not as easy as you might expect! Let’s take a look at what we have to do to make this happen (and then note the demo page again, just for good measure).

So, let’s start off with a big ol’ block of code:

/**
 * If the custom window is not already entirely within the map view, pan the map the minimum amount
 * necessary to bring the custom info window fully into view.
 */
CustomWindow.prototype.panToView = function(){
    var position = this.position,
        latlng = this.marker.getPosition(),
        top = parseInt(this.container.style.top, 10),
        cHeight = position.y - top,
        cWidth = this.container.offsetWidth / 2,
        map = this.getMap(),
        center = map.getCenter(),
        bounds = map.getBounds(),
        degPerPixel = (function(){
            var degs = {},
                div = map.getDiv(),
                span = bounds.toSpan();

        degs.x = span.lng() / div.offsetWidth;
        degs.y = span.lat() / div.offsetHeight;
        return degs;
    })(),
    infoBounds = (function(){
        var infoBounds = {};

        infoBounds.north = latlng.lat() + cHeight * degPerPixel.y;
        infoBounds.south = latlng.lat();
        infoBounds.west = latlng.lng() - cWidth * degPerPixel.x;
        infoBounds.east = latlng.lng() + cWidth * degPerPixel.x;
        return infoBounds;
    })(),
    newCenter = (function(){
        var ne = bounds.getNorthEast(),
            sw = bounds.getSouthWest(),
            north = ne.lat(),
            east = ne.lng(),
            south = sw.lat(),
            west = sw.lng(),
            x = center.lng(),
            y = center.lat(),
            shiftLng = ((infoBounds.west < west) ? west - infoBounds.west : 0) +
                ((infoBounds.east > east) ? east - infoBounds.east : 0),
            shiftLat = ((infoBounds.north > north) ? north - infoBounds.north : 0) +
                ((infoBounds.south < south) ? south - infoBounds.south : 0);

        return (shiftLng || shiftLat) ? new google.maps.LatLng(y - shiftLat, x - shiftLng) : void 0;
    })();

if (newCenter){
    map.panTo(newCenter);
    }

};

You’ll note almost all of the work happens in the long, run-on var statement. At first blush, this looks like a mere effort at character saving (all those extra vars would be pretty messy) - but thanks to Javascript var hoisting its actually a better representation of what the function block looks like to the runtime. But I digress.

var position = this.position,
    latlng = this.marker.getPosition(),
    top = parseInt(this.container.style.top, 10),
    cHeight = position.y - top,
    cWidth = this.container.offsetWidth / 2,
    map = this.getMap(),
    center = map.getCenter(),
    bounds = map.getBounds(),

We start off by getting the locations and bounds of a few things - namely, the position of the custom window itself, the position of its marker, the dimensions of the container, the map and its center and bounds, all for use in later computations.

degPerPixel = (function(){
    var degs = {},
        div = map.getDiv(),
        span = bounds.toSpan();

degs.x = span.lng() / div.offsetWidth;
degs.y = span.lat() / div.offsetHeight;
return degs;
})(),

Here, we create a closure to return an object whose values are the calculated number of degrees per pixel in our map view on the x and y axes. We need this in order to calculate the difference between the current bounds and position of the infoWindow versus the bounds of the map.

infoBounds = (function(){
    var infoBounds = {};

infoBounds.north = latlng.lat() + cHeight * degPerPixel.y;
infoBounds.south = latlng.lat();
infoBounds.west = latlng.lng() - cWidth * degPerPixel.x;
infoBounds.east = latlng.lng() + cWidth * degPerPixel.x;
return infoBounds;

})(),

Similarly, here we return an object containing the calculating bounds of our info window in degrees, with the base of the marker being considered the southern boundary, so that both the marker and info window will be in view when we’re done.

newCenter = (function(){
    var ne = bounds.getNorthEast(),
        sw = bounds.getSouthWest(),
        north = ne.lat(),
        east = ne.lng(),
        south = sw.lat(),
        west = sw.lng(),
        x = center.lng(),
        y = center.lat(),
        shiftLng = ((infoBounds.west < west) ? west - infoBounds.west : 0) +
            ((infoBounds.east > east) ? east - infoBounds.east : 0),
        shiftLat = ((infoBounds.north > north) ? north - infoBounds.north : 0) +
            ((infoBounds.south < south) ? south - infoBounds.south : 0);

        return (shiftLng || shiftLat) ? new google.maps.LatLng(y - shiftLat, x - shiftLng) : void 0;

})();

if (newCenter){
    map.panTo(newCenter);
}

Finally, we calculate what should be the new center of the map based on the minimum shift of the center necessary in order to ensure the full bounds of our info window, as previously calculated, will be in view, considering the bounds of the map. If no shift is required, we return undefined and skip setting a new center.

Want to see it in action? CGWin Demo Want to see the code? CGWin Github repo

keep going

More to explore in Build New.

All Build New resources →
let's talk

Got a problem like this one?

Tell us what's stuck. A real person replies within one business day.

Talk to our team →

Talk to people who know software, not a sales script.

Your first call is with Aja, our VP of Sales. He'll help make sure you're solving the right problem. Here's what you'll cover together:

  1. What you're trying to accomplish

    The business goal first, then the technology.

  2. Possible approaches

    And the tradeoffs of each, in plain language.

  3. Risks you may not have considered

    Technical and business, before they cost you.

  4. Whether AI changes the math

    What's practical, and what's just hype for your project.

  5. Sensible next steps

    Even if the right next step is not us.

book-a-call.ics

Loading the scheduler…

Open the booking page →

Some problems are easier to explain in writing. Send us a few lines about what you're working on, and we'll reply within one business day. Helpful to include:

  1. What the software does, or should do
  2. What's stuck, or what's worrying you
  3. Any deadline or budget you have in mind
note.md

Loading the form…