218 Part III — Google Map Hacks
The function also triggers the info panel to open. The XML for the marker is loaded from the
XML placed into the attribute for the marker object. This is a change from Chapter 9, where
the XML was loaded using a unique index reference from a global array.
Removing Existing Markers
To remove existing markers from the map, you step through the list of marker objects, remov-
ing each marker overlay. You can do this by using the marker reference stored as an attribute in
the marker object:
function clearmarkers(type) {
var keeplist = [];
for (var i=0;i<markers.length;i++) {
if (markers[i].type == type) {
map.removeOverlay(markers[i].marker);
} else {
keeplist.push(markers[i]);
}
}
markers = [];
for (var i=0;i<keeplist.length;i++) {
markers.push(keeplist[i]);
}
}
Markers also need to be removed from the array of markers displayed on the map. There are
many ways to achieve this, but the most efficient and reliable that I have found is to push each
marker that is not being deleted onto a new array. Then empty the original array and repopu-
late it with the markers that have not been deleted.
Adding Markers
Adding a marker involves creating the pointand markerobjects and accepting the icon
information and type to populate an appropriate entity object. The marker is then created, and
the listener for opening the info window to each marker is added:
function addmarker(x,y,title,info,icon,type) {
var point = new GPoint(parseFloat(x),parseFloat(y));
var marker = new GMarker(point,icon);
GEvent.addListener(marker,
‘click’,
function() {
marker.openInfoWindowXslt(info,”/examples/ch08-12.xsl”);
}
);
map.addOverlay(marker);
markers.push(new entitymarker(y,x,title,type,marker,info));
infopanel.innerHTML = infopanel.innerHTML +