Joke Collection Website - Public benefit messages - How to obtain address information by latitude and longitude

How to obtain address information by latitude and longitude

1. What is a network service?

Google Maps API provides these web services as interfaces for requesting Google Maps API data from external services and using them in your map application. These network services use HTTP requests with specific URLs and provide URL parameters as parameters of the service. Generally speaking, these services will return data in the form of JSON or XML in an HTTP request for your application to parse and/or process.

A typical network service request usually takes the following forms:

http://maps . g oogl e.co m/maps/API/service/output? factor

Where service represents the specific service requested and output represents the response format (usually json or xml).

2. Address resolution and anti-address resolution

Address resolution is the process of converting an address (such as "Mountain View City, California 1600 Amphitorium Baihui Road") into geographical coordinates (such as latitude 37.42302 1, longitude-122.083739), and you can place marks or locate maps according to the converted coordinates. Google geocoding API allows you to access the address resolver directly through HTTP requests. In addition, the service allows you to perform the reverse operation (converting coordinates into addresses), a process called "reverse address resolution" (address query).

3. Address query (reverse address resolution) request

Google geocoding API requests must be in the following form:

http://maps . g oogl e . c om/maps/API/geocode/output? factor

Where the output can be one of the following values:

Json (suggested) means output in the form of JavaScript object representation (JSON).

The Xml representation is output in the form of XML.

Some parameters are required and some are optional. According to the standard of the website, all parameters use the character &; (&) separation. These parameters and their possible values are listed below.

Google geocoding API uses the following URL parameters to define address query requests:

Latlng (required)-The latest text value of latitude/longitude you want to obtain, and the address can be read manually.

Bounds (optional)-The boundary of the visible area in which you want to offset the address resolution results more significantly.

Region (optional)-The region code, which is specified as a two-character value of ccTLD ("top-level domain name").

Language (optional)-The language in which the results are returned. Please note that we often update the supported languages, so this list may not be exhaustive. If no language is provided, the address resolver will try to use the local language of the region where the request is sent.

Sensor (required)-Indicates whether the address resolution request is from a device equipped with a location sensor. The value must be true or false.

Note: The boundary and region parameters will only affect the results returned by the address parser, but they cannot be completely restricted.

Example 1: The request to create the address information of query coordinates (39.9 10093,16.403945) requires that the response be output in xml format, and the language is Simplified Chinese (zh-CN).

Note: The writing order of latitude and longitude is (latitude, longitude).

Example 2: Use C# to create the above request in the client program.

1 WebClient client = new WebClient();

2 string URL = " http://m APS . g oogl e.co m/maps/API/geocode/XM l? latln g=39.9 10? 093, 1 16.403945 & amp; Language = zh-cn&; Sensor = false ";

3 client. Code = code. UTF8

4 string responseTest = client. download string(URL);

5. Address query (reverse address resolution) response

The address resolution response will be returned in the format indicated by the output tag in the URL request path. The XML response contains a

& lt Status & gt Include the status code in the request. (very important)

Zero or more

The "Status" field in the address resolution response object contains the status of the request and may contain debugging information to help you trace back the reason why address resolution does not work properly.

The Status field may contain the following values:

"OK" means that no error has occurred; Address resolution succeeded, and at least one address resolution result was returned. (Judge whether the request has been successfully responded)

"ZERO_RESULTS" indicates that the address was resolved successfully, but no results were returned. This happens if the remote location address or latlng passed in the address resolution process does not exist.

"OVER_QUERY_LIMIT" means that you have exceeded the limit.

"REQUEST_DENIED" means that your request was rejected, usually due to missing sensor parameters.

"INVALID_REQUEST" usually means missing query parameters (address or query).

Example 1: Enter the request in the above example 1 in IE browsing to view the response results.

The following information is displayed in the browser (this screenshot is only part of the response result):

Example 2: Output the response of Example 2 above through the console.

C# code:

1 Use the system;

2 use the system. Assemble. Generics;

3 use the system. Linq

4 use the system. Text;

5 Use System.Net;

six

7 namespace geocoding test

8 {

Level 9 program

10 {

1 1 static void Main(string[] args)

12 {

13 WebClient client = new WebClient();

14 string URL = " ht TP://maps . go ogle . c om/maps/a pi/geocode/XML? latlng=39.9 10093, 1 16.403945 & amp; Language = zh-cn&; Sensor = false ";

15 client. Code = code. UTF8

16 string response test = client. download string(URL);

17

18 console. write(“{ 0 }”,response test);

19

20 console. read();

2 1

22 }

23 }

24 }

Output results (this screenshot is only part of the response results):

5. Processing response results

Through the above content, we have been able to get xml response information. However, the response result contains a lot of information, so we need to analyze the required address information. The specific implementation process is as follows:

Step 1: Judge the status information of the status.

Step 2: Get the formatted_address address information.

Note: formatted_address is a string containing the readable address of this location. Usually, this address is equivalent to a "postal address", which sometimes varies from country to country.

The implementation code is as follows:

View code