如何解析Google地图地理编码结果

编程入门 行业动态 更新时间:2024-10-27 06:29:11
本文介绍了如何解析Google地图地理编码结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用地理编码从给定的经度和纬度检索地理位置地址,使用网站上的示例检索 maps.googleapis/maps/api/geocode/json?latlng=40.714224,- 73.961452& sensor = true 在这个例子中,我只需要城市名称纽约,国家名称即美国。但问题是这里有多个表达的词作为long_name。 我的问题如何在php中解析这个json / xml以获取城市名称和国家。

$ url = file_get_contents('maps.googleapis/maps/api/geocode/xml?latlng=40.714 224,-73.961452& sensor = true'); $ xml = simplexml_load_string($ url); $ tracks = $ xml->结果; $ temp; $城市; $国家; foreach($ track为$ key) { foreach($ key-> address_component as $ val) { $ temp = $ val-> LONG_NAME; if($ val-> type ==locality) { $ city = $ temp; } else if($ val-> type ==country) { $ country = $ temp; } } } echo $ country。< br />; echo $ city; <?xml version =1.0encoding =UTF-8?> < GeocodeResponse> < status>确定< / status> <结果> < type> street_address< / type> < formatted_address>美国纽约布鲁克林区贝德福德大街277号,邮编:11211< / formatted_address> < address_component> < long_name> 277< / long_name> <短名称> 277< /短名称> < type> street_number< / type> < / address_component> < address_component> < long_name>贝德福德大道< / long_name> < short_name> Bedford Ave< / short_name> < type>路由< / type> < / address_component> < address_component> < long_name>威廉斯堡< / long_name> < short_name>威廉斯堡< / short_name> < type>邻里< / type> < type>政治< / type> < / address_component> < address_component> < long_name>布鲁克林< / long_name> < short_name>布鲁克林< / short_name> < type> sublocality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>国王< / long_name> <短名称>国王< /短名称> < type> administrative_area_level_2< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> < address_component> < long_name> 11211< / long_name> <短名称> 11211< /短名称> < type> postal_code< / type> < / address_component> <几何> < location> < lat> 40.7142320< / lat> < lng> -73.9612889< / lng> < / location> < location_type> ROOFTOP< / location_type> < viewport> < lat> 40.7128830< / lat> < lng> -73.9626379< / lng> < / southwest> <东北> < lat> 40.7155810< / lat> < lng> -73.9599399< / lng> < / northeast> < / viewport> < / geometry> < / result> <结果> < type> bus_station< / type> < type> transit_station< / type> < type>建立< / type> < formatted_address> Grand St / Bedford Av,Brooklyn,New York 11211,USA< / formatted_address> < address_component> < long_name> Grand St / Bedford Av< / long_name> < short_name> Grand St / Bedford Av< / short_name> < type> bus_station< / type> < type> transit_station< / type> < type>建立< / type> < / address_component> < address_component> < long_name>威廉斯堡< / long_name> < short_name>威廉斯堡< / short_name> < type>邻里< / type> < type>政治< / type> < / address_component> < address_component> < long_name>布鲁克林< / long_name> < short_name>布鲁克林< / short_name> < type> sublocality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>国王< / long_name> <短名称>国王< /短名称> < type> administrative_area_level_2< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> < address_component> < long_name> 11211< / long_name> <短名称> 11211< /短名称> < type> postal_code< / type> < / address_component> <几何> < location> < lat> 40.7143210< / lat> < lng> -73.9611510< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.7129720< / lat> < lng> -73.9625000< / lng> < / southwest> <东北> < lat> 40.7156700< / lat> < lng> -73.9598020< / lng> < / northeast> < / viewport> < / geometry> < / result> <结果> < type> bus_station< / type> < type> transit_station< / type> < type>建立< / type> < formatted_address> Grand St / Bedford Av,Brooklyn,New York 11249,USA< / formatted_address> < address_component> < long_name> Grand St / Bedford Av< / long_name> < short_name> Grand St / Bedford Av< / short_name> < type> bus_station< / type> < type> transit_station< / type> < type>建立< / type> < / address_component> < address_component> < long_name>威廉斯堡< / long_name> < short_name>威廉斯堡< / short_name> < type>邻里< / type> < type>政治< / type> < / address_component> < address_component> < long_name>布鲁克林< / long_name> < short_name>布鲁克林< / short_name> < type> sublocality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>国王< / long_name> <短名称>国王< /短名称> < type> administrative_area_level_2< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> < address_component> < long_name> 11249< / long_name> < short_name> 11249< / short_name> < type> postal_code< / type> < / address_component> <几何> < location> < lat> 40.7146840< / lat> < lng> -73.9615630< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.7133350< / lat> < lng> -73.9629120< / lng> < / southwest> <东北> < lat> 40.7160330< / lat> < lng> -73.9602140< / lng> < / northeast> < / viewport> < / geometry> < / result> <结果> < type> bus_station< / type> < type> transit_station< / type> < type>建立< / type> < formatted_address> Bedford Av / Grand St,Brooklyn,New York 11211,USA< / formatted_address> < address_component> < long_name> Bedford Av / Grand St< / long_name> < short_name> Bedford Av / Grand St< / short_name> < type> bus_station< / type> < type> transit_station< / type> < type>建立< / type> < / address_component> < address_component> < long_name>威廉斯堡< / long_name> < short_name>威廉斯堡< / short_name> < type>邻里< / type> < type>政治< / type> < / address_component> < address_component> < long_name>布鲁克林< / long_name> < short_name>布鲁克林< / short_name> < type> sublocality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>国王< / long_name> <短名称>国王< /短名称> < type> administrative_area_level_2< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> < address_component> < long_name> 11211< / long_name> <短名称> 11211< /短名称> < type> postal_code< / type> < / address_component> <几何> < location> < lat> 40.7147100< / lat> < lng> -73.9609990< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.7133610< / lat> < lng> -73.9623480< / lng> < / southwest> <东北> < lat> 40.7160590< / lat> < lng> -73.9596500< / lng> < / northeast> < / viewport> < / geometry> < / result> <结果> < type> postal_code< / type> < formatted_address>布鲁克林,纽约11211,美国< / formatted_address> < address_component> < long_name> 11211< / long_name> <短名称> 11211< /短名称> < type> postal_code< / type> < / address_component> < address_component> < long_name>布鲁克林< / long_name> < short_name>布鲁克林< / short_name> < type> sublocality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约市< / long_name> < short_name>纽约市< / short_name> < type> locality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> <几何> < location> < lat> 40.7093358< / lat> < lng> -73.9565551< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.7008331< / lat> < lng> -73.9644697< / lng> < / southwest> <东北> < lat> 40.7280089< / lat> < lng> -73.9207299< / lng> < / northeast> < / viewport> < bounds> < lat> 40.7008331< / lat> < lng> -73.9644697< / lng> < / southwest> <东北> < lat> 40.7280089< / lat> < lng> -73.9207299< / lng> < / northeast> < / bounds> < / geometry> < / result> <结果> < type>邻里< / type> < type>政治< / type> < formatted_address>威廉斯堡,布鲁克林,纽约,美国< / formatted_address> < address_component> < long_name>威廉斯堡< / long_name> < short_name>威廉斯堡< / short_name> < type>邻里< / type> < type>政治< / type> < / address_component> < address_component> < long_name>布鲁克林< / long_name> < short_name>布鲁克林< / short_name> < type> sublocality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约市< / long_name> < short_name>纽约市< / short_name> < type> locality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>国王< / long_name> <短名称>国王< /短名称> < type> administrative_area_level_2< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> <几何> < location> < lat> 40.7064461< / lat> < lng> -73.9536163< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.6979329< / lat> < lng> -73.9698450< / lng> < / southwest> <东北> < lat> 40.7251773< / lat> < lng> -73.9364980< / lng> < / northeast> < / viewport> < bounds> < lat> 40.6979329< / lat> < lng> -73.9698450< / lng> < / southwest> <东北> < lat> 40.7251773< / lat> < lng> -73.9364980< / lng> < / northeast> < / bounds> < / geometry> < / result> <结果> < type> administrative_area_level_2< / type> < type>政治< / type> < formatted_address> Kings,纽约,美国< / formatted_address> < address_component> < long_name>国王< / long_name> <短名称>国王< /短名称> < type> administrative_area_level_2< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> <几何> < location> < lat> 40.6528762< / lat> < lng> -73.9594940< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.5695637< / lat> < lng> -74.0429503< / lng> < / southwest> <东北> < lat> 40.7394460< / lat> < lng> -73.8333651< / lng> < / northeast> < / viewport> < bounds> < lat> 40.5695637< / lat> < lng> -74.0429503< / lng> < / southwest> <东北> < lat> 40.7394460< / lat> < lng> -73.8333651< / lng> < / northeast> < / bounds> < / geometry> < / result> <结果> < type> sublocality< / type> < type>政治< / type> < formatted_address>布鲁克林,纽约,美国< / formatted_address> < address_component> < long_name>布鲁克林< / long_name> < short_name>布鲁克林< / short_name> < type> sublocality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约市< / long_name> < short_name>纽约市< / short_name> < type> locality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>国王< / long_name> <短名称>国王< /短名称> < type> administrative_area_level_2< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> <几何> < location> < lat> 40.6500000< / lat> < lng> -73.9500000< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.5510420< / lat> < lng> -74.0566300< / lng> < / southwest> <东北> < lat> 40.7394460< / lat> < lng> -73.8333651< / lng> < / northeast> < / viewport> < bounds> < lat> 40.5510420< / lat> < lng> -74.0566300< / lng> < / southwest> <东北> < lat> 40.7394460< / lat> < lng> -73.8333651< / lng> < / northeast> < / bounds> < / geometry> < / result> <结果> < type> locality< / type> < type>政治< / type> < formatted_address>纽约,纽约,美国< / formatted_address> < address_component> < long_name>纽约市< / long_name> < short_name>纽约市< / short_name> < type> locality< / type> < type>政治< / type> < / address_component> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> <几何> < location> < lat> 40.7143528< / lat> < lng> -74.0059731< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.4960060< / lat> < lng> -74.2557349< / lng> < / southwest> <东北> < lat> 40.9152556< / lat> < lng> -73.7002721< / lng> < / northeast> < / viewport> < bounds> < lat> 40.4959961< / lat> < lng> -74.2590879< / lng> < / southwest> <东北> < lat> 40.9152556< / lat> < lng> -73.7002721< / lng> < / northeast> < / bounds> < / geometry> < / result> <结果> < type>政治< / type> < formatted_address>美国,新闻·新闻·新闻·新闻·新闻稿>< / formatted_address> < address_component> < long_name>新闻·新闻=新闻·新闻·新闻&新闻< / long_name> <短名称>新闻·新闻=新闻·新闻·新闻&新闻< /短名称> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> <几何> < location> < lat> 40.9590293< / lat> < lng> -74.0300122< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 39.4985330< / lat> < lng> -75.3585939< / lng> < / southwest> <东北> < lat> 41.6018065< / lat> < lng> -71.8562140< / lng> < / northeast> < / viewport> < bounds> < lat> 39.4985330< / lat> < lng> -75.3585939< / lng> < / southwest> <东北> < lat> 41.6018065< / lat> < lng> -71.8562140< / lng> < / northeast> < / bounds> < / geometry> < / result> <结果> < type> administrative_area_level_1< / type> < type>政治< / type> < formatted_address>纽约,美国< / formatted_address> < address_component> < long_name>纽约< / long_name> < short_name> NY< / short_name> < type> administrative_area_level_1< / type> < type>政治< / type> < / address_component> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> <几何> < location> < lat> 43.2994285< / lat> < lng> -74.2179326< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 40.4960191< / lat> < lng> -79.7621438< / lng> < / southwest> <东北> < lat> 45.0156269< / lat> < lng> -71.8562643< / lng> < / northeast> < / viewport> < bounds> < lat> 40.4959961< / lat> < lng> -79.7621438< / lng> < / southwest> <东北> < lat> 45.0158650< / lat> < lng> -71.8562643< / lng> < / northeast> < / bounds> < / geometry> < / result> <结果> < type>国家< / type> < type>政治< / type> < formatted_address>美国< / formatted_address> < address_component> < long_name>美国< / long_name> < short_name>美国< / short_name> < type>国家< / type> < type>政治< / type> < / address_component> <几何> < location> < lat> 37.0902400< / lat> < lng> -95.7128910< / lng> < / location> < location_type> APPROXIMATE< / location_type> < viewport> < lat> 25.8200000< / lat> < lng> -124.3900000< / lng> < / southwest> <东北> < lat> 49.3800000< / lat> < lng> -66.9400000< / lng> < / northeast> < / viewport> < bounds> < lat> -90.0000000< / lat> < lng> -180.0000000< / lng> < / southwest> <东北> < lat> 90.0000000< / lat> < lng> 180.0000000< / lng> < / northeast> < / bounds> < / geometry> < / result> < / GeocodeResponse>

