Learn how I can make a difference #thatsIntelligence


read more @ www.agileatwork.co.uk

#techie - Tooltip text on a Google map marker

I've been overseeing some mapping work and one of the requirements is to show around 10 lines of key data for each marker. The Google mapping API does provide an InfoBox which is usually very good for this type of requirement but due to the numbers of pins (or markers) possibly in excess of a thousand! The InfoBox doesn't in my mind provide an aesthetically pleasing solution!

What I really need is a nicely formatted tooltip.... after some time the developer tasked with doing the job came back and reported that they couldn't find an API do so and that the basic tooltip text (which is generated from the title property) just doesn't cut the mustard!

I had a quick look at the API documentation and must admit I couldn't find an easy way of doing it.... So like all experienced developers I consulted Google.

However despite finding lots of people asking this question.... the answer coming back was use the InfoBox!!

So this is my solution to the problem, if somebody knows of a better solution please let me know.

Download the 'working example'

Written By Christian Miles

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Map tooltip text example</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&region=GB"></script>

<style type="text/css">
#mapCanvas { border:1px solid black; height:99%; float: left; }
.tooltip{border:thin 1px #eee;background-color:#FFFBF0;padding:5px;width:200px;}




<div id="mapWrapper">
<div id="mapCanvas"></div>

<script type="text/javascript">
var map;

$(document).ready(function ()
addSiteMarker ("Tooltip test!",-1,52)
setMapSize ();
$(window).resize(function(){setMapSize (); });
function setMapSize ()

function doMap ()
geocoder = new google.maps.Geocoder();
var myOptions = { zoom:7, mapTypeId: google.maps.MapTypeId.ROADMAP, minZoom: 2};
map = new google.maps.Map(document.getElementById("mapCanvas"), myOptions);
map.setCenter( new google.maps.LatLng(54,-2));
map.setZoom (4);
function addSiteMarker (name,longitude, latitude )
var marker = new google.maps.Marker({ position: new google.maps.LatLng(latitude,longitude),
zIndex:500 });
var chTooltipHtml ="<h3>"+ name + "</h3>Line 1<br>Line 2";
var tooltipOptions={ marker:marker, content:chTooltipHtml, cssClass:'tooltip'}
var tooltip = new Tooltip(tooltipOptions)
return marker;
function Tooltip(options)
this.marker_ = options.marker;
this.content_ = options.content;
this.map_ = options.marker.get('map');
this.cssClass_ = options.cssClass||null;
this.div_ = null;
var me = this;
// Show tooltip on mouseover event.
google.maps.event.addListener(me.marker_, 'mouseover', function()
// Hide tooltip on mouseout event.
google.maps.event.addListener(me.marker_, 'mouseout', function()
Tooltip.prototype = new google.maps.OverlayView();

Tooltip.prototype.onAdd = function()

var div = document.createElement('DIV');
div.style.position = "absolute";
div.style.visibility = "hidden";
div.className += " "+this.cssClass_;
div.innerHTML = this.content_;
this.div_ = div;

var panes = this.getPanes();

Tooltip.prototype.draw = function()
var overlayProjection = this.getProjection();
var ne = overlayProjection.fromLatLngToDivPixel(this.marker_.getPosition());
var div = this.div_;
div.style.left = ne.x + 'px';
div.style.top = ne.y + 'px';

Tooltip.prototype.onRemove = function()

Tooltip.prototype.hide = function()
if (this.div_)
this.div_.style.visibility = "hidden";

Tooltip.prototype.show = function()
if (this.div_)
this.div_.style.visibility = "visible";



Christian Miles