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

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

问题描述:

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&region=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&region=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.

如果我输入此网址: http:// maps .googleapis.com /地图/ API /地理编码/ JSON地址= LA%20coru%C3%菌素B1a,%20LA%20coru%C3%菌素B1a,%20espa%C3%菌素B1a&安培;传感器=假安培;区域= ES p>

我得到这个json对象: p>

  {“results”:[
 {
“address_components”:[
 {  
“long_name”:“ACoruña”,
“short_name”:“ACoruña”,
“类型”:[“locality”,“political”] 
}等等... 
  code  >  pre> 
 
 

但如果我尝试使用PHP: p>

 &lt;?php $ address = urlencode(“lacoruña,lacoruña, 西班牙“);  $ geocode = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&sensor=false&region=es');  echo“&lt; pre&gt;”; 的print_r($地理编码);  echo“&lt; / pre&gt;”;?&gt; 
  code>  pre> 
 
 

响应不相等。 p>

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

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

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").