2016-03-07 74 views
0

目前我得到的2位之间的距离在我的C#WinForms应用程序通过执行此操作:如何在google maps api中使用provideRouteAlternatives = true?

string requesturl = "http://maps.google.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false; 
string content = fileGetContents(requesturl); 

JObject o = JObject.Parse(content); 
distance = (decimal)o.SelectToken("routes[0].legs[0].distance.value")/1000; 

private static string fileGetContents(string fileName) 
{ 
    string sContents = string.Empty; 
    string me = string.Empty; 
    try 
    { 
     if (fileName.ToLower().IndexOf("http:") > -1) 
     { 
      System.Net.WebClient wc = new System.Net.WebClient(); 
      byte[] response = wc.DownloadData(fileName); 
      sContents = System.Text.Encoding.ASCII.GetString(response); 
     } 
     else 
     { 
      System.IO.StreamReader sr = new System.IO.StreamReader(fileName); 
      sContents = sr.ReadToEnd(); 
      sr.Close(); 
     } 
    } 
    catch { sContents = "unable to connect to server "; } 
    return sContents; 
} 

这工作得很好,但它总是返回最快的路线,而不是最短的。

我想获得最短距离,所以我想添加“provideRouteAlternatives = true”来调用google api。然后我可以搜索每个路径中的文件以获得最短的文件。
但我该怎么做?
要清楚,如果有多条路线可以返回,我知道如何调整我的代码以在每条路线中搜索,问题是如何让此参数开始工作,以便返回多条路线。

我尝试这样做:

string requesturl = "http://maps.google.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false&provideRouteAlternatives=true"; 

,但它似乎并不做任何事情。 当我问谷歌方向与相同的地址我得到多个路线,但这个API不。

我做错了什么,或者它只是不可能这样吗? 也许我需要JavaScript或其他东西,在winforms中可能吗? 一个例子会非常好。

回答

3

发现它,我使用了错误的参数。
我必须使用alternatives = true来代替provideroutealternatives = true

+0

我刚刚在过去的两个小时里解决了这个问题。感谢您的解决方案! (顺便说一下,Google文档仍然列出“provideRouteAlternatives”作为参数名称。) – mvanlamz

+0

很高兴我能提供帮助。也许在谷歌他们应该谷歌更多关于他们的文档问题:) – GuidoG