我想知道Lat / Lng的地名。为此我找到了Google API。我写的程序是
<?php
$url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng=13.74349%2C100.489067&sensor=false';
$data = json_decode(file_get_contents($url));
echo '<pre>';
print_r($data);
?>
输出如下
Array
(
[0] => stdClass Object
(
[results] => Array
(
[0] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Wang Doem 8
[short_name] => Wang Doem 8
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[long_name] => Wat Arun
[short_name] => Wat Arun
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Bangkok Yai
[short_name] => Bangkok Yai
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
[5] => stdClass Object
(
[long_name] => 10600
[short_name] => 10600
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Wang Doem 8, Wat Arun, Bangkok Yai, Bangkok 10600, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7448616
[lng] => 100.4888865
)
[southwest] => stdClass Object
(
[lat] => 13.7445018
[lng] => 100.4876895
)
)
[location] => stdClass Object
(
[lat] => 13.7446807
[lng] => 100.4882883
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7460306803
[lng] => 100.48963698
)
[southwest] => stdClass Object
(
[lat] => 13.7433327197
[lng] => 100.48693902
)
)
)
[types] => Array
(
[0] => route
)
)
[1] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Wat Arun
[short_name] => Wat Arun
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Bangkok Yai
[short_name] => Bangkok Yai
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => 10600
[short_name] => 10600
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Wat Arun, Bangkok Yai, Bangkok 10600, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.4919267
)
[southwest] => stdClass Object
(
[lat] => 13.7361885
[lng] => 100.4803822
)
)
[location] => stdClass Object
(
[lat] => 13.7434901
[lng] => 100.4890647
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.4919267
)
[southwest] => stdClass Object
(
[lat] => 13.7361885
[lng] => 100.4803822
)
)
)
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Bangkok Yai
[short_name] => Bangkok Yai
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => 10600
[short_name] => 10600
[types] => Array
(
[0] => postal_code
)
)
)
[formatted_address] => Bangkok Yai, Bangkok 10600, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.4919267
)
[southwest] => stdClass Object
(
[lat] => 13.722343
[lng] => 100.4615641
)
)
[location] => stdClass Object
(
[lat] => 13.7341183
[lng] => 100.4761897
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.4919267
)
[southwest] => stdClass Object
(
[lat] => 13.722343
[lng] => 100.4615641
)
)
)
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[3] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => 10600
[short_name] => 10600
[types] => Array
(
[0] => postal_code
)
)
[1] => stdClass Object
(
[long_name] => Wat Tha Phra
[short_name] => Wat Tha Phra
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[2] => stdClass Object
(
[long_name] => Bangkok Yai
[short_name] => Bangkok Yai
[types] => Array
(
[0] => sublocality
[1] => political
)
)
[3] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[4] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Wat Tha Phra, Bangkok Yai, Bangkok 10600, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.5127718
)
[southwest] => stdClass Object
(
[lat] => 13.694656
[lng] => 100.4615641
)
)
[location] => stdClass Object
(
[lat] => 13.7247046
[lng] => 100.4958678
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.7481218
[lng] => 100.5127718
)
[southwest] => stdClass Object
(
[lat] => 13.694656
[lng] => 100.4615641
)
)
)
[types] => Array
(
[0] => postal_code
)
)
[4] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => locality
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Bangkok, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.9551109
[lng] => 100.9384077
)
[southwest] => stdClass Object
(
[lat] => 13.4940968
[lng] => 100.3277368
)
)
[location] => stdClass Object
(
[lat] => 13.7522222
[lng] => 100.4938889
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.9551109
[lng] => 100.9384077
)
[southwest] => stdClass Object
(
[lat] => 13.4940968
[lng] => 100.3277368
)
)
)
[types] => Array
(
[0] => locality
[1] => political
)
)
[5] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Bangkok
[short_name] => Bangkok
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[1] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Bangkok, Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.9551109
[lng] => 100.9384077
)
[southwest] => stdClass Object
(
[lat] => 13.4940968
[lng] => 100.3277368
)
)
[location] => stdClass Object
(
[lat] => 13.7234186
[lng] => 100.4762319
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 13.9551109
[lng] => 100.9384077
)
[southwest] => stdClass Object
(
[lat] => 13.4940968
[lng] => 100.3277368
)
)
)
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[6] => stdClass Object
(
[address_components] => Array
(
[0] => stdClass Object
(
[long_name] => Thailand
[short_name] => TH
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Thailand
[geometry] => stdClass Object
(
[bounds] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 20.465143
[lng] => 105.636812
)
[southwest] => stdClass Object
(
[lat] => 5.612851
[lng] => 97.343396
)
)
[location] => stdClass Object
(
[lat] => 15.870032
[lng] => 100.992541
)
[location_type] => APPROXIMATE
[viewport] => stdClass Object
(
[northeast] => stdClass Object
(
[lat] => 20.465143
[lng] => 105.636812
)
[southwest] => stdClass Object
(
[lat] => 5.612851
[lng] => 97.343396
)
)
)
[types] => Array
(
[0] => country
[1] => political
)
)
)
[status] => OK
)
)
我不知道如何抓住顶部的address_components
。我尝试使用$data[0]->results[0]->address_components
等等。但它不起作用。请检查一下......谢谢。
答案 0 :(得分:2)
以下适用于我:
$data->results[0]->address_components[0]->long_name
您可能还想启用json_decode的assoc
- 参数,以便将对象转换为关联数组。
E.g。
$data = json_decode(file_get_contents($url), true);
$data["results"][0]["address_components"][0]["long_name"]