Advertisement
Guest User

Untitled

a guest
Jan 16th, 2013
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1.  
  2. import org.apache.http.HttpEntity;
  3. import org.apache.http.HttpResponse;
  4. import org.apache.http.client.HttpClient;
  5. import org.apache.http.client.methods.HttpPost;
  6. import org.apache.http.impl.client.DefaultHttpClient;
  7. import org.apache.http.util.EntityUtils;
  8. import org.json.JSONArray;
  9. import org.json.JSONObject;
  10.  
  11. public class GettingAddress {
  12. public String getAddress(double latitude, double longitude) {
  13.  
  14. int flag=0;
  15. String strAddress = null;
  16. String url="http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude+","+ longitude+"&sensor=true";
  17.  
  18. //String url=http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false;
  19. HttpPost httppost = new HttpPost(url);
  20. HttpClient httpclient = new DefaultHttpClient();
  21. String result = "";
  22. try
  23. {
  24. HttpResponse response = httpclient.execute(httppost);
  25. HttpEntity resEntityGet = response.getEntity();
  26.  
  27. if (resEntityGet != null) {
  28. result = EntityUtils.toString(resEntityGet);
  29.  
  30. }
  31.  
  32.  
  33. }catch (Exception e) {
  34.  
  35. System.out.println("Exception:" + e);
  36. }
  37. System.out.println("Result: " + result);
  38.  
  39. try
  40. {
  41. if (result != null) {
  42. JSONObject json=new JSONObject(result);
  43.  
  44. JSONArray ja = json.getJSONArray("results");
  45.  
  46. for (int i = 0; i < ja.length(); i++) {
  47.  
  48. JSONObject jo = ja.getJSONObject(i);
  49.  
  50. JSONArray jaa = jo.getJSONArray("address_components");
  51. for (int j = 0; j < jaa.length(); j++) {
  52.  
  53. JSONObject jotwo = jaa.getJSONObject(j);
  54.  
  55. //long_name
  56. strAddress=jotwo.getString("long_name");
  57. System.err.println("%%%%%%%:4 " + strAddress );
  58. }
  59.  
  60.  
  61.  
  62. }
  63.  
  64. if(strAddress.length()>0)
  65. {
  66. flag =1;
  67. }
  68. System.err.println("%%%%%%%: " + strAddress );
  69. }
  70.  
  71. }catch (Exception e) {
  72.  
  73. }
  74. return strAddress;
  75. }
  76.  
  77.  
  78.  
  79. public static void main(String arg[])
  80. {
  81.  
  82. GettingAddress ga = new GettingAddress();
  83.  
  84. String str = ga.getAddress(28.563676300000000000, 77.162895899999960000);
  85.  
  86. System.err.println("Hello I am beautiful: " + str);
  87. }
  88. }
  89.  
  90.  
  91. Output:-
  92. %%%%%%%:4 A-15
  93. %%%%%%%:4 Block A
  94. %%%%%%%:4 New Delhi
  95. %%%%%%%:4 Delhi (district)
  96. %%%%%%%:4 Delhi
  97. %%%%%%%:4 India
  98. %%%%%%%:4 110057
  99. %%%%%%%:4 Vasant Vihar
  100. %%%%%%%:4 New Delhi
  101. %%%%%%%:4 South West Delhi
  102. %%%%%%%:4 Delhi
  103. %%%%%%%:4 India
  104. %%%%%%%:4 Vasant Vihar
  105. %%%%%%%:4 New Delhi
  106. %%%%%%%:4 South West Delhi
  107. %%%%%%%:4 Delhi
  108. %%%%%%%:4 India
  109. %%%%%%%:4 110057
  110. %%%%%%%:4 New Delhi
  111. %%%%%%%:4 South West Delhi
  112. %%%%%%%:4 Delhi
  113. %%%%%%%:4 India
  114. %%%%%%%:4 South West Delhi
  115. %%%%%%%:4 Delhi
  116. %%%%%%%:4 India
  117. %%%%%%%:4 New Delhi
  118. %%%%%%%:4 Delhi
  119. %%%%%%%:4 India
  120. %%%%%%%:4 Delhi
  121. %%%%%%%:4 India
  122. %%%%%%%:4 India
  123. %%%%%%%: India
  124. Hello I am beautiful: India
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement