Use Geocoding API in Android

Source: Internet
Author: User
Tags url example

Step 1: Construct the URL required by the Geocoder API
URL format
Https://maps.googleapis.com/maps/api/geocode/output? Parameters
The output option is json/xml, and the parameters option is address/language/sensor.
URL example
Https://maps.googleapis.com/maps/api/geocode/json? Address = Beijing Tiananmen & language = zh_CN & sensor = false
Step 2: Send an Http request to the API and return a json string
[Java]
String uriAPI = "http://maps.googleapis.com/maps/api/geocode/json? Address = % s & language = % s & sensor = % s ";
// Construct the complete URL of the Geocoder API
String url = String. format (uriAPI, addr, "zh_CN", "false ");
// Create a request object
HttpGet request = new HttpGet (url );
// Create an HttpClient object
HttpClient client = new DefaultHttpClient ();
// Get the Request Response object
HttpResponse response = client.exe cute (request );
// If the status code is 200, the request is successful.
If (response. getStatusLine (). getStatusCode () = 200 ){
// Get the response entry -- the entry is a json string
String resultStr = EntityUtils. toString (response. getEntity ());
GeoPoint = parseJson (resultStr );
}


Step 3: parse the json string
[Java]
// Parse the root element to obtain an array
JSONArray jsonObjs = new JSONObject (str). getJSONArray ("results ");
// Retrieve the first json object in the array (in this example, the array actually contains only one element)
JSONObject jsonObj = jsonObjs. getJSONObject (0 );
// Parse the formatted_address Value
String address = jsonObj. getString ("formatted_address ");
// Parse the geometry object in the json object
JSONObject geometry = jsonObj. getJSONObject ("geometry ");
// Parse the location object in the geometry object
JSONObject location = geometry. getJSONObject ("location ");
// Parse the latitude and longpolling values in the location object
String lat = location. getString ("lat ");
String lng = location. getString ("lng ");
DLati = Double. parseDouble (lat );
DLong = Double. parseDouble (lng );

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.