0

地名を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たが、うまくいきません。一度ご確認ください... ありがとうございます。

4

1 に答える 1

2

以下は私にとってはうまくいきます:

$data->results[0]->address_components[0]->long_name

オブジェクトが連想配列に変換されるように、 json_decodeの-parameterを有効にすることもできます。 例えばassoc

$data = json_decode(file_get_contents($url), true);
$data["results"][0]["address_components"][0]["long_name"]
于 2013-01-31T16:15:51.167 に答える