Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private GoogleMap mMap;
- List<Position> positionsList = new ArrayList<>();
- ProgressDialog progressDialog;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_maps);
- // Obtain the SupportMapFragment and get notified when the map is ready to be used.
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- //delete if static
- PositionsTask positionsTask = new PositionsTask();
- positionsTask.execute();
- // end delete
- // positionsList.add(new Position("beb souika",1,1));
- }
- /**
- * Manipulates the map once available.
- * This callback is triggered when the map is ready to be used.
- * This is where we can add markers or lines, add listeners or move the camera. In this case,
- * we just add a marker near Sydney, Australia.
- * If Google Play services is not installed on the device, the user will be prompted to install
- * it inside the SupportMapFragment. This method will only be triggered once the user has
- * installed Google Play services and returned to the app.
- */
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- LatLng TUNIS = new LatLng(36.818248, 10.182416);
- for(Position pos: positionsList){
- LatLng latLng = new LatLng(pos.getLatitude(),pos.getLongitude());
- if(mMap!=null){
- mMap.addMarker(new MarkerOptions().position(latLng).title(pos.getName()));
- }
- }
- mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(TUNIS,11));
- }
- private class PositionsTask extends AsyncTask<Void, Void, Void> {
- private static final String TAG_ID = "id";
- private static final String TAG_NAME = "name";
- private static final String TAG_LATITUDE = "latitude";
- private static final String TAG_lONGITUDE = "longitude";
- @Override
- protected void onPreExecute(){
- progressDialog = new ProgressDialog(MapsActivity.this);
- progressDialog.setMessage("Chargement en cours ...");
- progressDialog.show();
- }
- @Override
- protected Void doInBackground(Void... params) {
- String link;
- BufferedReader bufferedReader;
- String result="[]";
- try {
- link = "http://192.168.8.105/map1.php";
- URL url = new URL(link);
- HttpURLConnection con = (HttpURLConnection) url.openConnection();
- bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
- String line = null;
- StringBuilder sb = new StringBuilder() ;
- while ((line = bufferedReader.readLine()) != null)
- {
- sb.append(line + "n");
- }
- result = sb.toString();
- } catch (Exception e) {
- Log.e("Exception: ",e.getMessage());
- }
- positionsList = getPositionList(result);
- return null;
- }
- @Override
- protected void onPostExecute(Void params) {
- progressDialog.dismiss();
- }
- @Override
- protected void onCancelled() {
- progressDialog.dismiss();
- }
- protected List getPositionList(String myJSON){
- List<Position> positions = new ArrayList<>();
- try {
- JSONArray data = new JSONArray(myJSON);
- for(int i=0;i<data.length();i++){
- Position position = new Position();
- JSONObject json = data.getJSONObject(i);
- position.setId(json.getInt(TAG_ID));
- position.setName(json.getString(TAG_NAME));
- position.setLatitude(json.getDouble(TAG_LATITUDE));
- position.setLongitude(json.getDouble(TAG_lONGITUDE));
- positions.add(position);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return positions;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement