Google Places API结果与Google搜索不匹配

时间:2014-10-24 03:40:57

标签: google-maps google-places-api google-search

在我的数据中,我发现了许多如下例子。如果您在Google搜索" Cheyney University PA",您会收到以下信息:

  

地址:1837 University Cir,Cheyney,PA 19319
  电话:(610)399-2000

但是,使用以下内容进行搜索时:

my $pid_url = Mojo::URL->new('https://maps.googleapis.com/maps/api/place/textsearch/json');
$pid_url->query({query => $query, key => 'removed'});

my $place_id = $ua->get($pid_url)->res->json->{results}->[0]->{place_id};

# Query = https://maps.googleapis.com/maps/api/place/textsearch/json?query=Cheyney+University+PA&key=removed
# Place ID returned = ChIJ5wiPkSvuxokR2rwaVUTMwks

$deets_url = Mojo::URL->new('https://maps.googleapis.com/maps/api/place/details/json');
$deets_url->query({placeid => $place_id, key => 'removed'});
say $ua->get($deets_url)->res->body;

我从谷歌那里得到了以下回复:

{
   "result" : {
      "address_components" : [
         {
            "long_name" : "Cheyney",
            "short_name" : "Cheyney",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "Thornbury",
            "short_name" : "Thornbury",
            "types" : [ "administrative_area_level_3", "political" ]
         },
         {
            "long_name" : "Delaware County",
            "short_name" : "Delaware County",
            "types" : [ "administrative_area_level_2", "political" ]
         },
         {
            "long_name" : "Pennsylvania",
            "short_name" : "PA",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "United States",
            "short_name" : "US",
            "types" : [ "country", "political" ]
         }
      ],
      "formatted_address" : "Cheyney, PA, USA",
      "name" : "Cheyney",
      "place_id" : "ChIJ5wiPkSvuxokR2rwaVUTMwks",
      "scope" : "GOOGLE",
      "types" : [ "locality", "political" ],
      "url" : "https://maps.google.com/maps/place?q=Cheyney,+PA,+USA&ftid=0x89c6ee2b918f08e7:0x4bc2cc44551abcda",
      "vicinity" : "Cheyney"
   },
   "status" : "OK"
}

(为清楚起见,我删除了一些输出。)请注意,结果中缺少街道地址组件(1837 University Cir)以及电话号码。我发现了很多像这样的实例,我试图找出为什么我的查询没有返回与Google获取信息时相同的结果。

这是另一个例子:

Google搜索结果:

  

地址:1320 S Dixie Hwy,Coral Gables,FL 33124
  电话:(305)284-2211

Places API结果:

