Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

188 Part III — Google Map Hacks


FIGURE10-1: A simple city marker map.

Building an Internal Data Representation


Before the statistical data is plotted onto the map, the application must account for the differ-
ent years for which the data is available. The information about the population data is stored in
a single XML file, but the file does not need to be parsed multiple times to extract the infor-
mation that is required.

The associative array in JavaScript enables data to be stored by text reference, instead of
numerical reference. By combining a textual reference (the year of the data) with an array of
population data (with the same numerical index as each city and associated map point and
marker) it is possible to select and display the population information.

To achieve this, change the code for parsing the XML file to extract the data and build the
necessary file. The core of processing remains the same:
if (request.readyState == 4) {
var xmlsource = request.responseXML;
var markerlist = xmlsource.documentElement.getElementsByTagName(“city”);
for (var i=0;i < markerlist.length;i++) {
addmarker(parseFloat(markerlist[i].getAttribute(“lng”)),
parseFloat(markerlist[i].getAttribute(“lat”)),
markerlist[i].getAttribute(“title”));

Then, create an associative array at the same index for your map pointer/marker:
popdata[i] = new Array();
Free download pdf