0

使用 时Geo::Coder:Google,从地址到纬度/经度的映射反之亦然。

但是输出不适合直接访问具体字段,尤其是在将纬度/经度转换为地址时。

根据我的调试日志:

loc by lat 48.308472 / lon 14.284578 ---\ {#012 address_components [#012 [0] {#012 long_name "Nibelungenbrücke",#012 short_name "B129",#012 types [#012 [0] "route"#012 ]#012 },#012 [1] {#012 long_name "Landstraße",#012 short_name "Landstraße",#012 types [#012 [0] "neighborhood",#012 [1] "political"#012 ]#012 },#012 [2] {#012 long_name "Innenstadt",#012 short_name "Innenstadt",#012 types [#012 [0] "sublocality",#012 [1] "political"#012 ]#012 },#012 [3] {#012 long_name "Linz",#012 short_name "Linz",#012 types [#012 [0] "locality",#012 [1] "political"#012 ]#012 },#012 [4] {#012 long_name "Linz",#012 short_name "Linz",#012 types [#012 [0] "administrative_area_level_2",#012 [1] "political"#012 ]#012 },#012 [5] {#012 long_name "Upper Austria",#012 short_name "OÖ",#012 types [#012 [0] "administrative_area_level_1",#012 [1] "political"#012 ]#012 },#012 [6] {#012 long_name "Austria",#012 short_name "AT",#012 types [#012 [0] "country",#012 [1] "political"#012 ]#012 },#012 [7] {#012 long_name 4020,#012 short_name 4020,#012 types [#012 [0] "postal_code"#012 ]#012 }#012 ],#012 formatted_address "Nibelungenbrücke, 4020 Linz, Austria",#012 geometry {#012 bounds {#012 n

使用子字符串 subs 会让我发疯。目前,代码:

    sub map_latlon_to_loc($$){ my $lat = shift; my $lon = shift;
          $location = $geocoder_google->reverse_geocode(latlng => $lat.','.$lon);
          dbg("loc by lat ".$lat." / lon ".$lon." --- " . p $location);
    }

哪种具体方式/方法/子适合访问其中的属性$location

btwp指的是打印机模块

在CPAN上什么也没看到,这可以解决问题。它不应该只适用于美国。我的实施必须应对任何国家。

4

1 回答 1

3

你没有说你用什么来显示谷歌返回的数据(什么是dbgp?)但它肯定不是一个字符串:它是一个包含嵌套哈希和数组的复杂哈希。用于Data::Dump显示它会显示内容并使它们易于访问和操作。

该程序使用您自己的子程序的固定版本来获取格林威治皇家天文台的信息。然后它打印数组long_name中每一行的值。address_components它还转储整个数据结构 usingData::Dump以便您可以查看返回的内容。

如果您愿意,Data::Dumper也可以使用。它是 Perl 5 的核心模块,因此不需要安装,但它生成的文本布局不如Data::Dump.

use strict;
use warnings;

use Geo::Coder::Google;
use Data::Dump;

my $geocoder = Geo::Coder::Google->new(apiver => 3);

my $location = map_latlon_to_loc(51.4768777, 0);

my $address = $location->{address_components};
print $_->{long_name}, "\n" for @$address;
print "\n\n";
dd $location;

sub map_latlon_to_loc {
  my ($lat, $lng) = @_;
  $geocoder->reverse_geocode(latlng => "$lat,$lng");
}

输出

Blackheath Avenue
Greater London
United Kingdom
SE10 8XJ
London


{
  address_components => [
    {
      long_name => "Blackheath Avenue",
      short_name => "Blackheath Ave",
      types => ["route"],
    },
    {
      long_name => "Greater London",
      short_name => "Gt Lon",
      types => ["administrative_area_level_2", "political"],
    },
    {
      long_name => "United Kingdom",
      short_name => "GB",
      types => ["country", "political"],
    },
    {
      long_name => "SE10 8XJ",
      short_name => "SE10 8XJ",
      types => ["postal_code"],
    },
    { long_name => "London", short_name => "London", types => ["postal_town"] },
  ],
  formatted_address => "Blackheath Avenue, London SE10 8XJ, UK",
  geometry => {
    bounds        => {
                       northeast => { lat => 51.4770228, lng => 0.0005404 },
                       southwest => { lat => 51.4762273, lng => -0.0001147 },
                     },
    location      => { lat => 51.4766277, lng => 0.0002212 },
    location_type => "APPROXIMATE",
    viewport      => {
                       northeast => { lat => 51.4779740302915, lng => 0.00156183029150203 },
                       southwest => { lat => 51.4752760697085, lng => -0.00113613029150203 },
                     },
  },
  types => ["route"],
}
于 2013-10-21T14:23:18.530 回答