我有一个谷歌地图链接,它返回一个 xml 给我。我想要该 xml 中特定标记的值。有人可以建议我该怎么做吗?链接:
http://maps.google.com/maps/api/geocode/xml?address=1270 Broadway Ste 803, New York, NY 10001, USA&sensor=false
最佳答案
这是一个返回字典中坐标的函数。注意:这是新的 V3 api。无需 API key 。传感器是。
// Figure out the geocoordinates for the location
private Dictionary<string, string> GeoCode(string location)
{
// Initialize the dictionary with empty values.
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("latitude", "");
dic.Add("longitude", "");
try
{
string url = "http://maps.google.com/maps/api/geocode/xml?address=" + System.Web.HttpUtility.UrlEncode(location) + "&sensor=false";
XmlDocument doc = new XmlDocument();
doc.Load(url);
XmlNodeList nodes = doc.SelectNodes("//location");
// We're assuming there's only one location node in the xml.
foreach (XmlNode node in nodes)
{
dic["latitude"] = node.SelectSingleNode("lat").InnerText;
dic["longitude"] = node.SelectSingleNode("lng").InnerText;
}
}
catch (Exception)
{
// If anything goes wrong, we want to get empty values back.
dic["latitude"] = "";
dic["longitude"] = "";
}
return dic;
}
关于从 google api 解析 xml 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2558810/