Javascript – Google Maps: How does Trulia create their custom InfoWIndows

google mapsjavascriptjquery

I really like how Trulia.com has created their custom Google Map InfoWindows.

What I like in particular about Trulia's implementation of the InfoWindow is:

  • Extends beyond the map border: Google Maps InfoWindows are contained within the map border whereas Trulia's seems to be able to float on-top of the map
  • Always displays InfoWindow near map Center: Google Maps InfoWindows always display the InfoWindow above the marker whereas Trulia InfoWindows always display the InfoWindow as close the center of the map as possible. For example, if the map marker icon is on the top of the map near the border, the Trulia InfoWindow is displayed below the map marker icon
  • InfoWindow is displayed on mouse hover (not 'click'): With the default Google Maps InfoWindow, you have to 'click' the map marker icon to display the InfoWindow whereas Trulia InfoWindows are display simply by hovering over the map marker icon.

I found the PdMarker, which is a 3rd party extension for Google Map InfoWindows that accomplishes most of the above bullets but not all. 1) It does not extend beyond the map border, 2) it does not work with Google Maps API version 3 (only version 2).

Anyone know how Trulia is accomplishing their InfoWindow-like implementation on Google Maps API v3?

Best Answer

That's an interesting question. I've been playing with maps recently too. I'm far from expert but I can tell you what I know.

I think the site you mentioned uses a custom div overlay rather than google's info window.

1. InfoWindow is displayed on mouse hover (not 'click')

This can be done with event listeners. For example in maps API v3:

google.maps.event.addListener(marker, 'mouseover', function() {
   // myDiv.style.visibility = 'visible'
});
google.maps.event.addListener(marker, 'mouseout', function() {
  // myDiv.style.visibility = 'hidden'
});

Here's is a pretty good example of how this can be done.

2. Extends beyond the map border
3. Always displays InfoWindow near map Center

Both of these can be achieved using CSS: (2) using z-index and (3) with position.

There is a similar example of using custom marker tooltips which you can find here. It also shows how you can utilize mouseovers to pop-up tooltips by hovering other elements on your page.

myElement.onmouseover = function(){
  google.maps.event.trigger(marker, 'mouseover');
}
myElement.onmouseout = function(){
  google.maps.event.trigger(marker, 'mouseout');
}

Finally, another site that makes a good use of maps, although this one uses V2 of the API. Hope this helps.