Guest User

Untitled

a guest
May 21st, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.00 KB | None | 0 0
  1. public class FetchJSON extends AsyncTask<Void,Void,Void> {
  2. String data = "";
  3. String id, name, address, lat, lng, type = "";
  4.  
  5. @Override
  6. protected Void doInBackground(Void... voids) {
  7. Log.i("", "TEST");
  8. try {
  9. URL url = new URL("https://api.myjson.com/bins/ehzqu");
  10. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  11. InputStream inputStream = httpURLConnection.getInputStream();
  12. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  13. String line = "";
  14. while(line!=null){
  15. line = bufferedReader.readLine();
  16. data = data + line;
  17. }
  18. Log.i("", "TEST2");
  19. JSONArray JA = new JSONArray(data);
  20. for( int i = 0 ; i < JA.length();i++)
  21. {
  22. JSONObject JO = (JSONObject) JA.get(i);
  23. //Log.i("", JO.toString());
  24.  
  25. id = (String) JO.get("id");
  26. name = (String) JO.get("name");
  27. address = (String) JO.get("address");
  28.  
  29. lat = JO.get("lat").toString();
  30. lng = JO.get("lng").toString();
  31. Log.i("JSON Values", lat + " " + lng); //////////////////////////
  32. type = (String) JO.get("type");
  33. }
  34. Log.i("", "TEST3");
  35.  
  36. } catch (MalformedURLException e) {
  37. e.printStackTrace();
  38.  
  39. } catch (IOException e) {
  40. e.printStackTrace();
  41. } catch (JSONException e) {
  42. e.printStackTrace();
  43. }
  44.  
  45.  
  46. return null;
  47. }
  48.  
  49.  
  50. @Override
  51. protected void onPostExecute(Void aVoid) {
  52.  
  53. TrackDifferentLocation.data.setText(this.data);
  54.  
  55.  
  56. super.onPostExecute(aVoid);
  57. }
  58.  
  59. public void printLat()
  60. {
  61. Log.i("", lat);
  62. }
  63.  
  64. public String getLat (){
  65. return lat;
  66. }
  67. public String getLng (){
  68. return lng;
  69. }
  70. public String getName (){
  71. return name;
  72. }
  73. public String getAddress (){
  74. return address;
  75. }
  76. public String getID (){
  77. return id;
  78. }
  79. public String getType (){
  80. return type;
  81. }
  82. }
  83.  
  84. public class TrackDifferentLocation extends AppCompatActivity implements OnMapReadyCallback {
  85.  
  86. private GoogleMap mMap;
  87.  
  88. String json_string;
  89. public static TextView data;
  90.  
  91. @Override
  92. protected void onCreate (Bundle savedInstanceState) {
  93. super.onCreate(savedInstanceState);
  94. //Toast.makeText(this, "Tracking location...", Toast.LENGTH_LONG).show();
  95. setContentView(R.layout.activity_track_different_location);
  96. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  97. .findFragmentById(R.id.map_fragment);
  98. mapFragment.getMapAsync(this);
  99. getSupportActionBar().setDisplayShowHomeEnabled(true);
  100. getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  101.  
  102. }
  103.  
  104. @Override
  105. public void onMapReady(GoogleMap googleMap) {
  106. mMap = googleMap;
  107. Log.i("", "onMapReady()");
  108. FetchJSON f = new FetchJSON();
  109.  
  110. // f.execute();
  111. // f.printLat();
  112. //Log.i("", f.getLat());
  113. //Log.i("", f.getLng());
  114.  
  115. double latitude = Double.valueOf(f.getLat());
  116. double longitude = Double.valueOf(f.getLng());
  117. LatLng latlng = new LatLng(latitude,longitude);
  118. //Log.d("Marker: ", m.getTitle());
  119. mMap.addMarker(new MarkerOptions().position(latlng));
  120. }
  121.  
  122. //Part of menu see following
  123. @Override
  124. public boolean onCreateOptionsMenu(Menu menu) {
  125. getMenuInflater().inflate(R.menu.menu, menu);
  126. return super.onCreateOptionsMenu(menu);
  127. }
  128.  
  129. @Override
  130. public boolean onOptionsItemSelected(MenuItem item) {
  131. int id = item.getItemId();
  132. if (id == android.R.id.home){
  133. //ends the activity
  134. this.finish();
  135. }
  136. switch (item.getItemId()) {
  137. case R.id.mapTypeNone:
  138. mMap.setMapType(GoogleMap.MAP_TYPE_NONE);
  139. break;
  140. case R.id.mapTypeNormal:
  141. mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  142. break;
  143. case R.id.mapTypeTerrain:
  144. mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
  145. break;
  146. case R.id.mapTypeSatellite:
  147. mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
  148. break;
  149. case R.id.mapTypeHybrid:
  150. mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
  151. break;
  152. default:
  153. break;
  154. }
  155. return super.onOptionsItemSelected(item);
  156. }
  157.  
  158. @Override
  159. public void onMapReady(GoogleMap googleMap) {
  160. mMap = googleMap;
  161.  
  162. displayMarker();
  163. }
  164.  
  165.  
  166. private void loadLocation() {
  167. new FetchJSON().execute();
  168. }
  169.  
  170.  
  171. class FetchJSON extends AsyncTask<String, Integer, LatLng> {
  172.  
  173. @Override
  174. protected LatLng doInBackground(String... params) {
  175. LatLng latLng = null;
  176. try {
  177. URL url = new URL("https://api.myjson.com/bins/ehzqu");
  178. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  179. InputStream inputStream = httpURLConnection.getInputStream();
  180. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  181.  
  182. StringBuilder stringBuilder = new StringBuilder();
  183. String line = "";
  184. while ((line = bufferedReader.readLine()) != null) {
  185. stringBuilder.append(line).append("n");
  186. }
  187. bufferedReader.close();
  188.  
  189. String json = stringBuilder.toString();
  190.  
  191. Log.e(TAG, "Return = " + json);
  192. String lat= "";
  193. String lng= "";
  194. JSONObject obj = new JSONObject(json);
  195. JSONArray array = obj.getJSONArray("server response");
  196. for(int i = 0; i < array.length(); i++){
  197. JSONObject o = array.getJSONObject(i);
  198.  
  199. lat = o.optString("lat");
  200. lng = o.optString("lng");
  201. }
  202.  
  203. Log.e(TAG, "Lat = " + lat);
  204. Log.e(TAG, "lng = " + lng);
  205.  
  206. double latDouble = Double.parseDouble(lat);
  207. double lngDouble = Double.parseDouble(lng);
  208.  
  209. latLng = new LatLng(latDouble, lngDouble);
  210. }
  211. catch (Exception ex) {
  212. Log.e(TAG, "doInBackground --- " + ex.getMessage());
  213. }
  214. return latLng;
  215. }
  216.  
  217.  
  218. @Override
  219. protected void onPostExecute(LatLng latLng) {
  220. try{
  221. if(latLng != null){
  222. mLatLng = latLng;
  223. displayMarker();
  224. }
  225.  
  226. }
  227. catch(Exception ex){
  228. Log.e(TAG, "onPostExecute" + ex.getMessage());
  229. }
  230. }
  231.  
  232. }
  233.  
  234. private void displayMarker(){
  235. if(mMap == null) return;
  236. if(mLatLng == null) return;
  237.  
  238. MarkerOptions markerOption = new MarkerOptions();
  239. markerOption.position(mLatLng);
  240. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLng, zoomFactor));
  241. mMap.addMarker(markerOption);
  242. }
  243.  
  244. @Override
  245. public void onMapReady(GoogleMap googleMap) {
  246. mMap = googleMap;
  247. Log.i("", "onMapReady()");
  248. FetchJSON f = new FetchJSON(mMap);
  249. }
  250.  
  251. @Override
  252. protected Void doInBackground(Void... voids) {
  253. ....
  254. lat = JO.get("lat").toString();
  255. lng = JO.get("lng").toString();
  256. type = (String) JO.get("type");
  257.  
  258. // Set the marker here:
  259. double latitude = Double.valueOf(f.getLat());
  260. double longitude = Double.valueOf(f.getLng());
  261. LatLng latlng = new LatLng(latitude,longitude);
  262. mMap.addMarker(new MarkerOptions().position(latlng));
  263. ....
  264. }
  265.  
  266. LatLng latlng = null;
  267.  
  268. @Override
  269. protected Long doInBackground(URL... urls) {
  270. ...
  271. double latitude = Double.valueOf(f.getLat());
  272. double longitude = Double.valueOf(f.getLng());
  273. latlng = new LatLng(latitude,longitude);
  274. ....
  275. }
  276.  
  277. @Override
  278. protected void onPostExecute(Long result) {
  279. if(latlng != null)
  280. mMap.addMarker(new MarkerOptions().position(latlng));
  281. }
  282.  
  283. String data = "{"server response":[{"id":"991","name":"GPSname","address":"GPSaddress","lat":"52.948616","lng":"-1.169131","type":"GPStype"}]}";
  284. Log.i("TAG", data);
  285.  
  286. String id, name, address, lat, lng, type;
  287. try {
  288. JSONObject json = new JSONObject(data);
  289. JSONArray serverResponse = json.getJSONArray("server response");
  290.  
  291. for (int i = 0; i < serverResponse.length(); i++) {
  292. JSONObject JO = (JSONObject) serverResponse.get(i);
  293. id = (String) JO.get("id");
  294. name = (String) JO.get("name");
  295. address = (String) JO.get("address");
  296.  
  297. lat = JO.get("lat").toString();
  298. lng = JO.get("lng").toString();
  299. type = (String) JO.get("type");
  300.  
  301. Log.i("TAG", String.format(
  302. "JSON Values: id=%s, name=%s, address=%s, lat=%s, lng=%s, type=%s",
  303. id, name, address, lat, lng, type));
  304. }
  305. } catch (Exception e) {
  306. Log.e("TAG", "exception", e);
  307. }
  308.  
  309. JSON Values: id=991, name=GPSname, address=GPSaddress, lat=52.948616, lng=-1.169131, type=GPStype
Add Comment
Please, Sign In to add comment