Advertisement
Guest User

Untitled

a guest
May 24th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. package com.example.marietopphem.groupout1;
  2.  
  3. import android.support.v4.app.FragmentActivity;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6.  
  7. /*
  8. import com.example.marietopphem.myapplicationmaps.PositionObject;
  9. import com.example.marietopphem.myapplicationmaps.R;*/
  10. import com.google.android.gms.maps.CameraUpdateFactory;
  11. import com.google.android.gms.maps.GoogleMap;
  12. import com.google.android.gms.maps.OnMapReadyCallback;
  13. import com.google.android.gms.maps.SupportMapFragment;
  14. import com.google.android.gms.maps.model.LatLng;
  15. import com.google.android.gms.maps.model.MarkerOptions;
  16.  
  17. import org.json.JSONArray;
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20.  
  21. import java.util.ArrayList;
  22.  
  23. public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
  24.  
  25. private GoogleMap mMap;
  26. ArrayList<PositionObject> javaPositions = new ArrayList<>();
  27.  
  28. double latitudeInput;
  29. double longitudeInput;
  30.  
  31. private String JSON_TEST_DATA =
  32. "[ "
  33. + " {" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.333988302200886\","
  34. + " \"Y\" : \"18.04795240339837\"" + " },"
  35. + " \"Id\" : \"35eb5586-74df-447b-b2df-ca055e3e9ec5\","
  36. + " \"Name\" : \"Kungsholms strandstigs utegym\" " + " },"
  37. + "{" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.37105799218928\","
  38. + " \"Y\" : \"18.058316113699362\"" + " },"
  39. + " \"Id\" : \"9013d8c9-b162-42ed-b256-b9d448961853\","
  40. + " \"Name\" : \"Lappkärrsbergets utegym, Docentbacken\" " + " },"
  41. + "{" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.339124550592736\","
  42. + " \"Y\" : \"18.040644423563727\"" + " },"
  43. + " \"Id\" : \"fb6ad163-513b-4962-ac3c-17c484ef2bb6\"," + " \"Name\" : \"Vasaparkens utegym\" "
  44. + " },"
  45. + "{" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.340671511858346\","
  46. + " \"Y\" : \"18.009912671835714\"" + " },"
  47. + " \"Id\" : \"90f749ae-96ab-4d73-a6f2-22c14846e095\","
  48. + " \"Name\" : \"Hornsbergs strands utegym\" " + " },"
  49. + " {" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.33234621581338\","
  50. + " \"Y\" : \"18.036367921017284\"" + " },"
  51. + " \"Id\" : \"4e635253-5611-4421-b86b-671a17d89f5f\","
  52. + " \"Name\" : \"Kronobergsparkens utegym\" " + " },"
  53. + "{" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.3258078912206\","
  54. + " \"Y\" : \"18.02116198619644\"" + " },"
  55. + " \"Id\" : \"02a9113d-b542-40fc-a6fa-a0f3a636adff\","
  56. + " \"Name\" : \"Smedsuddsbadets utegym\" " + " } , "
  57.  
  58.  
  59.  
  60.  
  61.  
  62. + "{" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.357626218928615\","
  63. + " \"Y\" : \"18.09991851664943\"" + " },"
  64. + " \"Id\" : \"55a72c6f-ed00-472a-bf0f-cfa0a6a8fc44\"," + " \"Name\" : \"Hjorthagens utegym\" "
  65. + " },"
  66. + "{" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.329230551407775\","
  67. + " \"Y\" : \"18.02530357000892\"" + " },"
  68. + " \"Id\" : \"f3880539-4c02-47bf-855c-dd79f11d4404\","
  69. + " \"Name\" : \"Rålambshovsparkens utegym\" " + " },"
  70. + " {" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.34630803139627\","
  71. + " \"Y\" : \"18.053722739958612\"" + " },"
  72. + " \"Id\" : \"3c1ad3a1-5227-410e-8d4b-e80f0b64480f\","
  73. + " \"Name\" : \"Vanadislundens utegym\" " + " },"
  74. + "{" + " \"GeographicalPosition\" : {" + " \"X\" : \"59.344186775120285\","
  75. + " \"Y\" : \"18.099205258013004\"" + " },"
  76. + " \"Id\" : \"8166652f-f79e-4fe7-a757-d0e6485b7bf9\","
  77. + " \"Name\" : \"Uteträffen i Tessinparken, utegym för seniorer\" " + " }"
  78. + " ]"
  79. ;
  80.  
  81.  
  82. public void parseJSON() throws JSONException{
  83. final JSONArray positions = new JSONArray(JSON_TEST_DATA);
  84.  
  85.  
  86.  
  87. final int n = positions.length();
  88.  
  89. for (int i = 0; i < n; i++) {
  90.  
  91. final JSONObject position = positions.getJSONObject(i);
  92.  
  93. String name = position.getString("Name");
  94. String id = position.getString("Id");
  95. double x = position.getJSONObject("GeographicalPosition").getDouble("X");
  96. double y = position.getJSONObject("GeographicalPosition").getDouble("Y");
  97.  
  98. PositionObject posObject = new PositionObject(name, id, x, y);
  99. javaPositions.add(posObject);
  100.  
  101. }
  102. Log.v("PrintArrayList", javaPositions.toString());
  103. }
  104.  
  105.  
  106. public void markerIterator(ArrayList<PositionObject> javaPositions){
  107. for(PositionObject pos: javaPositions){
  108. double x = pos.getLatitude();
  109. double y = pos.getLongitude();
  110.  
  111. LatLng newMark = new LatLng(x,y);
  112. mMap.addMarker(new MarkerOptions().position(newMark));
  113. }
  114. }
  115.  
  116.  
  117.  
  118. @Override
  119. protected void onCreate(Bundle savedInstanceState) {
  120. super.onCreate(savedInstanceState);
  121. setContentView(R.layout.t3map);
  122. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  123. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  124. .findFragmentById(R.id.mapDiv);
  125. mapFragment.getMapAsync(this);
  126. }
  127.  
  128. public void decidePosition(){
  129.  
  130. latitudeInput = 59.334591;
  131. longitudeInput = 18.063240;
  132. }
  133.  
  134. /**
  135. * Manipulates the map once available.
  136. * This callback is triggered when the map is ready to be used.
  137. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  138. * we just add a marker near Sydney, Australia.
  139. * If Google Play services is not installed on the device, the user will be prompted to install
  140. * it inside the SupportMapFragment. This method will only be triggered once the user has
  141. * installed Google Play services and returned to the app.
  142. */
  143. @Override
  144. public void onMapReady(GoogleMap googleMap) {
  145. mMap = googleMap;
  146. decidePosition();
  147. Log.v("YOLO", JSON_TEST_DATA);
  148. try {
  149. parseJSON();
  150. } catch (JSONException e) {
  151. e.printStackTrace();
  152. }
  153. // Add a marker in Stockholm and move the camera
  154. markerIterator(javaPositions);
  155. LatLng stockholm = new LatLng(latitudeInput, longitudeInput);
  156. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(stockholm, 12.0f));
  157.  
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement