我正在使用 XML 请求在谷歌地图(C#、ASP.NET)中获取行驶距离,我的示例请求:
string strURL = "http://maps.google.com/maps/api/directions/xml?origin=" + Session["UserLat"].ToString() + "," + Session["UserLon"].ToString() + "&destination=" + tmpList[d].X + "," + tmpList[d].Y + "&mode=driving&sensor=false";
List<float> distances = new List<float>();
distances.Clear();
HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(strURL);
//Request.Headers.Add("Man", "GET " + strURL);
HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse();
if (wRequest.HaveResponse)
{
if (wResponse.StatusCode == HttpStatusCode.OK)
{
StreamHandler = new System.IO.StreamReader(wResponse.GetResponseStream());
xmlDoc.LoadXml(StreamHandler.ReadToEnd());
Response.Write(xmlDoc.GetElementsByTagName("distance").Count.ToString() + ",");
for (int w = 0; w < xmlDoc.GetElementsByTagName("distance").Count; w++)
{
distances.Add(float.Parse(xmlDoc.GetElementsByTagName("distance")[w].ChildNodes[0].InnerText));
}
}
}
似乎有些请求返回 0 距离,当然每次我运行我的代码都会得到不同的结果,我在地图上有几个点(大约 19 个点),几乎有一半返回 0 距离,我要排序我的基于距离的项目,但是当我得到 0 距离时,我什么也做不了!我在循环中获取距离,这里出了什么问题?