【发布时间】:2016-03-07 14:48:17
【问题描述】:
目前,我通过这样做获得了我的 c# winforms 应用程序中 2 个位置之间的距离:
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;
}
这很好用,但它总是返回最快的路线,而不是最短的路线。
我想获得最短的距离,所以我想在对 google api 的调用中添加provideRouteAlternatives=true。然后我可以在每条路线的文件中搜索以获得最短的路径。
但是我该怎么做呢?
需要说明的是,如果要返回多条路线,我知道如何调整我的代码以在每条路线中进行搜索,问题是如何让此参数开始工作,以便返回多条路线。
我试过了:
string requesturl = "http://maps.google.com/maps/api/directions/json?origin="
+ origin + "&destination="
+ destination + "&sensor=false&provideRouteAlternatives=true";
但它似乎没有做任何事情。 当我向谷歌询问具有相同地址的路线时,我确实得到了不止一条路线,但这个 api 没有。
我做错了什么还是不可能像这样? 也许我需要javascript或其他东西,在winforms中可能吗? 举个例子就好了。
【问题讨论】:
标签: c# winforms google-maps-api-3