C# 谷歌地图位置按地图上最近的第一位排序

C# 谷歌地图位置按地图上最近的第一位排序,c#,winforms,google-maps,google-maps-api-3,C#,Winforms,Google Maps,Google Maps Api 3,我正在使用谷歌地图API。我有4个目的地,我想让他们的位置按最近的位置排序,第一个是最近的位置,第二个是最近的位置。我必须在谷歌地图上显示哪个位置是第一个最近的,然后是第二个最近的,以此类推。到目前为止,我仅使用以下代码成功计算了两个位置之间的距离: private void button1_Click(object sender, EventArgs e) { getDistance("DECATUR FARM ROAD CHICHESTER PO20 8JT",

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

  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;
    }
如何在谷歌地图上根据目的地的距离显示目的地。任何提示或指点都将不胜感激

请看图片,这就是我想要完成的 快乐:)享受编码吧

 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;
    }
ArrayList ArrayList;
List adddistance=新列表();
List getfinallist=新列表();
私有无效按钮1\u单击(对象发送者,事件参数e)
{
字符串s=“01单元悬崖到达格林希特DA9 9SW,松顶伯克兰兹巷圣阿尔本斯阿尔贝,霍利韦尔山圣阿尔本斯阿尔贝,霍利韦尔山老教区圣阿尔本斯阿尔贝”;
按钮(“迪凯特农场路奇切斯特PO20 8JT”,s);
}
公共无效按钮(字符串起始,字符串目标)
{
string[]words=destination.Split(',');
foreach(单词中的字符串)
{
getDistance(源,字);
}
排序();
}
public int getDistance(字符串原点、字符串目标)
{
系统线程线程睡眠(1000);
整数距离=0;
字符串url=”http://maps.googleapis.com/maps/api/directions/json?origin=“+origin+”&destination=“+destination+”&sensor=false”;
字符串requesturl=url;
string content=fileGetContents(requesturl);
JObject o=JObject.Parse(内容);
string strdistance=(string)o.SelectToken(“路由[0]。支腿[0]。距离.value”)+,“+destination+”,“;
adddistance.Add(标准距离);
返回距离;
}
公共空白排序()
{  
adddistance.Sort();
string getfirststring=adddistance.FirstOrDefault().ToString();
var vals=getfirststring.Split(',')[1];
getfinallist.Add(getfirststring.Split(',')[1]);
StringBuilder=新的StringBuilder();
adddistance.RemoveAt(0);
foreach(字符串cat in adddistance)//遍历所有字符串
{
Append(cat);//将字符串追加到StringBuilder
}
adddistance.Where((工作,索引)=>索引%2==0);
字符串结果=builder.ToString();
string[]words=result.Split(',');
string[]偶数=words.Where((str,ix)=>ix%2==1.ToArray();
adddistance.Clear();
foreach(偶数形式的字符串字)
{//string get=Regex.Match(word,@“^[“+numSet+@”]+$”).ToString();
如果(单词!=“”)
{
getDistance(VAL、word);
}               
}
排序();
}
受保护的字符串fileGetContents(字符串文件名)
{
string scocontents=string.Empty;
string me=string.Empty;
尝试
{
if(fileName.ToLower().IndexOf(“http:”)>-1)
{
System.Net.WebClient wc=新系统.Net.WebClient();
字节[]响应=wc.DownloadData(文件名);
sContents=System.Text.Encoding.ASCII.GetString(响应);
}
其他的
{
System.IO.StreamReader sr=新的System.IO.StreamReader(文件名);
sContents=sr.ReadToEnd();
高级关闭();
}
}
catch{sContents=“无法连接到服务器”;}
返回内容;
}