Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

156 Part III — Google Map Hacks


Generating an XML File Dynamically
In the previous example a static XML file was generated by adapting an earlier script.
Ultimately, though, the map interface was loading static XML. Loading XML that is gener-
ated on the fly is the next phase toward building a truly dynamic interface. Listing 9-5 shows
an adaptation of the earlier XML generating example that runs as a CGI and returns the XML
based on the earlier requirements.

Listing 9-5:Generating the XML File with Perl on the Fly

#!/usr/bin/perl

use CGI qw/:standard/;

print header(-type => ‘text/xml’);

my $points = [
{x => -0.6394,
y => 52.9114,
title => ‘China Inn’},
{x => -0.64,
y => 52.909444,
title => ‘One on Wharf’},
{x => -0.64454,
y => 52.91066,
title => ‘Hop Sing’},
{x => -0.642743,
y => 52.9123959,
title => ‘Nicklebys’},
{x => -0.6376,
y => 52.9073,
title => ‘Siam Garden’},
];

print ‘<marker>’;

foreach my $point (@{$points})
{
printf(‘<marker lat=”%f” lng=”%f” title=”%s”/>’,
$point->{y},
$point->{x},
$point->{title});
}

print ‘</marker>’;

The script uses the CGI module, a standard module within the Perl distribution, to generate
the information.
Free download pdf