Google Geolocation API - Use longitude and latitude to get address in textbox?

后端 未结 8 751
有刺的猬
有刺的猬 2021-01-31 05:25

I have noticed a lot of information about how to get your location using Google geolocation looks, based on IP address. But I am wondering if and how I could use this service t

8条回答
  •  眼角桃花
    2021-01-31 05:39

    protected void Button1_Click(object sender, EventArgs e)
        {
            this.calcularRota(txtOrigem.Text.Trim(), txtDestino.Text.Trim());
        }
    
    
        public void calcularRota(string latitude, string longitude)
        {
            //URL do distancematrix - adicionando endereco de origem e destino
            string url = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=false", latitude, longitude);
            XElement xml = XElement.Load(url);
    
            // verifica se o status é ok
            if (xml.Element("status").Value == "OK")
            {
                //Formatar a resposta
                Label3.Text = string.Format("Origem: {0}",
                    //Pegar endereço de origem 
                    xml.Element("result").Element("formatted_address").Value);
                //Pegar endereço de destino                    
            }
            else
            {
                Label3.Text = String.Concat("Ocorreu o seguinte erro: ", xml.Element("status").Value);
            }
        }
    }
    

提交回复
热议问题