如何解析 Google 地图地理编码结果
我想使用地理编码从给定的纬度和经度检索地理地址, 使用网站上给定的示例
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true
在这个例子中我只需要城市名称纽约和国家名称即美国。但问题是出现的次数不止一种 这些词作为long_name。
我的问题是如何在 php 中解析这个 json/xml 以获取城市名称和国家/地区。
$url= file_get_contents('http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-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>
I want to retrieve Geo address from given latitude and longitude using geocoding,
using the given example on the site
http://maps.googleapis.com/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('http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以检查 google 响应 (json) 中的 3 个变量来查找当前城市名称。因此,该名称有 3 种不同的解释:
如果您有多个地标,则需要循环遍历所有数组。
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:
If you have more then a Placemark you need to loop through all the array.
我有一个适用于 Google 地理编码 JSON 结果的简单解决方案。这是一个简单的 for 循环,返回字符串数组中的短版本和长版本。只需调用该函数并在参数中提供“类型”即可。
string[] street_number = GetJSONValueBasedOnType(record.geocode, "street_number");
I have a simple solution that works for the Google Geocode JSON results. It is a simple for loop that returns that short and long version in a string array. Just call the function and provide the "types" in the argument.
string[] street_number = GetJSONValueBasedOnType(record.geocode, "street_number");