Use the Baidu map geocoding API to obtain the longitude and latitude information of a specific region or address

Source: Internet
Author: User

Geocoding API is a service interface provided by Baidu. It is mainly used to provide translation services from address resolution to latitude and longitude coordinates or from latitude and longitude coordinates to addresses, you can send a request to receive returned data in JSON or XML format.

Most of the code and solutions provided on the Internet are based on Google map. In fact, their solutions are similar. Here, I will send a link to show you how to use Baidu map API> Web Service API> geocoding API V2.0...

Http://developer.baidu.com/map/webservice-geocoding.htm#.E6.9C.8D.E5.8A.A1.E5.9C.B0.E5.9D.80

Geocoding API address resolution is used to obtain latitude and longitude data, which is called Based on the address and city name parameters. The following is a simple example of geocode. Send an address parameter request and return the geographic coordinates of the address. The request URL is as follows:

Http://api.map.baidu.com/geocoder/v2? Ak = 2ae1130ce176b450fb29e59a69b18407 & callback = renderoption & Output = xml & Address = No. 97 Wenhua Road & City = Zhengzhou

This is the request's Baidu map geocoding API service address, and so on. AK is the key. You can obtain it with a Baidu account. Nothing else can be said, whether it is to receive JSON or XML data, as you like, but you know how to change it in your own code ....

To be honest, there was no clue when I first got into touch with this thing, so I had a great deal of bad things on Baidu's cloud games. I found a lot of links, including JS, and so on. net. However, learning is always based on your own. for programming, you can use one sentence: standing on the shoulders of predecessors, thinking !!!!

This link has given me a lot of inspiration for today's topics. Thank you very much! Http://www.cnblogs.com/liuhaorain/archive/2012/05/04/2334018.html hope to help you too...

The test code is as follows:

Static void main (string [] ARGs) {console. writeline ("Enter the standard address information... "); string fulladress = convert. tostring (console. readline (); console. writeline ("enter the city name... "); string cityname = convert. tostring (console. readline (); WebClient client = new WebClient (); // WebClient client object string url = "http://api.map.baidu.com/geocoder/v2? Ak = 2ae1130ce176b450fb29e59a69b18407 & callback = renderoption & Output = xml & Address = "+ fulladress +" & City = "+ cityname +" "; client. encoding = encoding. utf8; // encoding format string responsetest = client. downloadstring (URL); // download XML response data xmldocument Doc = new xmldocument (); // create an XML document object if (! String. isnullorempty (responsetest) {Doc. loadxml (responsetest); // load the XML string // obtain the status information string XPath = @ "geocodersearchresponse/status"; xmlnode node = Doc. selectsinglenode (XPath); string status = node. innertext. tostring (); If (status = "0") {// obtain coordinate information XPath = @ "geocodersearchresponse/result/location"; node = Doc. selectsinglenode (XPath); string loc = node. innerxml. tostring (); Doc. save (@ "D: \ location. XML "); console. writeline ("coordinates: {0}", Loc); // output coordinate information} console. read ();}

It is worth mentioning that the request URL can be in the form of code. After sending the request, of course, you have to respond to the request and return the data string responsetest = client. downloadstring (URL); // download the XML response data...

After loading the response data, first judge the status and decide whether to return the data. The second is to obtain the coordinate information, which is critical. Many do not know how to modify it. In fact, it is to obtain the path of the desired data in the XML document, that is, it:

XPath = @ "geocodersearchresponse/result/location"; while Google map lacks, XPath = @ "geocoderesponse/result/formatted_address"; these two paths are actually different from the root node...

String loc = node. innerxml. tostring (); the Code obtains the data of subnodes under the location node, including Lat, LNG, and longitude and latitude. This is about node attributes,

1. innertext gets or sets the serial value of the node and all its subnodes. If you select this attribute, the obtained latitude and longitude values are separated by one line, for example, 39.910093116.403945.

2. The innerxml acquisition or setting only indicates the label of the subnode of the node. The result is <lat> 39.910093 </LAT> <LNG> 116.403945 </LNG>.

Finally, if you want to save the returned data locally, such as Doc. save (@ "D: \ location. XML "); in this way, you can obtain data for other purposes.

The words of the family are for reference only. learn together and make progress together. Thank you !!!

 

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.