Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Google API使用公共交通的两点之间的距离_C#_Google Maps - Fatal编程技术网

C# 使用Google API使用公共交通的两点之间的距离

C# 使用Google API使用公共交通的两点之间的距离,c#,google-maps,C#,Google Maps,使用GoogleAPI,如何使用公共交通工具获取两点之间的距离 我可以利用公共交通工具在两点之间获得时间,但距离不够。使用汽车作为选项,我可以得到距离和时间 代码: static void Main(string[] args) { double dist = GetDrivingDistanceInMiles("Cubbon Park, Kasturba Road, Bangalore, Karnataka 560001", "Majestic, Bangalo

使用GoogleAPI,如何使用公共交通工具获取两点之间的距离

我可以利用公共交通工具在两点之间获得时间,但距离不够。使用汽车作为选项,我可以得到距离和时间

代码:

    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;
    }

此代码通过驾驶为我提供两点之间的距离,但在使用公共交通工具时是否仍然可以获得距离。

并非离题。这是google maps api v3方法返回的属性的有效问题。这不是离题。我想知道我是否能找到一种获取距离属性的方法。也许如果你尝试重新编写问题并用当前代码完成它,它会被重新打开:谢谢阿梅纳迪尔。那是.NET吗?我不认识它。