PHP 中的响应地理编码与直接在 URL 导航栏中的响应地理编码不同(Google 地图 api v3 json)

发布于 2024-12-21 08:55:30 字数 966 浏览 4 评论 0原文

如果我输入以下网址: http://maps.googleapis.com/maps/api/geocode/json?address=la%20coru%C3%B1a,%20la%20coru%C3%B1a,%20espa%C3%B1a& Sensor=false&region=es

我得到这个 json 对象:

{   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "A Coruña",
           "short_name" : "A Coruña",
           "types" : [ "locality", "political" ]
        }, etc...

但是如果我用 PHP 尝试:

<?php $address = urlencode("la coruña, la coruña, España"); $geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&sensor=false&region=es'); echo "<pre>"; print_r($geocode); echo "</pre>";?>

响应不相等。

你可以看到它不相等: Corunna != Coruña

我一直在处理 UTF-8 但我没有得到解决方案。

If I put this URL: http://maps.googleapis.com/maps/api/geocode/json?address=la%20coru%C3%B1a,%20la%20coru%C3%B1a,%20espa%C3%B1a&sensor=false®ion=es

I get this json object:

{   "results" : [
  {
     "address_components" : [
        {
           "long_name" : "A Coruña",
           "short_name" : "A Coruña",
           "types" : [ "locality", "political" ]
        }, etc...

But if I try it with PHP:

<?php $address = urlencode("la coruña, la coruña, España"); $geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&sensor=false®ion=es'); echo "<pre>"; print_r($geocode); echo "</pre>";?>

The response is not equal.

You can see that it's not equal: Corunna != Coruña

I have been dealing with UTF-8 but I don't get a solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

メ斷腸人バ 2024-12-28 08:55:30

您指定区域,但这会导致区域偏差,但不会影响输出语言。为此,您需要指定 language 参数。来自 API 文档

  • 语言(可选)- 如果未提供语言,地理编码器将尝试使用域的本地语言
    只要有可能,就会发送请求。

我猜测运行 PHP 代码的服务器与您所在的国家/地区不同(或者 Google 认为是)。这就是为什么从服务器请求和从浏览器请求时会得到不同的结果。

事实上,当我点击您提供的网址时,我得到了英文版本(“Corunna”)输出,大概是因为我在美国。如果我将 &language=es 添加到网址,我会得到西班牙语版本(“A Coruña”)。

You specify the region, but that causes region biasing, but doesn't affect the output language. For that, you need to specify the language parameter. From the API docs:

  • language (optional) — If language is not supplied, the geocoder will attempt to use the native language of the domain from
    which the request is sent wherever possible.

I would guess that the server that runs the PHP code is in a different country than you are (or Google thinks it is). That's why you get different results when requesting from the server and requesting from your browser.

In fact, when I click on the url you provide, I get the english version ("Corunna") output, presumably because I am in the United States. If I add &language=es to the url I get the spanish version ("A Coruña").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文