PHP 中的响应地理编码与直接在 URL 导航栏中的响应地理编码不同(Google 地图 api v3 json)
我得到这个 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®ion=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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您指定
区域
,但这会导致区域偏差,但不会影响输出语言。为此,您需要指定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 thelanguage
parameter. From the API docs: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").