How can I obtain address information through latitude and longitude?

Source: Internet
Author: User
Document directory
  • Summary
  • Directory
Summary

Google Maps API Web Services is a set of HTTP interfaces for Google services that provide geographical data for your map applications.These include: Google geocoding API, google directions API, Google elevation API, and Google places API. This article will discuss how to use Google geocoding
API service to obtain the address information.

Directory
  • What is a network service?
  • Distinguish between address resolution and anti-Address Resolution
  • Address query (Anti-Address Resolution) Request
  • Address query (Reverse Address Resolution) Response
  • Process Response Results

 

1. What is a network service?

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

A typical network service request usually takes the following form:

http://maps.google.com/maps/api/service/output?parameters

WhereserviceIndicates the requested service,outputResponse format (usually
jsonOrxml).

 

2. Address Resolution and Reverse Address Resolution

Address Resolution is the process of converting an address (such as "1600 amphitheatre Parkway, Mountain View, ca") to a geographical coordinate (such as latitude 37.423021 and longitude-122.083739, you can place tags or locate maps based on the converted coordinates. Google geocoding API allows you to directly access the address parser through HTTP requests. In addition, this service allows you to perform reverse operations.(Convert coordinates to addresses). This process is called "Reverse Address Resolution" (address query).

 

3. address query (Reverse Address Resolution) Request

Google geocoding API requests must take the following form:

http://maps.google.com/maps/api/geocode/output?parameters

Where,outputIt can be one of the following values:

  • json(Recommended) output in the form of JavaScript Object Notation (JSON)
  • xmlOutput in XML format

Some parameters are required and some are optional. According to the URL standard, all parameters use characters &(&. These parameters and their possible values are enumerated below.

The Google geocoding API uses the following URL parameters to define an address query request:

  • latlng(Required)-The latitude/longitude text value that you want to obtain, which is closest to, and can be manually read from the address.
  • bounds(Optional)-the border of the visible area in which the address resolution result is to be significantly offset.
  • region(Optional)-Region code, which is a dual-character value of ccTLD ("top-level domain.
  • language(Optional)-language used to return the result. Note that the Supported languages are updated frequently, so the list may not be detailed. If not
    languageThe address parser tries to use the local language of the region where the request is sent as much as possible.
  • sensor(Required)-indicates whether the address resolution request comes from a device with a location sensor.The value must be
    trueOr
    false.

Note:boundsAnd
regionParameters only affect the results returned by the address parser, but do not limit the results completely.

 

Instance 1:To create a request to query the address information of coordinates (39.910093, 116.403945), the response must be output in XML format. The language is simplified Chinese (zh-CN ).

http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false

Note:The 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://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false";3 client.Encoding = Encoding.UTF8;4 string responseTest = client.DownloadString(url);

 

4. address query (Reverse Address Resolution) Response

The address resolution response will startIn the URL request path
outputMark the indicated format
. The XML response contains<GeocodeResponse>And two top-level elements:

  • <status>Request containedStatus Code. (Very important)
  • Zero or multiple<result>Each element contains a separate set of Address Resolution address information and geometric information.

In the address resolution response object"status"The field contains the Request status and debugging information, to help you trace the reason why address resolution is not working properly.

"status"The field may contain the following values:

  • "OK"Indicates that no error occurred. The address is successfully parsed and at least one address resolution result is returned. (Determine whether the request is responded successfully)
  • "ZERO_RESULTS"The address is successfully parsed, but no result is returned. If the remote location passed during address resolution
    addressOr
    latlngIt does not exist.
  • "OVER_QUERY_LIMIT"Indicates that you have exceeded the quota.
  • "REQUEST_DENIED"Indicates that your request is rejected, usually due to lack
    sensorParameters.
  • "INVALID_REQUEST"It usually indicates that the query parameter is missing (addressOr
    latlng).

Instance 1:In IE browser, enter the request in instance medium to view the response result.

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

 

Example 2:Output the response of instance 2 on the console.

C # code:

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net; 6  7 namespace GeoCodeTest 8 { 9     class Program10     {11         static void Main(string[] args)12         {13             WebClient client = new WebClient();14             string url = "http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN&sensor=false";15             client.Encoding = Encoding.UTF8;16             string responseTest = client.DownloadString(url);17 18             Console.Write("{0}",responseTest);19 20             Console.Read();21 22         }23     }24 }

Output result (this is only part of the response result ):

 

5. Process Response Results

Through the above content, we can get the XML response information. However, the response results contain a lot of information, so we need to parse the desired address information. The specific implementation process is as follows:

Step 1:Status.

Step 2:Obtainformatted_addressAddress information.

Note:formatted_addressIs a string containing the human-readable address of this location. Generally, this address is equivalent to a "Postal Address", which sometimes varies with different countries/regions.

The implementation code is as follows:

View
Code

1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using system. net; 6 using system. XML; 7 8 namespace geocodetest 9 {10 class program11 {12 static void main (string [] ARGs) 13 {14 WebClient client = new WebClient (); // WebClient client object 15 string url = "http://maps.google.com/maps/api/geocode/xml? Latlng = 39.910093, 116.403945 & language = ZH-CN & sensor = false "; // request address 16 client. encoding = encoding. utf8; // encoding format 17 string responsetest = client. downloadstring (URL); // download XML response data 18 19 xmldocument Doc = new xmldocument (); // create an XML Document Object 20 21 if (! String. isnullorempty (responsetest) 22 {23 Doc. loadxml (responsetest); // load XML string 24 25 // obtain status information 26 string XPath = @ "geocoderesponse/status"; 27 xmlnode node = Doc. selectsinglenode (XPath); 28 string status = node. innertext. tostring (); 29 30 if (status = "OK") 31 {32 // get address information 33 XPath = @ "geocoderesponse/result/formatted_address"; 34 node = Doc. selectsinglenode (XPath); 35 string address = node. innertext. tostring (); 36 37 console. writeline ("Address: {0}", address); // output address information 38} 39 40} 41 42 43 console. read (); 44 45} 46} 47}

Output result:

Author: Komi kiddie

Source: http://liuhaorain.cnblogs.com

You are welcome to repost or share it, but be sure to declare the source of the article. If the article is helpful to you, I hope you can recommend it or follow it.

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.