{
   "html_attributions" : [],
   "results" : [
      {
         "formatted_address" : "University of Miami, Coral Gables, FL 33146, USA",
         "geometry" : {
            "location" : {
               "lat" : 25.7216374,
               "lng" : -80.2792843
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 25.7316894,
                  "lng" : -80.26327689999999
               },
               "southwest" : {
                  "lat" : 25.7115846,
                  "lng" : -80.29529169999999
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
         "id" : "7eb529034293a6eb760e990e034a4314d83edcf7",
         "name" : "University of Miami",
         "place_id" : "ChIJn9K4n_C32YgRqR37uAXB5IM",
         "reference" : "CqQBnAAAACffq3Xp3JUFowUDlEsge90htrhAoUES9tGmsB919iNQCxgldtq4khP4f_gCAju90qGY1IEUH52hZl_GP_H6xJC5z22EceCfprWEHFhhXKsW23Y9EeV24svXC-PRYnOyQaXZmbQFCeUlsmKWB6570e-8W_w31muZKwFd4hVPdeeqO45zH2zp7CAuSrHjBr0bSiOQufEB59ibxG4QcuwenSASEHvq1xMcCCtMUsFcyLYsrOIaFN0vg6S680hJay1jDSrZ686Mt9Gn",
         "types" : [ "sublocality_level_1", "sublocality", "political" ]
      }
   ],
   "status" : "OK"
}

{
   "html_attributions" : [],
   "result" : {
      "address_components" : [
         {
            "long_name" : "University of Miami",
            "short_name" : "University of Miami",
            "types" : [ "sublocality_level_1", "sublocality", "political" ]
         },
         {
            "long_name" : "Coral Gables",
            "short_name" : "Coral Gables",
            "types" : [ "locality", "political" ]
         },
         {
            "long_name" : "Miami-Dade County",
            "short_name" : "Miami-Dade County",
            "types" : [ "administrative_area_level_2", "political" ]
         },
         {
            "long_name" : "Florida",
            "short_name" : "FL",
            "types" : [ "administrative_area_level_1", "political" ]
         },
         {
            "long_name" : "United States",
            "short_name" : "US",
            "types" : [ "country", "political" ]
         },
         {
            "long_name" : "33146",
            "short_name" : "33146",
            "types" : [ "postal_code" ]
         }
      ],
      "adr_address" : "University of Miami, \u003cspan class=\"locality\"\u003eCoral Gables\u003c/span\u003e, \u003cspan class=\"region\"\u003eFL\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e33146\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUSA\u003c/span\u003e",
      "formatted_address" : "University of Miami, Coral Gables, FL 33146, USA",
      "geometry" : {
         "location" : {
            "lat" : 25.7216374,
            "lng" : -80.2792843
         },
         "viewport" : {
            "northeast" : {
               "lat" : 25.7316894,
               "lng" : -80.26327689999999
            },
            "southwest" : {
               "lat" : 25.7115846,
               "lng" : -80.29529169999999
            }
         }
      },
      "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
      "id" : "7eb529034293a6eb760e990e034a4314d83edcf7",
      "name" : "University of Miami",
      "place_id" : "ChIJn9K4n_C32YgRqR37uAXB5IM",
      "reference" : "CqQBlAAAAFeKC7LbGqAr7-LnBXMqbMIhuyuUh22XBMRO11hADojDclomC-InGocKSStmZ8S-S1UVWC1qHAsZi3XSp7PiDOFBgZVMqhm5kr87CfEAJmPdwsmZ6VEg5FxXh5LZmSSZdFgH0UR0OVGLL5M17SmWZp4W3Die8UNa5yCMyV5CP0T0VWmvqCb54qJR6mlyIx1itiLzRp1XhTs8_UmEt9pjIkYSEGrAqS6TWo2G1WBOrDO0YSAaFO4FfqvmFabJLia36vKL0XTz23Mu",
      "scope" : "GOOGLE",
      "types" : [ "sublocality_level_1", "sublocality", "political" ],
      "url" : "https://maps.google.com/maps/place?q=University+of+Miami,+Coral+Gables,+FL+33146,+USA&ftid=0x88d9b7f09fb8d29f:0x83e4c105b8fb1da9",
      "vicinity" : "University of Miami"
   },
   "status" : "OK"
}

1 个答案:

答案 0 :(得分:1)

您似乎想要使用Google地图进行某种地址验证和解析。遗憾的是,谷歌地图无法回复以一致地返回已解析的地址组件。 Google地图确实做得很好的是地理编码 - 即将输入转换为lat / long以显示在地图上。您也许可以尝试反向地理编码(即给予谷歌拉特/长)以查看返回的内容,但我很确定您会得到类似的结果。

过去我自己也曾想过并试图使用Google地图。经过对各种国际,现实世界数据集的大量测试后,我得出结论认为谷歌不能用于验证(a)获得纬度/经度(b)一般是/否可以解决看起来好看的问题。我还尝试过其他一些地理编码服务提供商,后来在GeocodeFarm解决了这个问题,因为一旦我的网站超出他们之前免费提供的东西,谷歌就证明太贵了。我的一般结论是相同的 - 我不得不坚持相当传统的地址数据输入表格,没有'全面'验证,否则用户会被告知地址错误,实际上是有效的。 GeocodeFarm虽然做得很好“打印”本地化地址(即几乎适合作为邮件标签放在信封上),但它没有被解析成组件以存储到典型的关系数据库模式中。

进行更多研究以验证自己的体验,我发现thisthis很有用。这些链接还指向一个名为“SmartyStreets”的服务 - 但我无法保证它,并且没有使用它的经验。

希望有所帮助!