Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

Chapter 13 — I Need to Get To... 267


elsif(param(‘m’) eq ‘getroute’)
{
getroute(param(‘routeid’));
}


This section is just a simple dispatcher; the code for individual request types is in the additional
functions.


Message Response


For each operation there should be an XML response to the Google application, even if it is a
basic message to indicate completion. The same system can also be used to return errors.
Depending on the application, the fact that the message is an error doesn’t even have to be
highlighted.


sub xmlmessage
{
my ($message,$attrib) = @_;


printf(‘<message text=”%s” %s/>’,
$message,
join(‘ ‘,map {sprintf(‘%s=”%s”’,$,$attrib->{$}) } keys %{$attrib}));
}


For convenience, the function accepts a second argument; a reference to a hash that contains an
additional list of attributes to return to the caller. This is used, for example, when saving a route
so that the mapping application knows the ID of the route. In this example application, the
JavaScript component uses the extracted ID to immediately load the route as if it had been
selected by the user, which in turn triggers both the start and end markers and the route
calculation.


Listing Existing Routes


All it takes to ask for a list of the existing routes is a suitable SQL query. When returning the
list, four pieces of information will be returned:


Route ID (used to select a route to load)
Route title

Latitude of the first point in the route
Longitude of the first point in the route

This requires a small join that looks for the first point from the database (identified from the
sequence ID). That point data is used by the application to show a marker at the start of each
route in the database.

Free download pdf