如何从谷歌地图api响应中读取城市和邮政编码 [英] how to read city and zip code from google map api response
本文介绍了如何从谷歌地图api响应中读取城市和邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<pre lang="xml">
<GeocodeResponse>
<status>OK</status>
<result>
<type>subway_station</type>
<type>train_station</type>
<type>transit_station</type>
<type>establishment</type>
<formatted_address>
Golf Course, Captain Shashi Kant Sharma Marg, Noida Golf Course, Sector 37, Noida, Uttar Pradesh 201303, India
<formatted_address>
<address_component>
<long_name>Golf Course</long_name>
<short_name>Golf Course</short_name>
<type>subway_station</type>
<type>train_station</type>
<type>transit_station</type>
<type>establishment</type>
</address_component>
<address_component>
<long_name>Captain Shashi Kant Sharma Marg</long_name>
<short_name>Captain Shashi Kant Sharma Marg</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Noida Golf Course</long_name>
<short_name>Noida Golf Course</short_name>
<type>sublocality_level_2</type>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Sector 37</long_name>
<short_name>Sector 37</short_name>
<type>sublocality_level_1</type>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Noida</long_name>
<short_name>Noida</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Gautam Buddh Nagar</long_name>
<short_name>Gautam Buddh Nagar</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Uttar Pradesh</long_name>
<short_name>UP</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>India</long_name>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>201303</long_name>
<short_name>201303</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>28.5671746</lat>
<lng>77.3459920</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>28.5658059</lat>
<lng>77.3445952</lng>
</southwest>
<northeast>
<lat>28.5685039</lat>
<lng>77.3472931</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>28.5665483</lat>
<lng>77.3451542</lng>
</southwest>
<northeast>
<lat>28.5677615</lat>
<lng>77.3467341</lng>
</northeast>
</bounds>
</geometry>
<partial_match>true</partial_match>
<place_id>ChIJn5CCdrjlDDkR1c3SwjoQ__A</place_id>
</result>
</GeocodeResponse>
</pre>
This is the response I am getting from api but problem is the index of city and zip code is always changing
my code is
<pre lang="c#">
string url = "http://maps.google.com/maps/api/geocode/xml?address=" + completeAdd + "&sensor=false";
WebRequest request = WebRequest.Create(url);
using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
DataSet dsResult = new DataSet();
dsResult.ReadXml(reader);
DataTable dtCoordinates = new DataTable();
dtCoordinates.Columns.AddRange(new DataColumn[5] {
new DataColumn("Id", typeof(int)),
new DataColumn("Address", typeof(string)),
new DataColumn("Latitude",typeof(string)),
new DataColumn("Longitude",typeof(string)),
new DataColumn("City",typeof(string))
});
if (dsResult.Tables["result"] != null)
foreach (DataRow row in dsResult.Tables["result"].Rows)
{
var city = "";
if (dsResult.Tables["address_component"].Select()[4].ItemArray[0].ToString() == "political")
{
city = dsResult.Tables["address_component"].Select("result_id = " + row["result_id"].ToString())[4].ItemArray[0].ToString();
}
string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString();
DataRow location = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0];
dtCoordinates.Rows.Add(row["result_id"], row["formatted_address"], location["lat"], location["lng"], city);
}
return dtCoordinates;
}
}
</pre>
推荐答案
使用数据集
读取XML不是很灵活。尝试使用 System.Xml.Linq [< a href =https://msdn.microsoft.com/en-us/library/system.xml.linq(v=vs.110).aspx\"target =_ blanktitle =New Window> ^ ]来解析XML。
Using aDataSet
to read XML is not very flexible. Try using System.Xml.Linq[^] to parse the XML instead.
DataTable dtCoordinates = new DataTable();
dtCoordinates.Columns.AddRange(new[] {
new DataColumn("Id", typeof(string)),
new DataColumn("Address", typeof(string)),
new DataColumn("Latitude", typeof(double)),
new DataColumn("Longitude", typeof(double)),
new DataColumn("City", typeof(string)),
new DataColumn("ZipCode", typeof(string))
});
// Make sure the address parameter is properly encoded:
string url = "http://maps.google.com/maps/api/geocode/xml?address=" + Uri.EscapeDataString(completeAdd) + "&sensor=false";
// Load the document, and find the "result" node:
XDocument document = XDocument.Load(url);
XElement result = document.Root == null ? null : document.Root.Element("result");
if (result != null)
{
// TODO: There doesn't seem to be a "result_id" node anywhere in the XML.
// Assuming you meant the "place_id", which is a string.
string placeId = (string)result.Element("place_id");
string formattedAddress = (string)result.Element("formatted_address");
// TODO: I've assumed that "Noida" is the city in your example.
string city = (string)result
.Elements("address_component")
.Where(ac => ac.Elements("type").Any(t => t.Value == "locality"))
.Elements("long_name")
.FirstOrDefault()
;
string zipCode = (string)result
.Elements("address_component")
.Where(ac => ac.Elements("type").Any(t => t.Value == "postal_code"))
.Elements("long_name")
.FirstOrDefault()
;
var location = result.Elements("geometry").Elements("location").FirstOrDefault();
double? latitude = location == null ? default(double?) : (double?)location.Element("lat");
double? longitude = location == null ? default(double?) : (double?)location.Element("lng");
dtCoordinates.Rows.Add(placeId, formattedAddress, latitude, longitude, city, zipCode);
}
return dtCoordinates;
这篇关于如何从谷歌地图api响应中读取城市和邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文