C # through Latitude to query specific address and region name _c# tutorial

Source: Internet
Author: User
Tags xpath
Recently, the project needs to query the specific address and region by latitude, through the query network resources, found that most of the provided to get the specific address of the region or the name of the city is not very good grasp; I got myself A:
Copy Code code as follows:

WebClient Client Objects
WebClient client = new WebClient ();
String url = "http://maps.google.com/maps/api/geocode/xml?latlng=" + Latitude + "," + Longitude + "&language=zh-cn&am P;sensor=false ";//Request Address
Client. Encoding = encoding.utf8;//encoding format
String responsetest = client. downloadstring (URL);
Downloading XML response data
string address = "";//returned addresses
XmlDocument doc = new XmlDocument ();
Creating an XML Document Object
if (!string. IsNullOrEmpty (ResponseTest))
{
Doc. Loadxml (ResponseTest);//Load XML string
Query status information
string XPath = @ "Geocoderesponse/status";
XmlNode node = doc. selectSingleNode (XPath);
String status = node. Innertext.tostring ();
if (status = = "OK") {
Query Detail 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 '] means to filter all dependent subnodes of type= ' sublocality ';
XmlNode idtst = idts[0];

String area = Idtst. selectSingleNode ("Short_name"). InnerText;
Address = address + "," + area;
}
}

Address is to obtain the specific location information and regional information;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.