郵便番号/郵便番号(カナダ/米国)のGoogleジオコードから、ユーザーの都市、州/県、国を取得したいと思います。データをJSONとして取得します:https ://developers.google.com/maps/documentation/geocoding/#JSON
問題は、「address_components」の量/順序が常に同じであるとは限らないことです。私は次のように使用していました:$geocodedinfo['results'][0]['address_components'][3]['short_name'];
しかし、グーグルが「address_components」の下に余分な要素を追加することがあるため、それが必ずしも州/州であるとは限らないことにすぐに気付きました。
必要な詳細(つまり、市、州/県、国)についてのみ結果を解析する方法はありますか?
編集私はそれを取得したときに実際にJSONをデコードしますjson_decode($result, true)
Array
(
[results] => Array
(
[0] => Array
(
[address_components] => Array
(
[0] => Array
(
[long_name] => V2X 2P6
[short_name] => V2X 2P6
[types] => Array
(
[0] => postal_code
)
)
[1] => Array
(
[long_name] => Maple Ridge
[short_name] => Maple Ridge
[types] => Array
(
[0] => locality
[1] => political
)
)
[2] => Array
(
[long_name] => Maple Ridge
[short_name] => Maple Ridge
[types] => Array
(
[0] => administrative_area_level_3
[1] => political
)
)
[3] => Array
(
[long_name] => Greater Vancouver Regional District
[short_name] => Greater Vancouver Regional District
[types] => Array
(
[0] => administrative_area_level_2
[1] => political
)
)
[4] => Array
(
[long_name] => British Columbia
[short_name] => BC
[types] => Array
(
[0] => administrative_area_level_1
[1] => political
)
)
[5] => Array
(
[long_name] => Canada
[short_name] => CA
[types] => Array
(
[0] => country
[1] => political
)
)
)
[formatted_address] => Maple Ridge, BC V2X 2P6, Canada
[geometry] => Array
(
[bounds] => Array
(
[northeast] => Array
(
[lat] => 49.2214351
[lng] => -122.6577849
)
[southwest] => Array
(
[lat] => 49.219268
[lng] => -122.663613
)
)
[location] => Array
(
[lat] => 49.2202679
[lng] => -122.660587
)
[location_type] => APPROXIMATE
[viewport] => Array
(
[northeast] => Array
(
[lat] => 49.2217005303
[lng] => -122.6577849
)
[southwest] => Array
(
[lat] => 49.2190025697
[lng] => -122.663613
)
)
)
[types] => Array
(
[0] => postal_code
)
)
)
[status] => OK
)