gpt4 book ai didi

c# - 谷歌地图位置按 map 上最近的第一个排序

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:47 26 4
gpt4 key购买 nike

我正在使用谷歌地图 API。我有 4 个目的地,我想让他们的位置按最近的位置排序,然后是第二个最近的位置。我必须在谷歌地图上显示哪个位置是第一个最近的,然后是下一个最近的,依此类推。到目前为止,我只使用以下代码成功计算了 2 个位置之间的距离:

  private void button1_Click(object sender, EventArgs e)
{
getDistance("DECATUR FARM ROAD CHICHESTER PO20 8JT", "UNIT 01 CLIFF REACH GREENHITHE DA9 9SW");
}

public int getDistance(string origin, string destination)
{
System.Threading.Thread.Sleep(1000);
int distance = 0;
//string from = origin.Text;
//string to = destination.Text;
string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";
string requesturl = url;
//string requesturl = @"http://maps.googleapis.com/maps/api/directions/json?origin=" + from + "&alternatives=false&units=imperial&destination=" + to + "&sensor=false";
string content = fileGetContents(requesturl);
JObject o = JObject.Parse(content);
try
{
distance = (int)o.SelectToken("routes[0].legs[0].distance.value");
return distance;
}
catch
{
return distance;
}
return distance;
//ResultingDistance.Text = distance;
}

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

如何根据谷歌地图上的距离显示目的地。任何提示或指示将不胜感激。

请看图片,这就是我想要完成的 enter image description here

最佳答案

快乐 :) 享受编码..

 ArrayList arraylist;
List<string> adddistance = new List<string>();
List<string> getfinallist = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
string s = "UNIT 01 CLIFF REACH GREENHITHE DA9 9SW,PINETOP BIRKLANDS LANE ST ALBANS AL1 1EE,HOLYWELL HILL ST ALBANS AL1 1BT,OLD RECTORY HOLYWELL HILL ST ALBANS AL1 1BY";
button("DECATUR FARM ROAD CHICHESTER PO20 8JT", s);
}

public void button(string orign , string destination)
{

string[] words = destination.Split(',');
foreach (string word in words)
{
getDistance(orign, word);
}
sorting();


}

public int getDistance(string origin, string destination)
{
System.Threading.Thread.Sleep(1000);
int distance = 0;



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

string content = fileGetContents(requesturl);
JObject o = JObject.Parse(content);


string strdistance = (string)o.SelectToken("routes[0].legs[0].distance.value") + " , " + destination + " , ";
adddistance.Add(strdistance);




return distance;


}


public void sorting()
{
adddistance.Sort();

string getfirststring = adddistance.FirstOrDefault().ToString() ;
var vals = getfirststring.Split(',')[1];

getfinallist.Add(getfirststring.Split(',')[1]);

StringBuilder builder = new StringBuilder();
adddistance.RemoveAt(0);

foreach (string cat in adddistance) // Loop through all strings
{
builder.Append(cat); // Append string to StringBuilder

}


adddistance.Where((wors, index) => index % 2 == 0);
string result = builder.ToString();

string[] words = result.Split(',');
string[] even = words.Where((str, ix) => ix % 2 == 1).ToArray();

adddistance.Clear();

foreach (string word in even)
{ //string get = Regex.Match(word, @"^[" + numSet + @"]+$").ToString();

if (word != " ")
{
getDistance(vals, word);
}


}
sorting();


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

关于c# - 谷歌地图位置按 map 上最近的第一个排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35380790/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com