How does the GOOGLE MAP API get address information via latitude?

Source: Internet
Author: User
Tags http request json web services
How to obtain address information through latitude and longitude.
Summary
Google Maps API WEB Services is a collection of HTTP interfaces for Google services that provide geographic data for your map application. Includes: Google Geocoding API, Google Directions API, Google Elevation API, Google Places API. This article explores how to get address information through the Google Geocoding API service.
Directory
What is Network service. Address resolution and anti-Address resolution address query (anti-Address resolution) Request address query (anti-address resolution) Response processing response results
1. What is a network service?
The Google Maps API provides these Web services as an interface to request Google Maps API data from external services and to use them in your map application. These network services use HTTP requests for specific URLs and provide URL parameters as parameters to the service. In general, these services 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 typically takes the following form: Http://maps.google.com/maps/api/service/output?parameters where service represents the specific service requested, output Represents the response format (typically JSON or XML).

2. Address resolution and anti-address resolution
Address resolution is the process of converting addresses (such as "Amphitheatre Parkway, Mountain View, CA") to geographic coordinates (such as latitude 37.423021 and longitude-122.083739), You can place markers or locate the map based on the coordinates you have converted. The Google Geocoding API allows you to access the address resolver directly through an HTTP request. In addition, the service allows you to perform reverse operations (converting coordinates to addresses), which 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 output can be one of the following values:
JSON (recommended) means that output XML representations in the form of JavaScript Object Notation (JSON) are required to output some parameters as XML, and some are optional. All parameters are delimited by the character & (&) According to the URL's criteria. These parameters and their possible values are enumerated below.
The Google Geocoding API defines address query requests using the following URL parameters:
LATLNG (required)-The latitude/Longitude text value that you want to get the nearest, manually read address. Bounds (optional)-The bounding rectangle of the viewable area in which to offset the address resolution results more significantly. Region (optional)-area code, specified as the CcTLD ("Top-level domain") double-character value. Language (optional)-the language in which the results are returned. Please note that the supported languages are updated frequently, so this list may not be exhaustive. If language is not provided, the address resolver attempts to use the local language of the region that sent the request whenever possible. Sensor (required)-Indicates whether the address resolution request is from a device with a location sensor installed. The value must be true or false. Note: The bounds and region parameters only affect the results returned by the address resolver, but they cannot be fully restricted.
Instance one: request to create address information for query coordinates (39.910093,116.403945) that requires output response in XML format, 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 in which latitude is written is (latitude, longitude).

Example two: 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 is returned in the format indicated by the output tag in the URL request path. The XML response consists of one and two top-level elements:
Contains the status code in the request. (very important) 0 or more elements, each containing a separate set of address resolution address information and geometry information. The "Status" field in the Address resolution response object contains the status of the request and may contain debugging information to help you trace why address resolution is not working correctly.
The Status field may contain the following values:
"OK" means no error occurred, the address was successfully parsed and at least one address resolution was returned. (Judging if the request was successful) "Zero_results" means that the address resolution was successful, but the result was not returned. This can occur if the address or latlng of the remote location passed in the resolution process does not exist. "Over_query_limit" means you are exceeding the quota. "Request_denied" indicates that your request was rejected, usually because of a missing sensor parameter. "Invalid_request" usually indicates a missing query parameter (address or LATLNG). Instance one: Enter the request in the above instance one in IE browse to view the response result.
The following information is displayed in the browser (this screenshot is only part of the information that responds to the result):
Example two: The response of the above instance two is output through 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 program      {        static void Main (string[] args)          {             webclient client = new WebClient ();             string url = "http://maps.google.com/ Maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-cn&sensor=false ";             client. Encoding = Encoding.UTF8;             string responseTest = client. downloadstring (URL); 17               console.write ("{0}", ResponseTest); 19              console.read (); 21          }     } 24}
Output (This screenshot is just some of the information that responds to the result):
5. Processing response Results
Through the above, we have been able to get the XML response information. However, the response result contains a lot of information, so we need to resolve the required address information. The specific implementation process is:
The first step: determine status information.
Step two: Get formatted_address address information.
Note: Formatted_address is a string that contains human-readable addresses for this location. Usually the address is equivalent to a "postal address" and sometimes varies by country/region.
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 {Ten     class program one    & nbsp {        static void Main (string[] args)          {            webclient Client = new WebClient ();//webclient Clients Object              string url = "Http://maps.google.com/maps/api/geocode/xml?latlng=39.910093,116.403945&language=zh-CN &sensor=false ";//Request Address             client. Encoding = encoding.utf8;//encoded format             string ResponseTest = client. Downloadstring (URL);//Download XML response data

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.