Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

Chapter 9 — Using Overlays 159


my ($title,$lng,$lat) = split /:/;

$dbh->do(sprintf(‘insert into ch09_simple values(%s,%s,%s)’,
$dbh->quote($lat),
$dbh->quote($lng),
$dbh->quote($title),
));
$counter++;
}
close(DATA);


print “$counter records inserted\n”;


With the information correctly inserted the table, the rows in the table can be used to generate
the XML information that is required by the HTML interface to load the data.


Generating XML from that Information


With a slight modification to an earlier script, a script that loads the information from the
database table and generates the information as XML is shown in Listing 9-8. Instead of
inserting data, a SELECTstatement is used to extract the data from the database, and then the
script reformats the data into XML ready for a Google Maps application.


Listing 9-8:Generating XML from Database Source Data

#!/usr/bin/perl


use DBI;
use strict;


my $dbh = DBI->connect( ‘dbi:mysql:database=mapsbookex;host=db.maps.mcslp.com’,
‘mapsbookex’,
‘examples’,
);


if (!defined($dbh))
{
die “Couldn’t open connection to database\n”;
}


my @lines;


my $sth = $dbh->prepare(‘select title,lat,lng from ch09_simple’);
$sth->execute();


while (my $row = $sth->fetchrow_hashref())


{


Continued
Free download pdf