Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

268 Part III — Google Map Hacks


The resulting list of rows is then returned as XML, using attributes to identify the individual
elements. Remember that you must embed the list of valid entries into an enclosing XML tag,
because of the way in which you extract that information from within the Google Maps XML
parser:
sub listroutes
{
my $sth = $dbh->prepare(‘select ch13_routesimple.routeid,title,lat,lng ‘.
‘from ch13_routesimple,ch13_routepoints where ‘.
‘ch13_routesimple.routeid = ‘.
‘ch13_routepoints.routeid and seqid = 1 order ;
by title’);
$sth->execute();

print “<routes>”;
while (my $row = $sth->fetchrow_hashref())
{
printf(‘<route routeid=”%s” title=”%s” lat=”%s” lng=”%s”/>’,
$row->{routeid},
$row->{title},
$row->{lat},
$row->{lng}
);
}
print “</routes>”;
}

The function is probably the simplest and most straightforward of the functions in the backend
application because it is a simple database query and XML formatting task. Incidentally, if the
query finds no routes, it effectively returns an empty tag pair, displaying no routes in the appli-
cation. By effectively returning an empty list, the need to return an error message is negated;
instead a valid XML document is returned that contains no information.

Saving a Route


When saving a route, the backend needs to account for saving a new route (a route with an
existing ID of zero), which requires an insert into the database of the base route information,
and saving an existing route, which requires an update statement for the base route informa-
tion. For both situations, the rows for the actual route points need to be inserted. Rather than
updating the route points (which would be overly complicated because the application would
need to record the existing points and how they had changed), the existing points are simply
deleted and the new points are created in their place.

The information is supplied to the backend as standard CGI field/value pairs. The only com-
plexity is the extraction of the individual points, which are separated first by commas for the
individual points and then by colons for the sequence number, longitude, and latitude in each
point:
sub saveroute
{
my $title = param(‘title’);
my $description = param(‘desc’);
my $points = param(‘points’);
Free download pdf