Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. try {
  2. addresses = geocoder.getFromLocation(latitude, longitude,1);}
  3. catch (IOException e)
  4. {
  5. e.printStackTrace();
  6. if(AppConstants.DEBUG)Log.v(AppConstants.DEBUG_TAG," e.printStackTrace(): "+e.getMessage());
  7. }
  8.  
  9. public JSONObject getLocationFormGoogle(String placesName) {
  10.  
  11. HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +placesName+"&ka&sensor=false");
  12. HttpClient client = new DefaultHttpClient();
  13. HttpResponse response;
  14. StringBuilder stringBuilder = new StringBuilder();
  15.  
  16. try {
  17. response = client.execute(httpGet);
  18. HttpEntity entity = response.getEntity();
  19. InputStream stream = entity.getContent();
  20. int b;
  21. while ((b = stream.read()) != -1) {
  22. stringBuilder.append((char) b);
  23. }
  24. } catch (ClientProtocolException e) {
  25. } catch (IOException e) {
  26. }
  27.  
  28. JSONObject jsonObject = new JSONObject();
  29. try {
  30. jsonObject = new JSONObject(stringBuilder.toString());
  31. } catch (JSONException e) {
  32.  
  33. e.printStackTrace();
  34. }
  35.  
  36. return jsonObject;
  37. }
  38.  
  39. public LatLng getLatLng(JSONObject jsonObject) {
  40.  
  41. Double lon = new Double(0);
  42. Double lat = new Double(0);
  43.  
  44. try {
  45.  
  46. lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
  47. .getJSONObject("geometry").getJSONObject("location")
  48. .getDouble("lng");
  49.  
  50. lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
  51. .getJSONObject("geometry").getJSONObject("location")
  52. .getDouble("lat");
  53.  
  54. } catch (JSONException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58.  
  59. return new LatLng(lat,lon);
  60.  
  61. }
  62.  
  63.  
  64.  
  65. LatLng Source =getLatLng(getLocationFormGoogle(placesName));
  66.  
  67. public static String getLocationCityName( double lat, double lon ){
  68. JSONObject result = getLocationFormGoogle(lat + "," + lon );
  69. return getCityAddress(result);
  70. }
  71.  
  72. protected static JSONObject getLocationFormGoogle(String placesName) {
  73.  
  74. String apiRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + placesName; //+ "&ka&sensor=false"
  75. ML.log(apiRequest);
  76. HttpGet httpGet = new HttpGet(apiRequest);
  77. HttpClient client = new DefaultHttpClient();
  78. HttpResponse response;
  79. StringBuilder stringBuilder = new StringBuilder();
  80.  
  81. try {
  82. response = client.execute(httpGet);
  83. HttpEntity entity = response.getEntity();
  84. InputStream stream = entity.getContent();
  85. int b;
  86. while ((b = stream.read()) != -1) {
  87. stringBuilder.append((char) b);
  88. }
  89. } catch (ClientProtocolException e) {
  90. } catch (IOException e) {
  91. }
  92.  
  93. JSONObject jsonObject = new JSONObject();
  94. try {
  95. jsonObject = new JSONObject(stringBuilder.toString());
  96. } catch (JSONException e) {
  97.  
  98. e.printStackTrace();
  99. }
  100.  
  101. return jsonObject;
  102. }
  103.  
  104. protected static String getCityAddress( JSONObject result ){
  105. if( result.has("results") ){
  106. try {
  107. JSONArray array = result.getJSONArray("results");
  108. if( array.length() > 0 ){
  109. JSONObject place = array.getJSONObject(0);
  110. JSONArray components = place.getJSONArray("address_components");
  111. for( int i = 0 ; i < components.length() ; i++ ){
  112. JSONObject component = components.getJSONObject(i);
  113. JSONArray types = component.getJSONArray("types");
  114. for( int j = 0 ; j < types.length() ; j ++ ){
  115. if( types.getString(j).equals("locality") ){
  116. return component.getString("long_name");
  117. }
  118. }
  119. }
  120. }
  121. } catch (JSONException e) {
  122. e.printStackTrace();
  123. }
  124. }
  125.  
  126. return null;
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement