c looks up specific addresses and region names by latitude and longitude
- 2020-05-07 20:21:37
- OfStack
Recently, the project needs to query the specific address and regional name through the longitude and latitude. Through the query of network resources, it is found that most of the addresses provided are specific, but the acquisition of regional or city name is not very good grasp; Here's one of my own:
address refers to the specific address information and region information obtained;
//webclient Client object
WebClient client = new WebClient();
string url = "http://maps.google.com/maps/api/geocode/xml?latlng=" + latitude + "," + longitude + "&language=zh-CN&sensor=false";// Request the address
client.Encoding = Encoding.UTF8;// Coding format
string responseTest = client.DownloadString(url);
// download xml The response data
string address = "";// Return address
XmlDocument doc = new XmlDocument();
// create XML The document object
if (!string.IsNullOrEmpty(responseTest))
{
doc.LoadXml(responseTest);// loading xml string
// Query status information
string xpath = @"GeocodeResponse/status";
XmlNode node = doc.SelectSingleNode(xpath);
string status = node.InnerText.ToString();
if (status == "OK") {
// Query for detailed address information
xpath = @"GeocodeResponse/result/formatted_address";
node = doc.SelectSingleNode(xpath);
address = node.InnerText.ToString();
// Query area information
XmlNodeList nodeListAll = doc.SelectNodes("GeocodeResponse/result");
XmlNode idt = nodeListAll[0];
XmlNodeList idts = idt.SelectNodes("address_component[type='sublocality']");
//address_component[type='sublocality'] Said screening type='sublocality' All relevant child nodes of;
XmlNode idtst = idts[0];
string area = idtst.SelectSingleNode("short_name").InnerText;
address = address + "," + area;
}
}
address refers to the specific address information and region information obtained;