使用Perl和Google Maps API进行反向地理编码 [英] Reverse Geocoding using Perl and Google Maps API

查看:129
本文介绍了使用Perl和Google Maps API进行反向地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

存在一个Perl模块以便与Google Maps API进行交互。代码如下:

 使用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,但它不准确。 Google地图似乎好多了。

解决方案

答案:


  1. 下载CPAN Geo :: Coder :: Google 对象。

  2. 浏览至V3。 pm对象。
  3. 将此模块放入代码中。

sub reverseGeocode {my $ self = shift;

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



$ b my $ location = $ param {location}
或Carp :: croak(用法:reverseGeocode(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}? '真假';
$ 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);
#签名必须是查询字符串中的最后一个参数,否则您将获得403的
$ url = $ uri-> as_string;
$ url。='& signature ='。$ signature if $ signature;
}

然后就像这样使用它:

  my $ location = $ geocoder-> reverseGeocode(location => '40 .7837366863403,-73.9882784482727'); 

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

  print $ location-> {formatted_address}; 

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


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.

解决方案

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/

这篇关于使用Perl和Google Maps API进行反向地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