Hacking Google Maps and Google Earth (ExtremeTech)

(Dana P.) #1

100 Part II — Instant Gratification


Listing 6-3(continued)

use URI::Escape;

my $ua = LWP::UserAgent->new(
agent => “Mozilla/5.0 ;
(X11; U; Linux i686; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01”,
);

# Send the query
my $response = $ua->get;
(‘http://maps.google.com/maps?q=’. uri_escape($ARGV[0]));

# Extract the content
my $var = $response->{_content};

# Extract the latitude and longitude
my ($lat,$lng) = ( $var =~ m/GLatLng\(([-\d.]+).*?([-\d.]+)\)/ms );
print “Lat: $lat - Lng: $lng\n”;

if (!defined($lat)) # Show alternates if we can’t find lat/lng
{
# First remove any irrelevant data from the raw text
$var =~ s/.*Did you mean:(.*)/$1/gmi;

# Extract the query values
my (@alternatives) = ($var =~ m/q=(.*?)[&”]/gm);
print “Alternatives:\n”;

# Dedupe results
my $alternates = {};
foreach my $alt (@alternatives)
{
$alt =~ s/\+/ /g;
$alternates->{$alt} = 1;
}

foreach my $alt (keys %{$alternates})
{
print “$alt\n”;
}
}

The script works by supplying a suitable query to the Google system. You then parse the out-
put, first extracting the latitude and longitude (if you can find it). If the correct latitude and
longitude cannot be found, you parse the raw HTML sent back to look for the alternatives
suggested by the Google Maps system. To do this, the script effectively does the opposite of
Free download pdf