Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

Chapter 9 — Using Overlays 157


The most important line in this script is the one that outputs the HTTP header. When supplying
XML you must specify the correct header for the returned data to ensure that it is correctly
identified as XML and not HTML.

You also need to update the URL of the XML file you want to load to reference the new CGI
script. However, in all other respects the main HTML interface developed for the earlier
example is still entirely valid. Updating the URL is a simple case of changing the appropriate
line to the request object:
request.open(‘GET’,’/examples/ch09-05.cgi’, true);

The map output will be the same, but the way the map gets the information to be displayed has
changed. You could replace the static information within the script with a system that gener-
ates the same XML structure, but from a different source, such as directly from a database.

Pulling the Data from a Database


The previous examples have concentrated on the production of information that is largely
static. Even when the XML content was generated dynamically, the source for that XML data
was actually just a hash within a Perl script that generated the information.

Although it would be trivial to update the information within the script, it still lacks the flexi-
bility of updating a database of information that in turn generates the necessary XML.

Creating a Suitable Database Structure


To load the information from a database, there must be a suitable database structure in place
that can be accessed through a CGI script, which can then reformat the information from the
database to the XML format required by Google Maps.

The examples here are in Perl, which can access a database using the DBI module through a
number of different databases, including local files, MySQL, PostgreSQL, and Oracle. Other
environments, such as PHP, Java, or Ruby, have similar interfaces and systems. Depending on
the database system in use, you will also probably need to create a database to hold the table
being used for your information.

Whichever system you use, the first step should be to create a suitable table structure to hold the
information. To replicate the same structure for the restaurants used in earlier examples, only three
fields are required: the latitude, longitude, and restaurant name. With most systems, tables are cre-
ated by using a suitable SQL statement. The following statement would create a suitable table:
create table ch09_simple (lat float,lng float,title varchar(80))

Remember latitude and longitude can be represented in a number of different ways, but the
method used internally by the Google Maps system is a floating-point value, so you will need to
use floating-point fields in your database.

Listing 9-6 demonstrates a simple script that connects to the database and creates the table.
Free download pdf