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 );