Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

172 Part III — Google Map Hacks


Moving the Map and Adding Information
When the user clicks a restaurant in the information window, the map recenters on the marker
and then shows the information window. For convenience, the information window XML was
placed into a global array, and each element of the array should have the same index as the
points for the restaurant. You can open the window by calling the openInfoWindowXslt()
method on the appropriate marker with the same window information that is triggered by the
click event for the marker. The two lines of the movemap()function are shown in Listing 9-17.

Listing 9-17: Moving the Map and Displaying the Info Window

function movemap(index) {
map.recenterOrPanToLatLng(points[index]);
markers[index].openInfoWindowXslt(markerinfo[index], ;
“/examples/ch09-12.xsl”);
}

The markers,points, and markerinfoarrays are built each time the addmarker()
function is called.

Adding a Marker to Your Map
Listing 9-18 is a small modification to the addmarker()function that updates the arrays
necessary for movemap(), in addition to configuring the point, marker, and event and the
information window.

Listing 9-18: Creating a New Marker

function addmarker(x,y,title,info,index) {
var point = new GPoint(parseFloat(x),parseFloat(y));
points.push(point);
var marker = new GMarker(point);
markers.push(marker);
markerinfo.push(info);
GEvent.addListener(marker,
‘click’,
function() {
marker.openInfoWindowXslt(info,”/examples/ch09-12.xsl”);
}
);
map.addOverlay(marker);
infopanel.innerHTML = infopanel.innerHTML +
‘<a href=”#” onClick=”movemap(‘ + index + ‘);”>’ +
title +
‘</a><br/>’;
index++;
}
Free download pdf