Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class GettingAddress {
- public String getAddress(double latitude, double longitude) {
- int flag=0;
- String strAddress = null;
- String url="http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude+","+ longitude+"&sensor=true";
- //String url=http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false;
- HttpPost httppost = new HttpPost(url);
- HttpClient httpclient = new DefaultHttpClient();
- String result = "";
- try
- {
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity resEntityGet = response.getEntity();
- if (resEntityGet != null) {
- result = EntityUtils.toString(resEntityGet);
- }
- }catch (Exception e) {
- System.out.println("Exception:" + e);
- }
- System.out.println("Result: " + result);
- try
- {
- if (result != null) {
- JSONObject json=new JSONObject(result);
- JSONArray ja = json.getJSONArray("results");
- for (int i = 0; i < ja.length(); i++) {
- JSONObject jo = ja.getJSONObject(i);
- JSONArray jaa = jo.getJSONArray("address_components");
- for (int j = 0; j < jaa.length(); j++) {
- JSONObject jotwo = jaa.getJSONObject(j);
- //long_name
- strAddress=jotwo.getString("long_name");
- System.err.println("%%%%%%%:4 " + strAddress );
- }
- }
- if(strAddress.length()>0)
- {
- flag =1;
- }
- System.err.println("%%%%%%%: " + strAddress );
- }
- }catch (Exception e) {
- }
- return strAddress;
- }
- public static void main(String arg[])
- {
- GettingAddress ga = new GettingAddress();
- String str = ga.getAddress(28.563676300000000000, 77.162895899999960000);
- System.err.println("Hello I am beautiful: " + str);
- }
- }
- Output:-
- %%%%%%%:4 A-15
- %%%%%%%:4 Block A
- %%%%%%%:4 New Delhi
- %%%%%%%:4 Delhi (district)
- %%%%%%%:4 Delhi
- %%%%%%%:4 India
- %%%%%%%:4 110057
- %%%%%%%:4 Vasant Vihar
- %%%%%%%:4 New Delhi
- %%%%%%%:4 South West Delhi
- %%%%%%%:4 Delhi
- %%%%%%%:4 India
- %%%%%%%:4 Vasant Vihar
- %%%%%%%:4 New Delhi
- %%%%%%%:4 South West Delhi
- %%%%%%%:4 Delhi
- %%%%%%%:4 India
- %%%%%%%:4 110057
- %%%%%%%:4 New Delhi
- %%%%%%%:4 South West Delhi
- %%%%%%%:4 Delhi
- %%%%%%%:4 India
- %%%%%%%:4 South West Delhi
- %%%%%%%:4 Delhi
- %%%%%%%:4 India
- %%%%%%%:4 New Delhi
- %%%%%%%:4 Delhi
- %%%%%%%:4 India
- %%%%%%%:4 Delhi
- %%%%%%%:4 India
- %%%%%%%:4 India
- %%%%%%%: India
- Hello I am beautiful: India
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement