Guest User

Untitled

a guest
Jan 30th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. package com.example.coreoffice;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.HttpURLConnection;
  9. import java.net.URL;
  10. import java.net.URLEncoder;
  11. import java.util.HashMap;
  12. import java.util.List;
  13.  
  14. import org.json.JSONObject;
  15.  
  16. import android.os.AsyncTask;
  17. import android.os.Bundle;
  18. import android.support.v4.app.FragmentActivity;
  19. import android.util.Log;
  20. import android.widget.Button;
  21. import android.widget.EditText;
  22.  
  23. import com.example.coreoffice.library.GeocodeJSONParser;
  24. import com.google.android.gms.maps.CameraUpdate;
  25. import com.google.android.gms.maps.CameraUpdateFactory;
  26. import com.google.android.gms.maps.GoogleMap;
  27. import com.google.android.gms.maps.SupportMapFragment;
  28. import com.google.android.gms.maps.model.LatLng;
  29. import com.google.android.gms.maps.model.LatLngBounds;
  30. import com.google.android.gms.maps.model.Marker;
  31. import com.google.android.gms.maps.model.MarkerOptions;
  32.  
  33. public class MapsActivity extends FragmentActivity
  34. {
  35.  
  36. Button mBtnFind;
  37. GoogleMap mMap;
  38. EditText etPlace;
  39.  
  40. private static final String TAG = "MapsActivity";
  41. private int i;
  42. public String[] Post;
  43. public String[] Description;
  44. public String[] ID;
  45. public String[] Title;
  46.  
  47. public MapsActivity()
  48. {
  49. this.i = 0;
  50. }
  51.  
  52. @Override
  53. protected void onCreate(Bundle savedInstanceState)
  54. {
  55. super.onCreate(savedInstanceState);
  56. setContentView(R.layout.fragment_maps);
  57.  
  58. Bundle extras = getIntent().getExtras();
  59.  
  60. String[] Post = extras.getStringArray("Post");
  61. String[] Description = extras.getStringArray("Description");
  62. int[] ID = extras.getIntArray("ID");
  63. String[] Title = extras.getStringArray("Title");
  64.  
  65. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
  66.  
  67. mMap = mapFragment.getMap();
  68.  
  69. for (int i = 0; i < Post.length; i++)
  70. {
  71. String location = Post[i].toString();
  72.  
  73. String url = "https://maps.googleapis.com/maps/api/geocode/json?";
  74.  
  75. try
  76. {
  77. location = URLEncoder.encode(location, "utf-8");
  78. }
  79. catch (UnsupportedEncodingException e)
  80. {
  81. e.printStackTrace();
  82. }
  83.  
  84. String address = "address=" + location;
  85. String sensor = "sensor=false";
  86.  
  87. url = url + address + "&" + sensor;
  88. Log.v(TAG + "onClick", "url is: " + url);
  89.  
  90. DownloadTask downloadTask = new DownloadTask();
  91.  
  92. downloadTask.execute(url);
  93. }
  94. }
  95.  
  96. private String downloadUrl(String strUrl) throws IOException
  97. {
  98. String data = "";
  99. InputStream iStream = null;
  100. HttpURLConnection urlConnection = null;
  101. try
  102. {
  103. URL url = new URL(strUrl);
  104.  
  105. // Creating an http connection to communicate with url
  106. urlConnection = (HttpURLConnection) url.openConnection();
  107.  
  108. // Connecting to url
  109. urlConnection.connect();
  110.  
  111. // Reading data from url
  112. iStream = urlConnection.getInputStream();
  113.  
  114. BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
  115.  
  116. StringBuffer sb = new StringBuffer();
  117.  
  118. String line = "";
  119. while ((line = br.readLine()) != null)
  120. {
  121. sb.append(line);
  122. }
  123.  
  124. data = sb.toString();
  125.  
  126. br.close();
  127.  
  128. }
  129. catch (Exception e)
  130. {
  131. Log.d("Exception while downloading url", e.toString());
  132. }
  133. finally
  134. {
  135. iStream.close();
  136. urlConnection.disconnect();
  137. }
  138.  
  139. return data;
  140.  
  141. }
  142.  
  143. /** A class, to download Places from Geocoding webservice */
  144. private class DownloadTask extends AsyncTask<String, Integer, String>
  145. {
  146.  
  147. String data = null;
  148.  
  149. // Invoked by execute() method of this object
  150. @Override
  151. protected String doInBackground(String... url)
  152. {
  153. try
  154. {
  155. data = downloadUrl(url[0]);
  156. }
  157. catch (Exception e)
  158. {
  159. Log.d("Background Task", e.toString());
  160. }
  161. return data;
  162. }
  163.  
  164. // Executed after the complete execution of doInBackground() method
  165. @Override
  166. protected void onPostExecute(String result)
  167. {
  168.  
  169. ParserTask parserTask = new ParserTask();
  170. parserTask.execute(result);
  171. }
  172.  
  173. }
  174.  
  175. class ParserTask extends AsyncTask<String, Integer, List<HashMap<String, String>>>
  176. {
  177.  
  178. JSONObject jObject;
  179.  
  180. @Override
  181. protected List<HashMap<String, String>> doInBackground(String... jsonData)
  182. {
  183.  
  184. List<HashMap<String, String>> places = null;
  185. GeocodeJSONParser parser = new GeocodeJSONParser();
  186.  
  187. try
  188. {
  189. jObject = new JSONObject(jsonData[0]);
  190.  
  191. /** Getting the parsed data as a an ArrayList */
  192. places = parser.parse(jObject);
  193.  
  194. }
  195. catch (Exception e)
  196. {
  197. Log.d("Exception", e.toString());
  198. }
  199. return places;
  200. }
  201.  
  202. // Executed after the complete execution of doInBackground() method
  203. @Override
  204. protected void onPostExecute(List<HashMap<String, String>> list)
  205. {
  206. final int padding = 30;
  207.  
  208. for (int i = 0; i < list.size(); i++)
  209. {
  210.  
  211. // Creating a marker
  212. MarkerOptions markerOptions = new MarkerOptions();
  213. HashMap<String, String> hmPlace = list.get(i);
  214.  
  215. double lat = Double.parseDouble(hmPlace.get("lat"));
  216. double lng = Double.parseDouble(hmPlace.get("lng"));
  217.  
  218. String name = hmPlace.get("formatted_address");
  219. LatLng latLng = new LatLng(lat, lng);
  220. markerOptions.position(latLng);
  221. markerOptions.title(name);
  222.  
  223. // mMap.addMarker(markerOptions);
  224.  
  225. Marker storeMarker = mMap.addMarker(new MarkerOptions().position(latLng).title(name).snippet("test"));
  226.  
  227. //CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(lat, lng));
  228. //CameraUpdate zoom = CameraUpdateFactory.zoomTo(10);
  229.  
  230. //mMap.moveCamera(center);
  231. //mMap.animateCamera(zoom);
  232.  
  233. LatLngBounds bounds = LatLngBounds.builder().include(latLng).build();
  234. mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, padding));
  235.  
  236. }
  237. }
  238. }
  239.  
  240. }
Advertisement
Add Comment
Please, Sign In to add comment