Reverse Geocoding using Perl and Google Maps API

北城以北 提交于 2019-12-11 04:29:45

问题


A Perl module exists in order to interface with the Google Maps API. The code is as follows:

 use Geo::Coder::Google;
 $geocoder = Geo::Coder::Google->new();
 @location = $geocoder->geocode(location => '1600 Pennsylvania Ave. Washington DC USA'); 

Site Source: http://search.cpan.org/~arcanez/Geo-Coder-Google-0.11/lib/Geo/Coder/Google/V2.pm

However I need to go from coordinates to address. How is that done even if that means using a different means in PERL? Please be advised that I tried the OpenMaps API and it is inaccurate. Google Maps seems to be much better.


回答1:


The maintainer for Geo::Coder::Google accepted my patch, so the module now supports reverse-geocoding (as of version 0.12).

Example use:

use Geo::Coder::Google;
$geocoder = Geo::Coder::Google->new(apiver => 3);
$location = $geocoder->reverse_geocode(latlng => '37.778907,-122.39732');

See docs at: http://metacpan.org/pod/Geo::Coder::Google::V3




回答2:


The answer, is that Geo::Coder::Google did not implement the latlng parameter for reverse lookups. So you cannot use it for that.

However, it would be pretty simple to add reverse lookup functionality.




回答3:


Answer:

  1. Download the CPAN Geo::Coder::Google object.
  2. Navigate your way to the V3.pm object.
  3. Drop this module into the code.

sub reverseGeocode {my $self = shift;

my %param;
if (@_ % 2 == 0) {
    %param = @_;
} else {
    $param{location} = shift;
}



my $location = $param{location} 
    or Carp::croak("Usage: reverseGeocode(location => \$location)");

if (Encode::is_utf8($location)) {
    $location = Encode::encode_utf8($location);
}

my $uri = URI->new("http://$self->{host}/maps/api/geocode/json");
my %query_parameters = (latlng => $location);
$query_parameters{language} = $self->{language} if defined $self->{language};
$query_parameters{region} = $self->{region} if defined $self->{region};
$query_parameters{oe} = $self->{oe};
$query_parameters{sensor} = $self->{sensor} ? 'true' : 'false';
$uri->query_form(%query_parameters);
my $url = $uri->as_string;

if ($self->{client} and $self->{key}) {
    $query_parameters{client} = $self->{client};
    $uri->query_form(%query_parameters);

    my $signature = $self->make_signature($uri);
    # signature must be last parameter in query string or you get 403's
    $url = $uri->as_string;
    $url .= '&signature='.$signature if $signature;
}

Then just use it like this:

 my $location = $geocoder->reverseGeocode(location => '40.7837366863403,-73.9882784482727');

Then you can access returning object like so:

 print $location->{formatted_address};

To see the detailed parts of the address see the following link as a guide: https://developers.google.com/maps/documentation/geocoding/



来源:https://stackoverflow.com/questions/14228371/reverse-geocoding-using-perl-and-google-maps-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!