Geo::Google seems dead, fails tests, now what?
I tried to install Geo::Google using CPAN ( http://metacpan.org/pod/Geo::Google v0.05 ) and i开发者_Go百科t failed almost all tests. I checked here http://matrix.cpantesters.org/?dist=Geo-Google+0.05 and it confirms that this module is failing everything.
It seems to be an abandoned module, but I need to compute the driving distance between 2 addresses from my Perl program. Any advice?
I am in the process of trying to recover an undermaintained module (Zoidberg) and the best you can do is dig through the results of the CPANtesters to see what went wrong in order to try to fix it.
That said google maps has a public api and even one with a simple web interface. I will try to mock you up an example, but to get yourself started try doing some requests with LWP::UserAgent
or WWW::Mechanize
and then parse out the results to find your answer.
Edit: Ok here is an example. Near the end $data
is a hashref of the data contained in the JSON response described in the link above. I have also calculated the total distance for the (first) route.
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new();
my $origin = "60607";
my $destination = "60067";
my $site = 'http://maps.googleapis.com/maps/api/directions/';
my $mode = 'json';
my $page = $site . $mode . '?origin="' . $origin . '"&destination="' . $destination . '"&sensor=false';
my $response = $ua->get( $page );
my $json = $response->content();
my $data = decode_json $json;
my @legs = @{ $data->{'routes'}[0]{'legs'} };
my $distance_meters = 0;
foreach my $leg (@legs) {
$distance_meters += $leg->{'distance'}{'value'};
}
my $distance_kilometers = $distance_meters / 1000;
my $distance_miles = $distance_kilometers * 0.62137119;
print $distance_miles . " Miles\n";
Edit: And now, since I was bored on a Sunday afternoon:
perl -e 'use JSON;use LWP::Simple;($s,$e)=@ARGV;$m+=$_->{distance}{value}for@{(decode_json get qq<http://maps.googleapis.com/maps/api/directions/json?origin="$s"&destination="$e"&sensor=false>)->{routes}[0]{legs}};printf"%.2f Miles\n",$m*0.6213e-3' 60607 60067
Edit: And now, a port of the one liner to the Mojolicious system:
perl -Mojo -E '$m+=$_->{distance}{value}for@{g("http://maps.googleapis.com/maps/api/directions/json",form=>{origin=>shift,destination=>shift,sensor=>"false"})->json("/routes/0/legs")};printf"%.2f Miles\n",$m*0.6213e-3' 60607 60067
精彩评论