Chapter 16 — Generating Google Earth Feeds 315
Generating KML Dynamically
The static KML file generation shown in the previous section is less than ideal when the data
changes regularly. For static elements, such as documenting the location of mountains or seas,
the information displayed is unlikely to change. For business details, the information is likely to
change at least monthly.
With some applications, you might want to update the information even more frequently than
that. For the moment, you’ll just handle the dynamic generation of information and how that
can be loaded into the Google Earth application.
Changing the Script
To generate the information dynamically, a similar change to that used when moving from the
static XML to dynamic XML system used in Chapters 9 and 10 for Google Maps applications
is required. The change comes down to a single line: The correct HTTP header and content
type must be output by the script.
Google Earth supports two file types: KML and KMZ. The former, the uncompressed version,
requires the following content type:
application/vnd.google-earth.kml+xml xml
For KMZ files use the following content type:
application/vnd.google-earth.kmz kmz
The script will be generating KML, so the change is very straightforward. The entire script is
included here for reference to show that there are no differences between this and the static
generation version:
#!/usr/bin/perl
use DBI;
use strict;
use CGI qw/:standard/;
print header(-type => ‘application/vnd.google-earth.kml+xml xml’);
my $dbh = DBI->connect( ‘dbi:mysql:database=mapsbookex;host=db.maps.mcslp.com’,
‘mapsbookex’,
‘examples’,
);
print<<EOF;
<?xml version=”1.0” encoding=”UTF-8”?>
EOF