解决方案

您可以在google响应中检查3个变量json)找到当前的城市名称。因此,有3种不同的名称解释:

国家 | AdministrativeArea | | _AdministrativeAreaName(cityname) | | _Locality_LocalityName(cityname) | | _SubAdministrativeArea | | _Locality_LocalityName(cityname) if($ this-> json->地标[0] - > AddressDetails-> Country-> ; AdministrativeArea-> Locality-> LocalityName!= NULL){ $ code ['cityname'] = $ this-> json->地标[0] - > AddressDetails-> ;国家 - > AdministrativeArea-> Locality->处所名称; } elseif($ this-> json-> Placemark [0] - > AddressDetails-> Country-> AdministrativeArea-> SubAdministrativeArea-> Locality-> LocalityName!= NULL){ $ code ['cityname'] = $ this-> json->地标[0] - > AddressDetails-> Country-> AdministrativeArea-> SubAdministrativeArea-> ; Locality->处所名称; $ b

如果你有更多的地标,你需要遍历所有数组。

results:[ {address_components:[ {long_name:277,short_name:277,types:[street_number] }, { long_name:Bedford Avenue,short_name:Bedford Ave,types:[route] }, { long_name:Williamsburg,short_name:Williamsburg,types:[neighborhood,political] }, {long_name:Brooklyn,short_name:Brooklyn,types:[sublocality,political] }, { long_name:Kings,short_name:Kings,types:[administrative_area_level_2,political] }, {long_name:纽约,short_name:NY,types:[administrative_area_level_1,political] }, {long_name:USA,short_name:US,types:[country,political] },$ b $b { \"long_name\" : \"11211\", \"short_name\" : \"11211\", \"types\" : [ \"postal_code\" ] } ] , \"formatted_address\" : \"277 Bedford Avenue, Brooklyn, New York 11211, USA\", \"geometry\" : { \"location\" : { \"lat\" : 40.714232 , \"lng\" : -73.9612889 }, \"location_type\" : \"ROOFTOP\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.7155809802915, \"lng\" : -73.9599399197085 }, \"southwest\" : { \"lat\" : 40.7128830197085, \"lng\" : -73.96263788029151 } } }, \"types\" : [ \"street_address\" ] }, { \"address_components\" : [ { \"long_name\" : \"Grand St/Bedford Av\", \"short_name\" : \"Grand St/Bedford Av\", \"types\" : [ \"bus_station\", \"transit_station\", \"establishment\" ] }, { \"long_name\" : \"Williamsburg\", \"short_name\" : \"Williamsburg\", \"types\" : [ \"neighborhood\", \"political\" ] }, { \"long_name\" : \"Brooklyn\", \"short_name\" : \"Brooklyn\", \"types\" : [ \"sublocality\", \"political\" ] }, { \"long_name\" : \"Kings\", \"short_name\" : \"Kings\", \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] }, { \"long_name\" : \"11211\", \"short_name\" : \"11211\", \"types\" : [ \"postal_code\" ] } ], \"formatted_address\" : \"Grand St/Bedford Av, Brook lyn, New York 11211, USA\", \"geometry\" : { \"location\" : { \"lat\" : 40.714321, \"lng\" : -73.961151 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.71566998029149, \"lng\" : -73.95980201970849 }, \"southwest\" : { \"lat\" : 40.7129720197085, \"lng\" : -73.96249998029151 } } }, \"types\" : [ \"bus_station\", \"transit_station\", \"establishment\" ] }, { \"address_components\" : [ { \"long_name\" : \"Grand St/Bedford Av\", \"short_name\" : \"Grand St/Bedford Av\", \"types\" : [ \"bus_station\", \"transit_station\", \"establishment\" ] } , { \"long_name\" : \"Williamsburg\", \"short_name\" : \"Williamsburg\", \"types\" : [ \"neighborhood\", \"political\" ] }, { \"long_name\" : \"Brooklyn\", \"short_name\" : \"Brooklyn\", \"types\" : [ \"sublocality\", \"political\" ] }, { \"long_name\" : \"Kings\", \"short_name\" : \"Kings\", \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] }, { \"long_name\" : \"11249\", \"short_name\" : \"11249\", \"types\" : [ \"postal_code\" ] } ], \"formatted_address\" : \"Grand St/Bedford Av, Brooklyn, New York 11249, USA\", \"geometry\" : { \"location\" : { \"lat\" : 40.714684, \"lng\" : -73.961563 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.71603298029149, \"lng\" : -73.9602140197085 }, \"southwest\" : { \"lat\" : 40.7133350197085, \"lng\" : -73.96291198029151 } } }, \"types\" : [ \"bus_station\", \"transit_station\", \"establishment\" ] }, { \"address_components\" : [ $b$ b { \"long_name\" : \"Bedford Av/Grand St\", \"short_name\" : \"Bedford Av/Grand St\", \"types\" : [ \"bus_station\", \"transit_station\", \"establishment\" ] }, { \"long_name\" : \"Williamsburg\", \"short_name\" : \"Williamsburg\", \"types\" : [ \"neighborhood\", \"political\" ] }, { \"long_name\" : \"Brooklyn\", \"short_name\" : \"Brooklyn\", \"types\" : [ \"sublocality\", \"political\" ] }, { \"long_name\" : \"Kings\", \"short_name\" : \"Kings\", \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"administrative_area_level_1\" , \"political\" ] }, { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] }, { \"long_name\" : \"11211\", \"short_name\" : \"11211\", \"types\" : [ \"postal_code\" ] } ], \"formatted_address\" : \"Bedford Av/Grand St, Brooklyn, New York 11211, USA\", \"geometry\" : { \"location\" : { \"lat\" : 40.71471, \"lng\" : -73.960999 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.7160589802915, \"lng\" : -73.95965001970849 }, \"southwest\" : { \"lat\" : 40.7133610197085, \"lng\" : -73.9623479802915 } } }, \"types\" : [ \"bus_station\", \"transit_station\", \"establishment\" ] }, { \"address_components\" : [ { \"long_name\" : \"11211\", \"short_name\" : \"11211\", \"types\" : [ \"postal_code\" ] }, { \"long_name\" : \"Brooklyn\", \"short_name\" : \"Brooklyn\", \"types\" : [ \"sublocality\", \"political\" ] }, { \"long_name\" : \"New York City\", \"short_name\" : \"New York City\", \"types\" : [ \"locality\", \"political\" ] }, { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, $b $b { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"Brooklyn, New York 11211, USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 40.7280089, \"lng\" : -73.9207299 }, \"southwest\" : { \"lat\" : 40.7008331, \"lng\" : -73.9644697 } }, \"location\" : { \"lat\" : 40.7093358, \"lng\" : -73.9565551 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.7280089, \"lng\" : -73.9207299 }, \"southwest\" : { \"lat\" : 40.7008331, \"lng\" : -73.9644697 } } }, \"types\" : [ \"postal_code\" ] }, { \"address_components\" : [ { \"long_name\" : \"Williamsburg\", \"short_name\" : \"Williamsburg\", \"types\" : [ \"neighborhood\", \"political\" ] }, { \"long_name\" : \"Brooklyn\", \"short_name\" : \"Brooklyn\", \"types\" : [ \"sublocality\", \"political\" ] }, { \"long_name\" : \"New York City\", \"short_name\" : \"New York City\", \"types\" : [ \"locality\", \"political\" ] }, { \"long_name\" : \"Kings\", \"short_name\" : \"Kings\", \"ty pes\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"Williamsburg, Brooklyn, New York, USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 40.7251773, \"lng\" : -73.936498 }, \"southwest\" : { \"lat\" : 40.6979329, \"lng\" : -73.96984499999999 } }, \"location\" : { \"lat\" : 40.7064461, \"lng\" : -73.95361629999999 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.7251773, \"lng\" : -73.936498 }, \"southwest\" : { \"lat\" : 40.6979329, \"lng\" : -73.96984499999999 } } }, \"types\" : [ \"neighborhood\", \"political\" ] }, { \"address_components\" : [ { \"long_name\" : \"Kings\", \"short_name\" : \"Kings\", \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] } , { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"Kings, New York, USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 40.739446, \"lng\" : -73.83336509999999 }, \"southwest\" : { \"lat\" : 40.5695637, \"lng\" : -74.0429503 } }, \"location\" : { \"lat\" : 40.6528762, \"lng\" : -73.95949399999999 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.739446, \"lng\" : -73.83336509999999 } , \"southwest\" : { \"lat\" : 40.5695637, \"lng\" : -74.0429503 } } }, \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"address_components\" : [ { \"long_name\" : \"Brooklyn\", \"short_name\" : \"Brooklyn\", \"types\" : [ \"sublocality\", \"political\" ] }, { \"long_name\" : \"New York City\", \"short_name\" : \"New York City\", \"types\" : [ \"locality\", \"political\" ] }, { \"long_name\" : \"Kings\", \"short_name\" : \"Kings\", \"types\" : [ \"administrative_area_level_2\", \"political\" ] }, { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"Brooklyn, New York, USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 40.739446, \"lng\" : -73.83336509999999 }, \"southwest\" : { \"lat\" : 40.551042, \"lng\" : -74.05663 } }, \"location\" : { \"lat\" : 40.65, \"lng\" : -73.95 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.739446, \"lng\" : -73.83336509999999 }, \"southwest\" : { \"lat\" : 40.551042, \"lng\" : -74.05663 } } }, \"types\" : [ \"sublocality\", \"political\" ] }, { \"address_components\" : [ { \"long_name\" : \"New York City\", \"short_name\" : \"New York City\", \"types\" : [ \"locality\", \"political\" ] }, { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"New York City, New York, USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 40.91525559999999, \"lng\" : -73.70027209999999 }, \"southwest\" : { \"lat\" : 40.4959961, \"lng\" : -74.2590879 } }, \"location\" : { \"lat\" : 40.7143528, \"lng\" : -74.00597309999999 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 40.91525559999999, \"lng\" : -73.70027209999999 }, \"southwest\" : { \"lat\" : 40.496006, \"lng\" : -74.25573489999999 } } }, \"types\" : [ \"locality\", \"political\" ] }, { \"address_components\" : [ { \"long_name\" : \"ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド\", \"short_name\" : \"ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド\", \"types\" : [ \"political\" ] }, { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"USA, ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 41.6018065, \"lng\" : -71.85621399 999999 }, \"southwest\" : { \"lat\" : 39.49853299999999, \"lng\" : -75.3585939 } }, \"location\" : { \"lat\" : 40.9590293, \"lng\" : -74.0300122 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 41.6018065, \"lng\" : -71.85621399999999 }, \"southwest\" : { \"lat\" : 39.49853299999999, \"lng\" : -75.3585939 } } }, \"types\" : [ \"political\" ] }, { \"address_components\" : [ { \"long_name\" : \"New York\", \"short_name\" : \"NY\", \"types\" : [ \"a dministrative_area_level_1\", \"political\" ] }, { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"New York, USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 45.015865, \"lng\" : -71.85626429999999 }, \"southwest\" : { \"lat\" : 40.4959961, \"lng\" : -79.76214379999999 } }, \"location\" : { \"lat\" : 43.2994285, \"lng\" : -74.21793260000001 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 45.0156269, \"lng\" : -71.85626429999999 }, \"southwest\" : { \"lat\" : 40.4960191, \"lng\" : -79.76214379999999 } } }, \"types\" : [ \"administrative_area_level_1\", \"political\" ] }, { \"address_components\" : [ { \"long_name\" : \"USA\", \"short_name\" : \"US\", \"types\" : [ \"country\", \"political\" ] } ], \"formatted_address\" : \"USA\", \"geometry\" : { \"bounds\" : { \"northeast\" : { \"lat\" : 90, \"lng\" : 180 }, \"southwest\" : { \"lat\" : -90, \"lng\" : -180 } }, \"location\" : { \"lat\" : 37.09024, \"lng\" : -95.712891 }, \"location_type\" : \"APPROXIMATE\", \"viewport\" : { \"northeast\" : { \"lat\" : 49.38, \"lng\" : -66.94 }, \"southwest\" : { \"lat\" : 25.82, \"lng\" : -124.39 } } }, \"types\" : [ \"country\", \"political\" ] } ], \"status\" : \"OK\" }

I want to retrieve Geo address from given latitude and longitude using geocoding, using the given example on the site maps.googleapis/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true I just need city name New York in this example and country name i.e United States. But the problem is there are more than one appearances of these words as long_name. My question how can I parse this json/xml in php to get just city name and country.

$url= file_get_contents('maps.googleapis/maps/api/geocode/xml?latlng=40.714‌​224,-73.961452&sensor=true'); $xml = simplexml_load_string($url); $tracks = $xml->result; $temp; $city; $country; foreach($tracks as $key) { foreach($key->address_component as $val) { $temp= $val->long_name; if($val->type=="locality") { $city=$temp; }else if($val->type=="country") { $country=$temp; } } } echo $country."<br />"; echo $city; <?xml version="1.0" encoding="UTF-8"?> <GeocodeResponse> <status>OK</status> <result> <type>street_address</type> <formatted_address>277 Bedford Avenue, Brooklyn, New York 11211, USA</formatted_address> <address_component> <long_name>277</long_name> <short_name>277</short_name> <type>street_number</type> </address_component> <address_component> <long_name>Bedford Avenue</long_name> <short_name>Bedford Ave</short_name> <type>route</type> </address_component> <address_component> <long_name>Williamsburg</long_name> <short_name>Williamsburg</short_name> <type>neighborhood</type> <type>political</type> </address_component> <address_component> <long_name>Brooklyn</long_name> <short_name>Brooklyn</short_name> <type>sublocality</type> <type>political</type> </address_component> <address_component> <long_name>Kings</long_name> <short_name>Kings</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>11211</long_name> <short_name>11211</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>40.7142320</lat> <lng>-73.9612889</lng> </location> <location_type>ROOFTOP</location_type> <viewport> <southwest> <lat>40.7128830</lat> <lng>-73.9626379</lng> </southwest> <northeast> <lat>40.7155810</lat> <lng>-73.9599399</lng> </northeast> </viewport> </geometry> </result> <result> <type>bus_station</type> <type>transit_station</type> <type>establishment</type> <formatted_address>Grand St/Bedford Av, Brooklyn, New York 11211, USA</formatted_address> <address_component> <long_name>Grand St/Bedford Av</long_name> <short_name>Grand St/Bedford Av</short_name> <type>bus_station</type> <type>transit_station</type> <type>establishment</type> </address_component> <address_component> <long_name>Williamsburg</long_name> <short_name>Williamsburg</short_name> <type>neighborhood</type> <type>political</type> </address_component> <address_component> <long_name>Brooklyn</long_name> <short_name>Brooklyn</short_name> <type>sublocality</type> <type>political</type> </address_component> <address_component> <long_name>Kings</long_name> <short_name>Kings</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>11211</long_name> <short_name>11211</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>40.7143210</lat> <lng>-73.9611510</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.7129720</lat> <lng>-73.9625000</lng> </southwest> <northeast> <lat>40.7156700</lat> <lng>-73.9598020</lng> </northeast> </viewport> </geometry> </result> <result> <type>bus_station</type> <type>transit_station</type> <type>establishment</type> <formatted_address>Grand St/Bedford Av, Brooklyn, New York 11249, USA</formatted_address> <address_component> <long_name>Grand St/Bedford Av</long_name> <short_name>Grand St/Bedford Av</short_name> <type>bus_station</type> <type>transit_station</type> <type>establishment</type> </address_component> <address_component> <long_name>Williamsburg</long_name> <short_name>Williamsburg</short_name> <type>neighborhood</type> <type>political</type> </address_component> <address_component> <long_name>Brooklyn</long_name> <short_name>Brooklyn</short_name> <type>sublocality</type> <type>political</type> </address_component> <address_component> <long_name>Kings</long_name> <short_name>Kings</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>11249</long_name> <short_name>11249</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>40.7146840</lat> <lng>-73.9615630</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.7133350</lat> <lng>-73.9629120</lng> </southwest> <northeast> <lat>40.7160330</lat> <lng>-73.9602140</lng> </northeast> </viewport> </geometry> </result> <result> <type>bus_station</type> <type>transit_station</type> <type>establishment</type> <formatted_address>Bedford Av/Grand St, Brooklyn, New York 11211, USA</formatted_address> <address_component> <long_name>Bedford Av/Grand St</long_name> <short_name>Bedford Av/Grand St</short_name> <type>bus_station</type> <type>transit_station</type> <type>establishment</type> </address_component> <address_component> <long_name>Williamsburg</long_name> <short_name>Williamsburg</short_name> <type>neighborhood</type> <type>political</type> </address_component> <address_component> <long_name>Brooklyn</long_name> <short_name>Brooklyn</short_name> <type>sublocality</type> <type>political</type> </address_component> <address_component> <long_name>Kings</long_name> <short_name>Kings</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <address_component> <long_name>11211</long_name> <short_name>11211</short_name> <type>postal_code</type> </address_component> <geometry> <location> <lat>40.7147100</lat> <lng>-73.9609990</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.7133610</lat> <lng>-73.9623480</lng> </southwest> <northeast> <lat>40.7160590</lat> <lng>-73.9596500</lng> </northeast> </viewport> </geometry> </result> <result> <type>postal_code</type> <formatted_address>Brooklyn, New York 11211, USA</formatted_address> <address_component> <long_name>11211</long_name> <short_name>11211</short_name> <type>postal_code</type> </address_component> <address_component> <long_name>Brooklyn</long_name> <short_name>Brooklyn</short_name> <type>sublocality</type> <type>political</type> </address_component> <address_component> <long_name>New York City</long_name> <short_name>New York City</short_name> <type>locality</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>40.7093358</lat> <lng>-73.9565551</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.7008331</lat> <lng>-73.9644697</lng> </southwest> <northeast> <lat>40.7280089</lat> <lng>-73.9207299</lng> </northeast> </viewport> <bounds> <southwest> <lat>40.7008331</lat> <lng>-73.9644697</lng> </southwest> <northeast> <lat>40.7280089</lat> <lng>-73.9207299</lng> </northeast> </bounds> </geometry> </result> <result> <type>neighborhood</type> <type>political</type> <formatted_address>Williamsburg, Brooklyn, New York, USA</formatted_address> <address_component> <long_name>Williamsburg</long_name> <short_name>Williamsburg</short_name> <type>neighborhood</type> <type>political</type> </address_component> <address_component> <long_name>Brooklyn</long_name> <short_name>Brooklyn</short_name> <type>sublocality</type> <type>political</type> </address_component> <address_component> <long_name>New York City</long_name> <short_name>New York City</short_name> <type>locality</type> <type>political</type> </address_component> <address_component> <long_name>Kings</long_name> <short_name>Kings</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>40.7064461</lat> <lng>-73.9536163</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.6979329</lat> <lng>-73.9698450</lng> </southwest> <northeast> <lat>40.7251773</lat> <lng>-73.9364980</lng> </northeast> </viewport> <bounds> <southwest> <lat>40.6979329</lat> <lng>-73.9698450</lng> </southwest> <northeast> <lat>40.7251773</lat> <lng>-73.9364980</lng> </northeast> </bounds> </geometry> </result> <result> <type>administrative_area_level_2</type> <type>political</type> <formatted_address>Kings, New York, USA</formatted_address> <address_component> <long_name>Kings</long_name> <short_name>Kings</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>40.6528762</lat> <lng>-73.9594940</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.5695637</lat> <lng>-74.0429503</lng> </southwest> <northeast> <lat>40.7394460</lat> <lng>-73.8333651</lng> </northeast> </viewport> <bounds> <southwest> <lat>40.5695637</lat> <lng>-74.0429503</lng> </southwest> <northeast> <lat>40.7394460</lat> <lng>-73.8333651</lng> </northeast> </bounds> </geometry> </result> <result> <type>sublocality</type> <type>political</type> <formatted_address>Brooklyn, New York, USA</formatted_address> <address_component> <long_name>Brooklyn</long_name> <short_name>Brooklyn</short_name> <type>sublocality</type> <type>political</type> </address_component> <address_component> <long_name>New York City</long_name> <short_name>New York City</short_name> <type>locality</type> <type>political</type> </address_component> <address_component> <long_name>Kings</long_name> <short_name>Kings</short_name> <type>administrative_area_level_2</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>40.6500000</lat> <lng>-73.9500000</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.5510420</lat> <lng>-74.0566300</lng> </southwest> <northeast> <lat>40.7394460</lat> <lng>-73.8333651</lng> </northeast> </viewport> <bounds> <southwest> <lat>40.5510420</lat> <lng>-74.0566300</lng> </southwest> <northeast> <lat>40.7394460</lat> <lng>-73.8333651</lng> </northeast> </bounds> </geometry> </result> <result> <type>locality</type> <type>political</type> <formatted_address>New York City, New York, USA</formatted_address> <address_component> <long_name>New York City</long_name> <short_name>New York City</short_name> <type>locality</type> <type>political</type> </address_component> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>40.7143528</lat> <lng>-74.0059731</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.4960060</lat> <lng>-74.2557349</lng> </southwest> <northeast> <lat>40.9152556</lat> <lng>-73.7002721</lng> </northeast> </viewport> <bounds> <southwest> <lat>40.4959961</lat> <lng>-74.2590879</lng> </southwest> <northeast> <lat>40.9152556</lat> <lng>-73.7002721</lng> </northeast> </bounds> </geometry> </result> <result> <type>political</type> <formatted_address>USA, ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド</formatted_address> <address_component> <long_name>ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド</long_name> <short_name>ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド</short_name> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>40.9590293</lat> <lng>-74.0300122</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>39.4985330</lat> <lng>-75.3585939</lng> </southwest> <northeast> <lat>41.6018065</lat> <lng>-71.8562140</lng> </northeast> </viewport> <bounds> <southwest> <lat>39.4985330</lat> <lng>-75.3585939</lng> </southwest> <northeast> <lat>41.6018065</lat> <lng>-71.8562140</lng> </northeast> </bounds> </geometry> </result> <result> <type>administrative_area_level_1</type> <type>political</type> <formatted_address>New York, USA</formatted_address> <address_component> <long_name>New York</long_name> <short_name>NY</short_name> <type>administrative_area_level_1</type> <type>political</type> </address_component> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>43.2994285</lat> <lng>-74.2179326</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>40.4960191</lat> <lng>-79.7621438</lng> </southwest> <northeast> <lat>45.0156269</lat> <lng>-71.8562643</lng> </northeast> </viewport> <bounds> <southwest> <lat>40.4959961</lat> <lng>-79.7621438</lng> </southwest> <northeast> <lat>45.0158650</lat> <lng>-71.8562643</lng> </northeast> </bounds> </geometry> </result> <result> <type>country</type> <type>political</type> <formatted_address>USA</formatted_address> <address_component> <long_name>USA</long_name> <short_name>US</short_name> <type>country</type> <type>political</type> </address_component> <geometry> <location> <lat>37.0902400</lat> <lng>-95.7128910</lng> </location> <location_type>APPROXIMATE</location_type> <viewport> <southwest> <lat>25.8200000</lat> <lng>-124.3900000</lng> </southwest> <northeast> <lat>49.3800000</lat> <lng>-66.9400000</lng> </northeast> </viewport> <bounds> <southwest> <lat>-90.0000000</lat> <lng>-180.0000000</lng> </southwest> <northeast> <lat>90.0000000</lat> <lng>180.0000000</lng> </northeast> </bounds> </geometry> </result> </GeocodeResponse>

解决方案

You can check for 3 variables in the google response (json) to find the current cityname. Hence there is 3 different interpretation of the name:

Country | AdministrativeArea | |_AdministrativeAreaName (cityname) | |_Locality_LocalityName (cityname) | |_SubAdministrativeArea | |_Locality_LocalityName (cityname) if ($this->json->Placemark[0]->AddressDetails->Country->AdministrativeArea->Locality->LocalityName != NULL) { $code['cityname'] = $this->json->Placemark[0]->AddressDetails->Country->AdministrativeArea->Locality->LocalityName; } elseif ($this->json->Placemark[0]->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName != NULL) { $code['cityname'] = $this->json->Placemark[0]->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName; }

If you have more then a Placemark you need to loop through all the array.

"results" : [ { "address_components" : [ { "long_name" : "277", "short_name" : "277", "types" : [ "street_number" ] }, { "long_name" : "Bedford Avenue", "short_name" : "Bedford Ave", "types" : [ "route" ] }, { "long_name" : "Williamsburg", "short_name" : "Williamsburg", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Brooklyn", "short_name" : "Brooklyn", "types" : [ "sublocality", "political" ] }, { "long_name" : "Kings", "short_name" : "Kings", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "11211", "short_name" : "11211", "types" : [ "postal_code" ] } ], "formatted_address" : "277 Bedford Avenue, Brooklyn, New York 11211, USA", "geometry" : { "location" : { "lat" : 40.714232, "lng" : -73.9612889 }, "location_type" : "ROOFTOP", "viewport" : { "northeast" : { "lat" : 40.7155809802915, "lng" : -73.9599399197085 }, "southwest" : { "lat" : 40.7128830197085, "lng" : -73.96263788029151 } } }, "types" : [ "street_address" ] }, { "address_components" : [ { "long_name" : "Grand St/Bedford Av", "short_name" : "Grand St/Bedford Av", "types" : [ "bus_station", "transit_station", "establishment" ] }, { "long_name" : "Williamsburg", "short_name" : "Williamsburg", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Brooklyn", "short_name" : "Brooklyn", "types" : [ "sublocality", "political" ] }, { "long_name" : "Kings", "short_name" : "Kings", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "11211", "short_name" : "11211", "types" : [ "postal_code" ] } ], "formatted_address" : "Grand St/Bedford Av, Brooklyn, New York 11211, USA", "geometry" : { "location" : { "lat" : 40.714321, "lng" : -73.961151 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 40.71566998029149, "lng" : -73.95980201970849 }, "southwest" : { "lat" : 40.7129720197085, "lng" : -73.96249998029151 } } }, "types" : [ "bus_station", "transit_station", "establishment" ] }, { "address_components" : [ { "long_name" : "Grand St/Bedford Av", "short_name" : "Grand St/Bedford Av", "types" : [ "bus_station", "transit_station", "establishment" ] }, { "long_name" : "Williamsburg", "short_name" : "Williamsburg", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Brooklyn", "short_name" : "Brooklyn", "types" : [ "sublocality", "political" ] }, { "long_name" : "Kings", "short_name" : "Kings", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "11249", "short_name" : "11249", "types" : [ "postal_code" ] } ], "formatted_address" : "Grand St/Bedford Av, Brooklyn, New York 11249, USA", "geometry" : { "location" : { "lat" : 40.714684, "lng" : -73.961563 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 40.71603298029149, "lng" : -73.9602140197085 }, "southwest" : { "lat" : 40.7133350197085, "lng" : -73.96291198029151 } } }, "types" : [ "bus_station", "transit_station", "establishment" ] }, { "address_components" : [ { "long_name" : "Bedford Av/Grand St", "short_name" : "Bedford Av/Grand St", "types" : [ "bus_station", "transit_station", "establishment" ] }, { "long_name" : "Williamsburg", "short_name" : "Williamsburg", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Brooklyn", "short_name" : "Brooklyn", "types" : [ "sublocality", "political" ] }, { "long_name" : "Kings", "short_name" : "Kings", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "11211", "short_name" : "11211", "types" : [ "postal_code" ] } ], "formatted_address" : "Bedford Av/Grand St, Brooklyn, New York 11211, USA", "geometry" : { "location" : { "lat" : 40.71471, "lng" : -73.960999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 40.7160589802915, "lng" : -73.95965001970849 }, "southwest" : { "lat" : 40.7133610197085, "lng" : -73.9623479802915 } } }, "types" : [ "bus_station", "transit_station", "establishment" ] }, { "address_components" : [ { "long_name" : "11211", "short_name" : "11211", "types" : [ "postal_code" ] }, { "long_name" : "Brooklyn", "short_name" : "Brooklyn", "types" : [ "sublocality", "political" ] }, { "long_name" : "New York City", "short_name" : "New York City", "types" : [ "locality", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Brooklyn, New York 11211, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 40.7280089, "lng" : -73.9207299 }, "southwest" : { "lat" : 40.7008331, "lng" : -73.9644697 } }, "location" : { "lat" : 40.7093358, "lng" : -73.9565551 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 40.7280089, "lng" : -73.9207299 }, "southwest" : { "lat" : 40.7008331, "lng" : -73.9644697 } } }, "types" : [ "postal_code" ] }, { "address_components" : [ { "long_name" : "Williamsburg", "short_name" : "Williamsburg", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Brooklyn", "short_name" : "Brooklyn", "types" : [ "sublocality", "political" ] }, { "long_name" : "New York City", "short_name" : "New York City", "types" : [ "locality", "political" ] }, { "long_name" : "Kings", "short_name" : "Kings", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Williamsburg, Brooklyn, New York, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 40.7251773, "lng" : -73.936498 }, "southwest" : { "lat" : 40.6979329, "lng" : -73.96984499999999 } }, "location" : { "lat" : 40.7064461, "lng" : -73.95361629999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 40.7251773, "lng" : -73.936498 }, "southwest" : { "lat" : 40.6979329, "lng" : -73.96984499999999 } } }, "types" : [ "neighborhood", "political" ] }, { "address_components" : [ { "long_name" : "Kings", "short_name" : "Kings", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Kings, New York, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 40.739446, "lng" : -73.83336509999999 }, "southwest" : { "lat" : 40.5695637, "lng" : -74.0429503 } }, "location" : { "lat" : 40.6528762, "lng" : -73.95949399999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 40.739446, "lng" : -73.83336509999999 }, "southwest" : { "lat" : 40.5695637, "lng" : -74.0429503 } } }, "types" : [ "administrative_area_level_2", "political" ] }, { "address_components" : [ { "long_name" : "Brooklyn", "short_name" : "Brooklyn", "types" : [ "sublocality", "political" ] }, { "long_name" : "New York City", "short_name" : "New York City", "types" : [ "locality", "political" ] }, { "long_name" : "Kings", "short_name" : "Kings", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "Brooklyn, New York, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 40.739446, "lng" : -73.83336509999999 }, "southwest" : { "lat" : 40.551042, "lng" : -74.05663 } }, "location" : { "lat" : 40.65, "lng" : -73.95 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 40.739446, "lng" : -73.83336509999999 }, "southwest" : { "lat" : 40.551042, "lng" : -74.05663 } } }, "types" : [ "sublocality", "political" ] }, { "address_components" : [ { "long_name" : "New York City", "short_name" : "New York City", "types" : [ "locality", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "New York City, New York, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 40.91525559999999, "lng" : -73.70027209999999 }, "southwest" : { "lat" : 40.4959961, "lng" : -74.2590879 } }, "location" : { "lat" : 40.7143528, "lng" : -74.00597309999999 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 40.91525559999999, "lng" : -73.70027209999999 }, "southwest" : { "lat" : 40.496006, "lng" : -74.25573489999999 } } }, "types" : [ "locality", "political" ] }, { "address_components" : [ { "long_name" : "ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド", "short_name" : "ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド", "types" : [ "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "USA, ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド", "geometry" : { "bounds" : { "northeast" : { "lat" : 41.6018065, "lng" : -71.85621399999999 }, "southwest" : { "lat" : 39.49853299999999, "lng" : -75.3585939 } }, "location" : { "lat" : 40.9590293, "lng" : -74.0300122 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 41.6018065, "lng" : -71.85621399999999 }, "southwest" : { "lat" : 39.49853299999999, "lng" : -75.3585939 } } }, "types" : [ "political" ] }, { "address_components" : [ { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "New York, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 45.015865, "lng" : -71.85626429999999 }, "southwest" : { "lat" : 40.4959961, "lng" : -79.76214379999999 } }, "location" : { "lat" : 43.2994285, "lng" : -74.21793260000001 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 45.0156269, "lng" : -71.85626429999999 }, "southwest" : { "lat" : 40.4960191, "lng" : -79.76214379999999 } } }, "types" : [ "administrative_area_level_1", "political" ] }, { "address_components" : [ { "long_name" : "USA", "short_name" : "US", "types" : [ "country", "political" ] } ], "formatted_address" : "USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 90, "lng" : 180 }, "southwest" : { "lat" : -90, "lng" : -180 } }, "location" : { "lat" : 37.09024, "lng" : -95.712891 }, "location_type" : "APPROXIMATE", "viewport" : { "northeast" : { "lat" : 49.38, "lng" : -66.94 }, "southwest" : { "lat" : 25.82, "lng" : -124.39 } } }, "types" : [ "country", "political" ] } ], "status" : "OK" }

更多推荐

如何解析Google地图地理编码结果

本文发布于:2023-05-29 21:33:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/353308.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:地理   地图   Google

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!