Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

Chapter 11 — Building a Community Site 211


(‘http://maps.google.com/maps?q=’. uri_escape($text));

my $var = $response->{_content};

my ($lat,$lng) = ( $var =~ m/GLatLng\(([-\d.]+).*?([-\d.]+)\)/ms );

return ($lat,$lng);
}


The data is extracted from the HTML through a regular expression, and then the request is
repeated to find the real latitude/longitude.


With this script, you can populate the database with different business types by specifying the
type and town/city on the command line. For example, you could update the list of restaurants
in Grantham using the following:


$ ch11inserter.pl restaurant Grantham


You could update the list of banks using this:


$ ch11inserter.pl banks Grantham


The list of what could be inserted into the database using this technique is virtually limitless.


Backend Database Interface


To extract the information from the database (that was populated using the script in the earlier
section), another CGI script, similar to the one in Chapter 9, generates the information in suit-
able XML format when requested by the Google Maps applications. The basic script is identi-
cal, only the SQL required to extract the information and the XML format of the data that is
returned are different:


#!/usr/bin/perl


use DBI;
use strict;
use CGI qw/:standard/;


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


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”;
}


if (param(‘m’) eq ‘entitylist’)
{

Free download pdf