0

我们在我们的网站中有一个嵌入式搜索栏,它利用Google Maps API提供建议和结果。当用户搜索“纽约”,明确对建议点击这表明他们正在寻找纽约州这个API调用时:当查询Google Maps API时,我得到了“纽约,美国”和“纽约,纽约,美国”的相同结果

https://maps.googleapis.com/maps/api/js/GeocodeService.Search?4sNew%20York%2C%20United%20States&7sUS&8m2&1scountry&2sUS&9sen-US&callback=xdc._ohwoca&token=44216

当他们只需键入“纽约”和按下回车键,或选择其表示“纽约,纽约”的建议,该API调用时:

https://maps.googleapis.com/maps/api/js/GeocodeService.Search?4sNew%20York%2C%20NY%2C%20United%20States&7sUS&8m2&1scountry&2sUS&9sen-US&callback=xdc._n2ga8u&token=62787

的问题是,返回的JSON是两者相同的,回调的例外标题:

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "New York", 
       "short_name" : "New York", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "New York", 
       "short_name" : "NY", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "United States", 
       "short_name" : "US", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "New York, NY, USA", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : 40.9175771, 
        "lng" : -73.70027209999999 
       }, 
       "southwest" : { 
        "lat" : 40.4773991, 
        "lng" : -74.25908989999999 
       } 
      }, 
      "location" : { 
       "lat" : 40.7127837, 
       "lng" : -74.0059413 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : 40.9152555, 
        "lng" : -73.70027209999999 
       }, 
       "southwest" : { 
        "lat" : 40.4960439, 
        "lng" : -74.25573489999999 
       } 
      } 
     }, 
     "place_id" : "ChIJOwg_06VPwokRYv534QaPC8g", 
     "types" : [ "locality", "political" ] 
     } 
    ], 
    "status" : "OK" 
} 

这会导致不正确的搜索结果提供给我们的用户。具体而言,我们键入"types"字段。由于两份文件均指明用户搜索“地点”,因此我们总是返回纽约市的结果,而不是纽约州可能已指定的结果。

这是Google Maps API中的错误吗?我们是否需要指定一些标志来强制更严格地解释我们发送的内容?其他想法?

请注意,如果您在面向www.google.com/maps的用户上执行此搜索,您将获得纽约州而不是纽约市。我不太了解该页面与API的不同之处,以了解其中发生了什么。

+0

请提供[最小,完整和可验证的示例](http://stackoverflow.com/help/mcve),它允许重现此问题。你如何得到这些结果?你使用的是什么版本的API?我没有得到相同的结果。 – MrUpsidown

+0

@MrUpsidown我假设我的链接确实提供了重现问题的方法。我们在maps.googleapis.com/maps-api-v3/api/js/27/12/上使用该API的版本。此外,您可以在Google自己的API文档中尝试任何非纽约州状态:https://developers.google.com/maps/documentation/geocoding/intro,您将看到该状态。如果你输入“纽约”或“纽约”,你会回到城市而不是国家。 – Necoras

回答

1

我最终打开了一个issue与谷歌地图团队。根据他们的工程师,这是预期的行为。他们认为,大多数用户打算在搜索“纽约”时特意看到“纽约市”,而不是国家。如果我们的系统中没有额外的“城市”参数指定“纽约”,我们最终会捕捉用户的查询并强制在我们的系统中进行州搜索。