使用Google API,如何使用公共交通获取两点之间的距离?
我能够在两点(使用公共交通工具)之间获得时间,但不能获得距离。使用汽车作为选项,我可以获得距离和时间。
代码:
static void Main(string[] args)
{
double dist = GetDrivingDistanceInMiles("Cubbon Park, Kasturba Road, Bangalore, Karnataka 560001", "Majestic, Bangalore, Karnataka");
Console.WriteLine("Distance is :: ");
Console.WriteLine(dist);
Console.ReadKey();
}
public static double GetDrivingDistanceInMiles(string origin, string destination)
{
string url = @"http://maps.googleapis.com/maps/api/distancematrix/xml?origins=" +
origin + "&destinations=" + destination +
"&mode=driving&language=en-EN&units=metric ";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
WebResponse response = request.GetResponse();
Stream dataStream = response.GetResponseStream();
StreamReader sreader = new StreamReader(dataStream);
string responsereader = sreader.ReadToEnd();
response.Close();
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(responsereader);
if (xmldoc.GetElementsByTagName("status")[0].ChildNodes[0].InnerText == "OK")
{
XmlNodeList distance = xmldoc.GetElementsByTagName("distance");
return Convert.ToDouble(distance[0].ChildNodes[1].InnerText.Replace(" km", ""));
}
return 0;
}
此代码通过驾驶给出了两点之间的距离,但无论如何都要在使用公共交通工具时保持距离。