1

为了与 Google Maps API 交互,存在一个 Perl 模块。代码如下:

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

网站来源:http ://search.cpan.org/~arcanez/Geo-Coder-Google-0.11/lib/Geo/Coder/Google/V2.pm

但是我需要从坐标到地址。即使这意味着在 PERL 中使用不同的方法,那又是如何完成的呢?请注意,我尝试了 OpenMaps API,但它不准确。谷歌地图似乎要好得多。

4

3 回答 3

3

Geo::Coder::Google 的维护者接受了我的补丁,因此该模块现在支持反向地理编码(从 0.12 版开始)。

示例使用:

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

参见文档:http ://metacpan.org/pod/Geo::Coder::Google::V3

于 2013-06-06T22:27:02.270 回答
1

答案是 Geo::Coder::Google 没有为反向查找实现latlng参数。所以你不能用它来做这件事。

但是,添加反向查找功能将非常简单。

于 2013-01-09T05:21:52.690 回答
0

回答:

  1. 下载 CPANGeo::Coder::Google对象。
  2. 导航到 V3.pm 对象。
  3. 将此模块放入代码中。

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

然后像这样使用它:

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

然后你可以像这样访问返回的对象:

 print $location->{formatted_address};

要查看地址的详细部分,请参阅以下链接作为指南: https ://developers.google.com/maps/documentation/geocoding/

于 2013-01-12T00:22:41.413 回答