Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. private GoogleMap mMap;
  2. List<Position> positionsList = new ArrayList<>();
  3. ProgressDialog progressDialog;
  4.  
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_maps);
  9. // Obtain the SupportMapFragment and get notified when the map is ready to be used.
  10. SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
  11. .findFragmentById(R.id.map);
  12. mapFragment.getMapAsync(this);
  13.  
  14. //delete if static
  15. PositionsTask positionsTask = new PositionsTask();
  16. positionsTask.execute();
  17. // end delete
  18.  
  19. // positionsList.add(new Position("beb souika",1,1));
  20. }
  21.  
  22.  
  23. /**
  24. * Manipulates the map once available.
  25. * This callback is triggered when the map is ready to be used.
  26. * This is where we can add markers or lines, add listeners or move the camera. In this case,
  27. * we just add a marker near Sydney, Australia.
  28. * If Google Play services is not installed on the device, the user will be prompted to install
  29. * it inside the SupportMapFragment. This method will only be triggered once the user has
  30. * installed Google Play services and returned to the app.
  31. */
  32. @Override
  33. public void onMapReady(GoogleMap googleMap) {
  34. mMap = googleMap;
  35. LatLng TUNIS = new LatLng(36.818248, 10.182416);
  36.  
  37. for(Position pos: positionsList){
  38. LatLng latLng = new LatLng(pos.getLatitude(),pos.getLongitude());
  39. if(mMap!=null){
  40. mMap.addMarker(new MarkerOptions().position(latLng).title(pos.getName()));
  41. }
  42. }
  43. mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(TUNIS,11));
  44. }
  45.  
  46. private class PositionsTask extends AsyncTask<Void, Void, Void> {
  47.  
  48.  
  49. private static final String TAG_ID = "id";
  50. private static final String TAG_NAME = "name";
  51. private static final String TAG_LATITUDE = "latitude";
  52. private static final String TAG_lONGITUDE = "longitude";
  53.  
  54.  
  55. @Override
  56. protected void onPreExecute(){
  57. progressDialog = new ProgressDialog(MapsActivity.this);
  58. progressDialog.setMessage("Chargement en cours ...");
  59. progressDialog.show();
  60. }
  61. @Override
  62. protected Void doInBackground(Void... params) {
  63.  
  64. String link;
  65. BufferedReader bufferedReader;
  66. String result="[]";
  67.  
  68. try {
  69.  
  70.  
  71. link = "http://192.168.8.105/map1.php";
  72. URL url = new URL(link);
  73. HttpURLConnection con = (HttpURLConnection) url.openConnection();
  74.  
  75. bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
  76. String line = null;
  77. StringBuilder sb = new StringBuilder() ;
  78. while ((line = bufferedReader.readLine()) != null)
  79. {
  80. sb.append(line + "n");
  81. }
  82. result = sb.toString();
  83.  
  84.  
  85. } catch (Exception e) {
  86. Log.e("Exception: ",e.getMessage());
  87. }
  88. positionsList = getPositionList(result);
  89.  
  90. return null;
  91. }
  92.  
  93. @Override
  94. protected void onPostExecute(Void params) {
  95. progressDialog.dismiss();
  96. }
  97.  
  98. @Override
  99. protected void onCancelled() {
  100. progressDialog.dismiss();
  101. }
  102.  
  103. protected List getPositionList(String myJSON){
  104. List<Position> positions = new ArrayList<>();
  105. try {
  106. JSONArray data = new JSONArray(myJSON);
  107.  
  108. for(int i=0;i<data.length();i++){
  109. Position position = new Position();
  110. JSONObject json = data.getJSONObject(i);
  111. position.setId(json.getInt(TAG_ID));
  112. position.setName(json.getString(TAG_NAME));
  113. position.setLatitude(json.getDouble(TAG_LATITUDE));
  114. position.setLongitude(json.getDouble(TAG_lONGITUDE));
  115.  
  116. positions.add(position);
  117. }
  118.  
  119. } catch (JSONException e) {
  120. e.printStackTrace();
  121. }
  122. return positions;
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement