【问题标题】:Google Maps API - Geocode - Postman get ZERO_RESULTS vs Browser get OKGoogle Maps API - 地理编码 - 邮递员获得零结果与浏览器获得确定
【发布时间】:2019-11-23 19:49:13
【问题描述】:

请求:

https://maps.googleapis.com/maps/api/geocode/json?address=TV%20FRANCISCO%20VIGNOLI%2C%2053%2C%20CENTRO&components=administrative_area:SAQUAREMA|administrative_area:RJ|country:br&key=YOUR_API_KEY

得到结果:

{ "results": [], "status": "ZERO_RESULTS" }

但是当我使用浏览器时得到"status": "OK"

我错过了什么吗?

【问题讨论】:

  • 请不要发布(仅)代码或响应的图片。

标签: rest google-maps postman google-maps-api-2


【解决方案1】:

我相信诀窍在于您的浏览器在请求标头中添加了一些附加信息。例如,浏览器可能知道您的区域设置和语言,因此添加此信息可能有助于 Google 将结果偏向特定区域。

TV FRANCISCO VIGNOLI, 53, CENTRO 可能不会为说英语的人带来任何相关的结果,但它可以找到与说葡萄牙语的用户相关的东西。

在您的示例中,如果我从邮递员执行查询

https://maps.googleapis.com/maps/api/geocode/json?address=TV%20FRANCISCO%20VIGNOLI%2C%2053%2C%20CENTRO&key=MY_API_KEY

我得到零结果。

但如果我在请求中添加区域参数并将结果偏向巴西

https://maps.googleapis.com/maps/api/geocode/json?address=TV%20FRANCISCO%20VIGNOLI%2C%2053%2C%20CENTRO&region=BR&key=MY_API_KEY

我收到回复

{
  "results":[
    {
      "address_components":[
    {
      "long_name":"LOJA 01",
      "short_name":"LOJA 01",
      "types":[
        "subpremise"
      ]
    },
    {
      "long_name":"53",
      "short_name":"53",
      "types":[
        "street_number"
      ]
    },
    {
      "long_name":"Tv Francisco Vignoli",
      "short_name":"Tv Francisco Vignoli",
      "types":[
        "route"
      ]
    },
    {
      "long_name":"Centro",
      "short_name":"Centro",
      "types":[
        "political","sublocality","sublocality_level_1"
      ]
    },
    {
      "long_name":"Saquarema",
      "short_name":"Saquarema",
      "types":[
        "administrative_area_level_2","political"
      ]
    },
    {
      "long_name":"Rio de Janeiro",
      "short_name":"RJ",
      "types":[
        "administrative_area_level_1","political"
      ]
    },
    {
      "long_name":"Brazil",
      "short_name":"BR",
      "types":[
        "country","political"
      ]
    },
    {
      "long_name":"28990-774",
      "short_name":"28990-774",
      "types":[
        "postal_code"
      ]
    }
      ],
      "formatted_address":"Tv Francisco Vignoli, 53 - LOJA 01 - Centro, Saquarema - RJ, 28990-774, Brazil",
      "geometry":{
    "location":{
      "lat":-22.9325062,"lng":-42.4960983
    },
    "location_type":"ROOFTOP",
    "viewport":{
      "northeast":{
        "lat":-22.9311572197085,"lng":-42.4947493197085
      },
      "southwest":{
        "lat":-22.9338551802915,"lng":-42.4974472802915
      }
    }
      },
      "place_id":"ChIJbZ-VwgNflwARDpn0-ZA19Xs",
      "plus_code":{
    "compound_code":"3G83+XH Centro, Saquarema - State of Rio de Janeiro, Brazil","global_code":"589V3G83+XH"
      },
      "types":[
    "establishment","health","point_of_interest","store"
      ]
    }
  ],
  "status":"OK"
}

此时,您的浏览器似乎已经足够智能,可以将区域信息发送到 Google 后端服务器。

我希望我的回答能澄清你的疑问。

【讨论】:

  • 是的,这很简单,我没有考虑过。已经在这里工作了!谢谢@xomena
【解决方案2】:

您需要设置 Accept-Language 标题 '接受语言'=>'Fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3' 更多信息在这里https://developers.google.com/maps/documentation/javascript/localization

【讨论】:

    【解决方案3】:

    Your link 在我的浏览器中不起作用(当我添加自己的密钥时)。

    看起来地理编码器知道的地址是:

    电视。 Francisco Vignole, 53 - Campo Aviacao, Saquarema - RJ, 28990-000, 巴西

    This request 在我的浏览器和邮递员中都适用(当我添加密钥时):

    https://maps.googleapis.com/maps/api/geocode/json?address=Francisco%20Vignole,53%20Campo%20Aviacao&components=administrative_area:SAQUAREMA|administrative_area:RJ|country:br&key=
    

    【讨论】:

    • 您更改了 URL,没有 Tv.,我知道子地区是 Campo Aviacao 而不是 CENTRO。我试图了解使用浏览器的相同 URL 是如何工作的,并且使用任何其他工具都不会发出相同的请求
    • Your link 在我的浏览器中不起作用(当我添加有效密钥时,它返回 "status" : "ZERO_RESULTS")。请提供一个 minimal reproducible example 来证明您的问题。
    • 该链接对我有用,但它仍然是有效地址吗?当我向世界任何地方的有效地址发出请求时,我使用 Postman 和我的浏览器也会得到相同的结果。
    猜你喜欢
    • 1970-01-01
    • 2016-06-26
    • 2011-11-28
    • 2011-05-19
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2012-03-06
    • 1970-01-01
    相关资源
    最近更新 更多