我如何从 google 地理编码 api 的 xml 响应中检索邮政编码?
How would i retrieve the postal code from an xml response from google geocoding api?
我目前正在处理 MVC 项目,需要根据用户地址获取邮政编码。我已经能够成功获取纬度和经度,但无法弄清楚如何从 xml 响应中提取邮政编码。
这是 XML 即时通讯,他们在 api 文档页面上使用的示例 XML。
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address>
<address_component>
<long_name>1600</long_name>
<short_name>1600</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Amphitheatre Pkwy</long_name>
<short_name>Amphitheatre Pkwy</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Mountain View</long_name>
<short_name>Mountain View</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>San Jose</long_name>
<short_name>San Jose</short_name>
<type>administrative_area_level_3</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Santa Clara</long_name>
<short_name>Santa Clara</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>California</long_name>
<short_name>CA</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>94043</long_name>
<short_name>94043</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>37.4217550</lat>
<lng>-122.0846330</lng>
</location>
<location_type>ROOFTOP</location_type>
<viewport>
<southwest>
<lat>37.4188514</lat>
<lng>-122.0874526</lng>
</southwest>
<northeast>
<lat>37.4251466</lat>
<lng>-122.0811574</lng>
</northeast>
</viewport>
</geometry>
<place_id>ChIJ2eUgeAK6j4ARbn5u_wAGqWA</place_id>
>plus_code<
>global_code<849VCWC8+W5>/global_code<
>compound_code<CWC8+W5 Mountain View, California, United States>/compound_code<
>/plus_code<
</result>
</GeocodeResponse>
这是我在控制器中用来获取信息的代码:
string address = "Users Address(HIDDEN)";
string requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false&key=HIDDEN", address);
WebRequest request = WebRequest.Create(requestUri);
WebResponse response = request.GetResponse();
XmlDocument document = new XmlDocument();
document.Load(requestUri);
string status = document.SelectSingleNode("/GeocodeResponse/status").InnerText;
string lat = "", lng = "", postal = "";
if (status == "OK")
{
lat = document.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
lng = document.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
postal = document.SelectSingleNode("/GeocodeResponse/result/address_component/long_name").InnerText;
}
return ($"Latitude: {lat} / Longitude: {lng} / {postal}");
如有任何帮助,我们将不胜感激。
我一直在做更多的挖掘并找到了答案。基本上必须遍历 address_component 节点,直到我到达 address_component 的 postal_code 子节点并获得具有邮政的 long_name 子节点。这是我的解决方案:
XmlNodeList nlist = document.SelectNodes("/GeocodeResponse/result/address_component");
foreach(XmlNode node in nlist)
{
var longName = node.ChildNodes.Item(0).InnerText;
var shortName = node.ChildNodes.Item(1).InnerText;
var zipType = node.ChildNodes.Item(2).InnerText;
if(zipType == "postal_code")
{
postal = longName;
}
}
我目前正在处理 MVC 项目,需要根据用户地址获取邮政编码。我已经能够成功获取纬度和经度,但无法弄清楚如何从 xml 响应中提取邮政编码。
这是 XML 即时通讯,他们在 api 文档页面上使用的示例 XML。
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA</formatted_address>
<address_component>
<long_name>1600</long_name>
<short_name>1600</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Amphitheatre Pkwy</long_name>
<short_name>Amphitheatre Pkwy</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Mountain View</long_name>
<short_name>Mountain View</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>San Jose</long_name>
<short_name>San Jose</short_name>
<type>administrative_area_level_3</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Santa Clara</long_name>
<short_name>Santa Clara</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>California</long_name>
<short_name>CA</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>United States</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>94043</long_name>
<short_name>94043</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>37.4217550</lat>
<lng>-122.0846330</lng>
</location>
<location_type>ROOFTOP</location_type>
<viewport>
<southwest>
<lat>37.4188514</lat>
<lng>-122.0874526</lng>
</southwest>
<northeast>
<lat>37.4251466</lat>
<lng>-122.0811574</lng>
</northeast>
</viewport>
</geometry>
<place_id>ChIJ2eUgeAK6j4ARbn5u_wAGqWA</place_id>
>plus_code<
>global_code<849VCWC8+W5>/global_code<
>compound_code<CWC8+W5 Mountain View, California, United States>/compound_code<
>/plus_code<
</result>
</GeocodeResponse>
这是我在控制器中用来获取信息的代码:
string address = "Users Address(HIDDEN)";
string requestUri = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=false&key=HIDDEN", address);
WebRequest request = WebRequest.Create(requestUri);
WebResponse response = request.GetResponse();
XmlDocument document = new XmlDocument();
document.Load(requestUri);
string status = document.SelectSingleNode("/GeocodeResponse/status").InnerText;
string lat = "", lng = "", postal = "";
if (status == "OK")
{
lat = document.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat").InnerText;
lng = document.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng").InnerText;
postal = document.SelectSingleNode("/GeocodeResponse/result/address_component/long_name").InnerText;
}
return ($"Latitude: {lat} / Longitude: {lng} / {postal}");
如有任何帮助,我们将不胜感激。
我一直在做更多的挖掘并找到了答案。基本上必须遍历 address_component 节点,直到我到达 address_component 的 postal_code 子节点并获得具有邮政的 long_name 子节点。这是我的解决方案:
XmlNodeList nlist = document.SelectNodes("/GeocodeResponse/result/address_component");
foreach(XmlNode node in nlist)
{
var longName = node.ChildNodes.Item(0).InnerText;
var shortName = node.ChildNodes.Item(1).InnerText;
var zipType = node.ChildNodes.Item(2).InnerText;
if(zipType == "postal_code")
{
postal = longName;
}
}