Google Maps Reverse Geocoding in Java
Reverse Geocoding google map API is helpful tool (webservice) that converts given location on maps (latitude & langitude) into human readable address.
15.783 & 45.5698 ————> no:5, new street, t.nagar , Chennai -17.
Reverse Geocoding google map API (webservice) full url.
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false
where
latlang – Latitude & Langitude value of location on maps for which we need human readable address.
sensor – Indicates whether geocode request from device with sensor.
(note : sensor parameter must be true or false in the url while requesting result)
http://maps.googleapis.com/maps/api/geocode/json?latlng=13.0810,80.2740&sensor=true
Sample JSon output for the given latlng value (13.0810,80.2740).
“formatted_address” : “Poonamallee High Road, Park Town, Chennai, Tamil Nadu, India”,
“geometry” : {
“bounds” : {
“northeast” : {
“lat” : 13.08159910,
“lng” : 80.27514250
},
“southwest” : {
“lat” : 13.08136550,
“lng” : 80.27338689999999
}
},
“location” : {
“lat” : 13.08149240,
“lng” : 80.27426340
},
“location_type” : “APPROXIMATE”,
“viewport” : {
“northeast” : {
“lat” : 13.08283128029150,
“lng” : 80.27561368029150
},
“southwest” : {
“lat” : 13.08013331970850,
“lng” : 80.27291571970849
}
}
},
“types” : [ “route” ]
},
Now we are going get the needed human readable format address from 3 pages JSON ouptut using java.
Using URL class of java reading data from the given URL. The output was JSON formated string, so we need convert the JSON formated string into JSON object using JSONSerializer class.
note: please add following needed library files in your java project to get it work without any error.
Here is Java source code to get human readable format address from the given latitude & longitude value using Google maps reverse Geo-code Api.
package maps; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class Geolocation { public static void main(String[] args) { // making url request try { URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true"); // making connection HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setRequestProperty("Accept", "application/json"); if (conn.getResponseCode() != 200) { throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode()); } // Reading data's from url BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream()))); String output; String out=""; System.out.println("Output from Server .... \n"); while ((output = br.readLine()) != null) { //System.out.println(output); out+=output; } // Converting Json formatted string into JSON object JSONObject json = (JSONObject) JSONSerializer.toJSON(out); JSONArray results=json.getJSONArray("results"); JSONObject rec = results.getJSONObject(0); JSONArray address_components=rec.getJSONArray("address_components"); for(int i=0;i<address_components.size();i++){ JSONObject rec1 = address_components.getJSONObject(i); //trace(rec1.getString("long_name")); JSONArray types=rec1.getJSONArray("types"); String comp=types.getString(0); if(comp.equals("locality")){ System.out.println("city ————-"+rec1.getString("long_name")); } else if(comp.equals("country")){ System.out.println("country ———-"+rec1.getString("long_name")); } } String formatted_address = rec.getString("formatted_address"); System.out.println("formatted_address————–"+formatted_address); conn.disconnect(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts
If you want any of my script need to be customized according to your business requirement,
Please feel free to contact me [at] muni2explore[at]gmail.com
Note: But it will be charged based on your customization requirement