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

标签 c# winforms google-maps google-maps-api-3

我正在使用谷歌地图 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/

相关文章:

c# - MVVM 灯 : Pass object from view to viewmodel

C# 字典在删除相同的键后没有在最后一个索引处添加新项目?

c# - 防止 ComboBox 下拉和它的 AutoComplete 下拉外观冲突

c# - 如何以编程方式将文件包含在我的项目中?

android - 如何在 React Native 中离线使用谷歌地图

c# - UWP: MapControl 透明背景

c# - typeof(object).TypeHandle.Value 的替代方案

vb.net - ListView右键单击VB.Net

swift - Firebase 如何在单击按钮时在另一个 viewController 中显示右子值

android - 发布版本中的 Google Maps API key 不起作用