使用Perl和Google Maps API进行反向地理编码

时间:2013-01-09 04:40:29

标签: perl google-maps cpan reverse-geocoding

存在Perl模块以便与Google Maps API连接。代码如下:

 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,但这是不准确的。谷歌地图似乎要好得多。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

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

答案 2 :(得分:0)

答案:

  1. 下载CPAN Geo::Coder::Google对象。
  2. 导航到V3.pm对象。
  3. 将此模块放入代码中。
  4. 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/