我已经使用 XML 和 JSON 通过谷歌地图方向 API 来获取行驶距离,(我使用 ASP.NET,C#),但是这两种方法在给出正确值方面都有问题,它们给出了一些 0 值,有时他们不给出任何价值。获得两点之间行驶距离的最佳方法是什么?你能给我一个工作样本吗
这是我的 XML 方法:
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,怎么了?