So previously I showed how to create a basic map, then how to add markers to it. Now suppose you’re wanting to have one of those little bubbles pop up when you click on your map with some information in it. These are called infowindows in Google’s terminology.
Let’s start with the very basics – creating a map which has an infowindow already visible on it.
function initialize() {
var homeLatlng = new google.maps.LatLng(51.476706,0);
var myOptions = {
zoom: 15,
center: homeLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var infowindow = new google.maps.InfoWindow({
content: 'Hello World!',
map: map,
position: homeLatlng
});
}
google.maps.event.addDomListener(window, 'load', initialize);
So we create an infowindow, specify some content for it and which map it’s for, and give it a latlong position (which coincidentally also happens to be where I’ve specified this map is to be centered, although that doesn’t have to be the case). The documentation says that “After constructing an InfoWindow, you must call open to display it on the map“; however I find the above code will open the infowindow without needing to explicitly call the open function.
But this shows the infowindow immediately. Perhaps you’d rather it only appeared after some user interaction. Typically you’d put markers on the map, and when the user clicks the marker, then you’d show the infowindow.
So let’s create the infowindow slightly differently. This time, don’t specify the position attribute. Then create a marker as normal, and finally we can open the infowindow, and we pass the marker as the second parameter. So the infowindow gets anchored to the marker’s position.
var infowindow = new google.maps.InfoWindow({
content: 'Hello World!',
map: map
});
var marker = new google.maps.Marker({
position: homeLatlng,
map: map
});
infowindow.open(map, marker);
So we’ve now added a marker to the map, which the infowindow is then anchored to.
You probably only want this to happen after the user has clicked on that marker. For that we need to have an event listener function. There are many of these available for all the different map elements, in this case we want to listen to the mouseclick event on that marker.
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, this);
});
So we say to the Google Maps event handler framework, on the marker, listen for any ‘click’ events. And when that happens, call this anonymous function which currently only has one line, to open the marker.
We could also make this infowindow appear in response to other events, e.g. if you simply hovered the mouse over the marker.
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.open(map, this);
});
And then to make the infowindow disappear again when you move the mouse away from the marker, just call the close() function on mouseout.
google.maps.event.addListener(marker, 'mouseout', function() {
infowindow.close();
});
Suppose you had several markers on your map, each of which would have its own infowindow. Here’s one way to deal with that. Firstly have an array of everything you need for each marker, arrDestinations. Then loop over them, adding a marker for each. We only have one infowindow variable, and we just update the content for it in response to user clicks. We need to delegate the event handler to an external function, bindInfoWindow, otherwise you end up just getting the content of the last array item for each marker.
<script type="text/javascript">
function initialize() {
var i;
var arrDestinations = [
{
lat: 50.815155,
lon: -0.137072,
title: "Brighton Pier",
description: "Brighton Palace Pier dates to 1899"
},
{
lat: 50.822638,
lon: -0.137361,
title: "Brighton Pavilion",
description: "The Pavilion was built for the Prince of Wales in 1787"
},
{
lat: 50.821226,
lon: -0.139372,
title: "English's",
description: "English's Seafood Restaurant and Oyster Bar"
}
];
var myOptions = {
zoom: 15,
center: new google.maps.LatLng(50.820645,-0.137376),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var infowindow = new google.maps.InfoWindow({
content: ''
});
// loop over our array
for (i = 0; i < arrDestinations.length; i++) {
// create a marker
var marker = new google.maps.Marker({
title: arrDestinations[i].title,
position: new google.maps.LatLng(arrDestinations[i].lat, arrDestinations[i].lon),
map: map
});
// add an event listener for this marker
bindInfoWindow(marker, map, infowindow, "<p>" + arrDestinations[i].description + "</p>");
}
}
function bindInfoWindow(marker, map, infowindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(html);
infowindow.open(map, marker);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
In production you’d probably use a server-side language to populate the array and just pass it into the initialize function. There are other ways of doing this as well. Another approach is to have multiple infowindows, one for each marker. On click, you close any open infowindows, and then just open the one that corresponds to the current marker. I don’t believe that approach is as efficient as my example though.







