【问题标题】:How to make Geo::Coder::Google run even if input location doesn't exist即使输入位置不存在,如何使 Geo::Coder::Google 运行
【发布时间】:2013-02-28 11:50:02
【问题描述】:

我正在尝试使用Geo::Coder::Google 从位置数组中获取坐标列表。我的问题是位置数组是由另一个脚本生成的,该脚本有时会在其中放入一些在谷歌地图中找不到的奇怪位置,即 CorseMétéo

这会生成以下错误消息:

"Google Maps API returned error: 500 Can't connect to maps.google.com:80 (Bad hostname) at geoTest.pl line 24.". 

我的代码如下所示:

#!/usr/bin/perl -w

use strict;
use locale;
use warnings;
#use diagnostics;
use utf8;

binmode(STDIN, "encoding(utf8)");
binmode(STDOUT, "encoding(utf8)");
binmode(STDERR, "encoding(utf8)");

use Geo::Coder::Google;

my @place = ('Daluis', 'Corse', 'CorseMétéo');
my ($long, $lat);

foreach my $place(@place){
     my $geocoder = Geo::Coder::Google->new(apikey => '{MyAPIkey}');

     my $response;
     until (defined $response){
         $response = $geocoder->geocode(location => $place);
         }
     ($long, $lat) = @{ $response->{Point}{coordinates} };
     print "$long\n";
     print "$lat\n";
 }

通常这个 perl 模块用于地理定位街道地址,但它似乎在更大的地理位置上运行得很好。

有人遇到过类似的问题吗?

谢谢。

【问题讨论】:

    标签: perl geolocation


    【解决方案1】:

    如果您希望代码在出现错误的情况下继续运行,请使用eval block

     until (defined $@ or defined $response){
         eval {
              $response = $geocoder->geocode(location => $place);
         }
     }
    
     if ($@)
     {
         #some error handling.
     }
    

    请注意,eval { BLOCK }eval "string of code" 不同。它不会在运行时编译代码,也不是安全问题。这只是一种简单的异常处理方式。

    【讨论】:

    • @Krishna,请参阅 eval perldoc.perl.org/functions/eval.html 的文档
    • 它仍然给我一个错误,它根本不起作用。我得到:在 geoTest.pl 第 29 行的 void 上下文中无用使用常量(#some 错误处理)。不能在 geoTest.pl 第 31 行使用未定义的值作为 ARRAY 引用。似乎 $@ 是冲突的($long, $lat) = @{ $response->{Point}{coordinates} }.
    【解决方案2】:

    我设法找到了让它继续工作的方法,它看起来像这样:

    #!/usr/bin/perl -w
    
    use strict;
    use locale;
    use warnings;
    #use diagnostics;
    use utf8;
    
    binmode(STDIN, "encoding(utf8)");
    binmode(STDOUT, "encoding(utf8)");
    binmode(STDERR, "encoding(utf8)");
    
    use Geo::Coder::Google;
    
    
    my @place = ('Daluis', 'Corse', 'CorseMétéo', 'New Delhi');
    my ($long, $lat);
    
    foreach my $place(@place){
        my $geocoder = Geo::Coder::Google->new(apikey => '{MyAPIkeyHere}');
    
        my $response;
        until (defined $response){
            eval{
                $response = $geocoder->geocode(location => $place);
                if ($@){
                    "Couldn't get location : $place\n";
                }else{
                    ($long, $lat) = @{ $response->{Point}{coordinates} };
                }
            }
    
        }
        print "$place\n";
        print "$long\n";
        print "$lat\n";     
    }
    

    现在的情况是,每次找不到位置时,都会将前一个位置的坐标推入其中。因此,之后只需摆脱重复项即可。我使用坐标来填充 JavaScript,以便使用 google maps API 生成位置图。 但是,代码仍然会产生错误输出:

    Useless use of a constant (Couldn't get location) in void context at get_LatLng.pl line 48.
    

    但代码现在可以使用。如果有人知道如何管理此错误,那就太好了。

    还是谢谢你!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多