如何使用地理位置获取城市名称并重定向到该城市的URL?

时间:2018-02-02 19:14:08

标签: javascript jquery html5 geolocation

例如,如果用户访问http://example.com并根据地理位置获取“纽约”作为城市,则将用户重定向到http://example.com/NY

如果是“洛杉矶”重定向到http://example.com/LA,如果是其他任何城市,则会将用户定向到第3个网址。

我该怎么做?

这是我目前的尝试/代码:

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public static void SendRequest(object sender, EventArgs e)
        {
            while (true)
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://api.coinbase.com/v2/prices/USD/spot?");
                using (var response = req.GetResponse())
                {
                    var html = new StreamReader(response.GetResponseStream()).ReadToEnd();
                    string Btc = Regex.Match(html, "\"BTC\",\"currency\":\"USD\",\"amount\":\"([^ \"]*)").ToString();
                }
                Thread.Sleep(300);
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        public void Value_Click(object sender, EventArgs e)
        {
            Value.Text = Btc;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

根据我在您的代码中看到的,您必须将您想要的城市添加到路线对象,如下所示:

{
 "Los Angeles": "http://google.com/LA", 
 "New York": "http://google.com/NY", 
 "Other City": "http://google.com/othercity"
}
相关问题