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

后端 未结 8 611
有刺的猬
有刺的猬 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:37

    An easy way to get an Address is through google's API.

    For example.

    using System.Xml;
    
    //Console.WriteLine("enter coordinate:");
    string coordinate = "32.797821,-96.781720"; //Console.ReadLine();
    
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load("https://maps.googleapis.com/maps/api/geocode/xml?latlng=" + coordinate);
    
    XmlNodeList xNodelst = xDoc.GetElementsByTagName("result");
    XmlNode xNode = xNodelst.Item(0);
    string FullAddress = xNode.SelectSingleNode("formatted_address").InnerText;
    string Number = xNode.SelectSingleNode("address_component[1]/long_name").InnerText;
    string Street = xNode.SelectSingleNode("address_component[2]/long_name").InnerText;
    string Village = xNode.SelectSingleNode("address_component[3]/long_name").InnerText;
    string Area = xNode.SelectSingleNode("address_component[4]/long_name").InnerText;
    string County = xNode.SelectSingleNode("address_component[5]/long_name").InnerText;
    string State = xNode.SelectSingleNode("address_component[6]/long_name").InnerText;
    string Zip = xNode.SelectSingleNode("address_component[8]/long_name").InnerText;
    string Country = xNode.SelectSingleNode("address_component[7]/long_name").InnerText;
    Console.WriteLine("Full Address: " + FullAddress);
    Console.WriteLine("Number: " + Number);
    Console.WriteLine("Street: " + Street);
    Console.WriteLine("Village: " + Village);
    Console.WriteLine("Area: " + Area);
    Console.WriteLine("County: " + County);
    Console.WriteLine("State: " + State);
    Console.WriteLine("Zip: " + Zip);
    Console.WriteLine("Country: " + Country);
    
    Console.ReadLine();
    

    P.S. be careful with different addresses with different components.

提交回复
热议问题