Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

170 Part III — Google Map Hacks


script that provides an interface to the database. The CGI script is asked for a list of cities,
which it collects from the database and returns as XML, placing the name of each city into a
suitable attribute in an appropriate tag.

Listing 9-15 shows the showcitylist()function. This function is responsible for connect-
ing to the CGI script, requesting a list of cities, and formatting that list of cities as a list of
clickable links that will in turn trigger the loading of restaurants in a city.

Listing 9-15: Showing a List of Cities

function showcitylist() {
map.clearOverlays();
index = 0;
points = [];
markers = [];
markerinfo = [];
cities = []; message.innerHTML = ‘Select a City’;
infopanel.innerHTML = ‘’;
var request = GXmlHttp.create();
request.open(‘GET’,’/examples/ch09-16.cgi?m=citylist’, true);
request.onreadystatechange = function() {
if (request.readyState == 4) {
var xmlsource = request.responseXML;
var citylist = ;
xmlsource.documentElement.getElementsByTagName(“city”);
for (var i=0;i < citylist.length;i++) {
cities.push(citylist[i].getAttribute(“cityname”));
infopanel.innerHTML = infopanel.innerHTML +
‘<a href=”#” onClick=”loadcity(‘ +
i +
‘);”>’ +
citylist[i].getAttribute(“cityname”) +
‘</a><br/>’;
}
}
}
request.send(null);
}

Although when the script is initially executed all of the global variables will effectively be empty,
the showcitylist()function can also be called once a list of restaurants in a given city have
been listed, effectively resetting the application. The first step therefore is to reset the contents of
all the variables. You then format the HTML information window with a list of available cities by
loading the data from the CGI script as XML and creating a list of suitable links.

When the user clicks an individual city, the loadcity()function is triggered (see Listing
9-16). On the whole, this function is almost identical to the previous examples in this chapter;
it asks the CGI script for a list of restaurants in a given city.
Free download pdf