Google地图地理编码在浏览器中运行,但不是从PHP服务器运行

Hug*_*o H 3 php google-maps google-geocoding-api

当我从浏览器中获取Google Maps Geocoding API时,它正在运行,但不是来自我的PHP脚本.

工作:https://maps.google.com/maps/api/geocode/json?appens = false&key = AzazaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address = La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

退货:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "La Pérelle",
               "short_name" : "La Pérelle",
               "types" : [ "colloquial_area", "political" ]
            },
         ],
         "formatted_address" : "La Pérelle, 38380, France",
         // other stuffs
         "partial_match" : true,
         "place_id" : "ChIJZzDRRAX4ikcRnZ-vb1tnTgY",
         "types" : [ "colloquial_area", "political" ]
      }
   ],
   "status" : "OK"
}
Run Code Online (Sandbox Code Playgroud)

不工作:

<?php 
$url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France';
echo file_get_contents($url);
Run Code Online (Sandbox Code Playgroud)

返回:

{
   "results" : [],
   "status" : "ZERO_RESULTS"
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

更新:有些人也没有看到他们的浏览器有任何结果.谷歌可以利用我的立场来展示不错的结果吗?

mig*_*uev 5

(缺席)语言偏好最常见于此问题.

当您从浏览器发送API请求时:

https://maps.google.com/maps/api/geocode/json?address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France

可能会发生您的浏览器语言首选项设置为首选语言作为第一语言,这使您的浏览器包括发送Accept-Language标头以及所有请求.

Google地理编码API将检查languageGET参数,如果没有,它将检查Accept-Language标头作为备份.如果您的浏览器设置为首选法语内容,则应使上述请求等效

https://maps.google.com/maps/api/geocode/json?address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France&language=fr

对我来说,因为我的浏览器设置为更喜欢英语,第一个请求返回"La Perelle,73130 Saint-François-Longchamp,France",而第二个请求返回"LaPérelle,38380 Saint-Pierre-de-Chartreuse,France".

自2016年末引入新的地理编码器以来,该region参数不再是唯一影响结果的language参数,该参数确实会影响结果:

首选语言对API选择返回的结果集以及返回的顺序影响很小.地理编码器根据语言不同地解释缩写,例如街道类型的缩写,或者可能在一种语言中有效而在另一种语言中无效的同义词.例如,utca和tér是匈牙利语中的街道的同义词.

也就是说,如果"La Perelle,73130 Saint-François-Longchamp,France"对于上述查询(LaPérelle,Saint-Pierre-de-Chartreuse,法国)只是一个糟糕的结果,请考虑提交错误报告.


yiv*_*ivi 3

我认为与地区偏向有关。

\n\n

尝试添加region参数(尽管如果您有针对许多不同区域的请求,则在构建请求时可能需要预先计算该参数)。

\n\n

例如,对于来自西班牙马德里的我来说,此请求带来零结果:

\n\n
https://maps.google.com/maps/api/geocode/json?sensor=false&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France\n
Run Code Online (Sandbox Code Playgroud)\n\n

但是,如果我添加region=fr参数

\n\n
https://maps.google.com/maps/api/geocode/json?sensor=false&region=fr&key=AIzaSyDqIj_SXTf5Z5DgE_cvn5VF9h5NbuaiCbs&address=La%20P%C3%A9relle,%20Saint-Pierre-de-Chartreuse,%20France\n
Run Code Online (Sandbox Code Playgroud)\n\n

我得到:

\n\n
{\n   "results" : [\n      {\n         "address_components" : [\n            {\n               "long_name" : "Chartreuse Mountains",\n               "short_name" : "Chartreuse Mountains",\n               "types" : [ "establishment", "natural_feature" ]\n            },\n            {\n               "long_name" : "Saint-Pierre-d\'Entremont",\n               "short_name" : "Saint-Pierre-d\'Entremont",\n               "types" : [ "locality", "political" ]\n            },\n            {\n               "long_name" : "Isere",\n               "short_name" : "Isere",\n               "types" : [ "administrative_area_level_2", "political" ]\n            },\n            {\n               "long_name" : "Auvergne-Rh\xc3\xb4ne-Alpes",\n               "short_name" : "Auvergne-Rh\xc3\xb4ne-Alpes",\n               "types" : [ "administrative_area_level_1", "political" ]\n            },\n            {\n               "long_name" : "France",\n               "short_name" : "FR",\n               "types" : [ "country", "political" ]\n            },\n            {\n               "long_name" : "73670",\n               "short_name" : "73670",\n               "types" : [ "postal_code" ]\n            }\n         ],\n         "formatted_address" : "Chartreuse Mountains, 73670 Saint-Pierre-d\'Entremont, France",\n         "geometry" : {\n            "location" : {\n               "lat" : 45.3773525,\n               "lng" : 5.827970700000001\n            },\n            "location_type" : "APPROXIMATE",\n            "viewport" : {\n               "northeast" : {\n                  "lat" : 45.3787014802915,\n                  "lng" : 5.829319680291503\n               },\n               "southwest" : {\n                  "lat" : 45.3760035197085,\n                  "lng" : 5.826621719708499\n               }\n            }\n         },\n         "place_id" : "ChIJSXJWFav4ikcREQZmf4QdYMc",\n         "types" : [ "establishment", "natural_feature" ]\n      }\n   ],\n   "status" : "OK"\n}\n
Run Code Online (Sandbox Code Playgroud)\n