270 Part III — Google Map Hacks
Again, a simple message is returned to indicate the completion of the operation.
Obtaining a Single Route
To get a single route is a two-stage process. First, the base route information needs to be
extracted. Then the route points need to be extracted from the database in the correct order for
them to be represented on the map. The sequence is important. A sequence ID is added to
each route point when the route is saved; otherwise the individual points would skip around
the map, instead of following the proscribed route.
The function also has to take into account what might happen if the route had been deleted
after the list was generated, but before the chosen route had been requested. This error event is
handled by returning a suitable error message through the xmlmessage()function back to
the JavaScript component of the application:
sub getroute
{
my ($reqrouteid) = @_;
my $routedata =
$dbh->selectrow_hashref(‘select routeid,title,description ‘.
‘from ch13_routesimple where routeid = ‘.
$dbh->quote($reqrouteid));
if ($routedata->{routeid} != $reqrouteid)
{
xmlmessage(‘Error: route not found’);
return();
}
printf(‘<route><routeinfo routeid=”%s” title=”%s” description=”%s”/>’,
$routedata->{routeid},
$routedata->{title},
$routedata->{description});
With the basic route information in place you can proceed to return a list of points. Remember
that the points must be extracted in the right sequence, so a suitable order byclause is added
to the SQL statement:
my $sth =
$dbh->prepare(sprintf(‘select lat,lng from ch13_routepoints ‘.
‘where routeid = %s order by seqid’,
$dbh->quote($routedata->{routeid})));
$sth->execute();
As the script works through the individual points of the table, it will also calculate the distance
between two points. To do this, the current set of points is recorded; once at least two points
are known (the recorded set and the current set) the calculation can take place. Thus if you
have a route with three points, A, B, and C, you can calculate the total distance by adding the
distance between A and B and B and C together. The process is examined in more detail in the
next section, “Calculating Distance.”