使用谷歌地图方向API获得驾驶距离

时间:2012-10-31 16:21:09

标签: asp.net google-maps

我已经使用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,出了什么问题?

0 个答案:

没有答案