Step 1: Construct the URL required by the geocoder APIURL 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 exampleHttps://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
String uriapi = "http://maps.googleapis.com/maps/api/geocode/json? Address = % S & language = % S & sensor = % s "; // construct the complete URL of geocoder API = string. format (uriapi, ADDR, "zh_cn", "false"); // create a request object httpget request = new httpget (URL ); // create the httpclient object httpclient client = new defaulthttpclient (); // obtain the Request Response object httpresponse response = client.exe cute (request); // If the status code is 200, if (response. getstatusline (). getstatuscode () = 200) {// obtain the response entry -- this entry is a JSON string resultstr = entityutils. tostring (response. getentity (); geopoint = parsejson (resultstr );}
Step 3: parse the JSON string
// Parse the root element to obtain an array jsonarray jsonobjs = new jsonobject (STR ). getjsonarray ("Results"); // retrieves the first JSON object in the array (in this example, the array actually contains only one element). jsonobject jsonobj = jsonobjs. getjsonobject (0); // parses the formatted_address value string address = jsonobj. getstring ("formatted_address"); // parse the geometry jsonobject geometry = jsonobj in the json object. getjsonobject ("Geometry"); // parse the location object jsonobject location = geometry in the geometry object. getjsonobject ("location"); // parses 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 );
Google geocoding API Introduction
Https://developers.google.com/maps/documentation/geocoding? Hl = ZH-CN