RealTimeBusNavigation_5.0/app/src/main/java/com/example/saurabh/realtimebusnavigation/MapsActivity.java
@Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                /*map =(HashMap) dataSnapshot.getValue();
                Iterator iter = (Iterator) map.keySet().iterator();
                stops_name = new ArrayList<String>();
                //stops = new ArrayList<LatLng>();*/
                ArrayList<String> retrival = (ArrayList<String>)dataSnapshot.getValue();
                Log.d("JSON Parser",retrival.get(0).toString() );
                //for(Object key : map.keySet())
                for(int key=0; key< retrival.size(); key++) {
                    String coordinade = (String) retrival.get(key);
                    double lat = parseFloat(coordinade.split(",")[0].split(" ")[0]);
                    double lng = parseFloat(coordinade.split(",")[0].split(" ")[1]);
                    String bus_stop = coordinade.split(",")[1];

                    stops_name.add(bus_stop);
                    stops.add(new LatLng(lat, lng));
                }
                    mMap.addMarker(new MarkerOptions().position(stops.get(stops.size()-1)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
                    for (int i = stops.size()-2; i>=0 ; i--) {
                        mMap.addMarker(new MarkerOptions().position(stops.get(i)));
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(stops.get(i), 16));
                    }
                    //Plot start
                    for (int i = 0; i < stops.size() - 1; i++) {

                        int SDK_INT = android.os.Build.VERSION.SDK_INT;
                        if (SDK_INT > 8) {
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                    .permitAll().build();
                            StrictMode.setThreadPolicy(policy);

                        }

                        String origin = stops.get(0).latitude + "," + stops.get(0).longitude;
                        String destination = stops.get(stops.size()-1).latitude + "," + stops.get(stops.size()-1).longitude;
                        String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&key=AIzaSyBC5mZ8i142fKpuGMGTHvs95MkpZ3kBPic\n";

                        URL Url = null;
                        HttpURLConnection urlConnection = null;


                        try {
                            Url = new URL(url);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }
                        try {
                            urlConnection = (HttpURLConnection) Url.openConnection();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }


                        try {
                            is = new BufferedInputStream(urlConnection.getInputStream());
                            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                            StringBuilder total = new StringBuilder(is.available());
                            String line;
                            while ((line = reader.readLine()) != null) {
                                total.append(line).append('\n');
                            }
                            output = total.toString();
                        } catch (IOException e) {
                            Log.e("JSON Parser", "IO error " + e.toString());

                        } finally {
                            urlConnection.disconnect();
                        }

                        try {
                            json = new JSONObject(output);
                        } catch (JSONException e) {
                            Log.e("JSON Parser", "Error parsing data " + e.toString());
                        }


                        List<Route> routes = new ArrayList<>();
                        JSONObject jsonData = json;
                        JSONArray jsonRoutes = null;


                        try {
                            jsonRoutes = jsonData.getJSONArray("routes");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }

                        for (int j = 0; j < jsonRoutes.length(); j++) {

                            JSONObject jsonRoute = null;
                            try {
                                jsonRoute = jsonRoutes.getJSONObject(j);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            Route route = new Route();


                            JSONObject overview_polylineJson = null;
                            try {
                                overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            try {
                                route.points = decodePolyLine(overview_polylineJson.getString("points"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            routes.add(route);
                        }

                        plot(routes);
                        getLocation();


                    }//Till plot
                //}

            }
//Retrival of bus_stop name and coordinate and plot on map( Promises problem occured ) and initiate get_current_coordinate of bus request.
Retrival of bus_stop name and coordinate and plot on map( Promises problem occured ) and initiate get_current_coordinate of bus request.
Retrival of bus_stop name and coordinate and plot on map ( Promises problem occured ) and initiate get_current_coordinate of bus request .
@Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                /*map =(HashMap) dataSnapshot.getValue();
                Iterator iter = (Iterator) map.keySet().iterator();
                stops_name = new ArrayList<String>();
                //stops = new ArrayList<LatLng>();*/
                ArrayList<String> retrival = (ArrayList<String>)dataSnapshot.getValue();
                Log.d("JSON Parser",retrival.get(0).toString() );
                for(int key=0; key< retrival.size(); key++) {
                    String coordinade = (String) retrival.get(key);
                    double lat = parseFloat(coordinade.split(",")[0].split(" ")[0]);
                    double lng = parseFloat(coordinade.split(",")[0].split(" ")[1]);
                    String bus_stop = coordinade.split(",")[1];
                    stops_name.add(bus_stop);
                    stops.add(new LatLng(lat, lng));
                }
                    mMap.addMarker(new MarkerOptions().position(stops.get(stops.size()-1)).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
                    for (int i = stops.size()-2; i>=0 ; i--) {
                        mMap.addMarker(new MarkerOptions().position(stops.get(i)));
                        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(stops.get(i), 16));
                    }
                    for (int i = 0; i < stops.size() - 1; i++) {
                        int SDK_INT = android.os.Build.VERSION.SDK_INT;
                        if (SDK_INT > 8) {
                            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                                    .permitAll().build();
                            StrictMode.setThreadPolicy(policy);
                        }
                        String origin = stops.get(0).latitude + "," + stops.get(0).longitude;
                        String destination = stops.get(stops.size()-1).latitude + "," + stops.get(stops.size()-1).longitude;
                        String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&key=AIzaSyBC5mZ8i142fKpuGMGTHvs95MkpZ3kBPic\n";
                        URL Url = null;
                        HttpURLConnection urlConnection = null;
                        try {
                            Url = new URL(url);
                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        }
                        try {
                            urlConnection = (HttpURLConnection) Url.openConnection();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        try {
                            is = new BufferedInputStream(urlConnection.getInputStream());
                            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                            StringBuilder total = new StringBuilder(is.available());
                            String line;
                            while ((line = reader.readLine()) != null) {
                                total.append(line).append('\n');
                            }
                            output = total.toString();
                        } catch (IOException e) {
                            Log.e("JSON Parser", "IO error " + e.toString());
                        } finally {
                            urlConnection.disconnect();
                        }
                        try {
                            json = new JSONObject(output);
                        } catch (JSONException e) {
                            Log.e("JSON Parser", "Error parsing data " + e.toString());
                        }
                        List<Route> routes = new ArrayList<>();
                        JSONObject jsonData = json;
                        JSONArray jsonRoutes = null;
                        try {
                            jsonRoutes = jsonData.getJSONArray("routes");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        for (int j = 0; j < jsonRoutes.length(); j++) {
                            JSONObject jsonRoute = null;
                            try {
                                jsonRoute = jsonRoutes.getJSONObject(j);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            Route route = new Route();
                            JSONObject overview_polylineJson = null;
                            try {
                                overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            try {
                                route.points = decodePolyLine(overview_polylineJson.getString("points"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                            routes.add(route);
                        }
                        plot(routes);
                        getLocation();
                    }
            }
@ Override public void onDataChange ( DataSnapshot dataSnapshot ) { /*map =(HashMap) dataSnapshot.getValue();
                Iterator iter = (Iterator) map.keySet().iterator();
                stops_name = new ArrayList<String>();
                //stops = new ArrayList<LatLng>();*/ ArrayList < String > retrival = ( ArrayList < String > ) dataSnapshot . getValue ( ) ; Log . d ( "JSON Parser" , retrival . get ( 0 ) . toString ( ) ) ; for ( int key = 0 ; key < retrival . size ( ) ; key ++ ) { String coordinade = ( String ) retrival . get ( key ) ; double lat = parseFloat ( coordinade . split ( "," ) [ 0 ] . split ( " " ) [ 0 ] ) ; double lng = parseFloat ( coordinade . split ( "," ) [ 0 ] . split ( " " ) [ 1 ] ) ; String bus_stop = coordinade . split ( "," ) [ 1 ] ; stops_name . add ( bus_stop ) ; stops . add ( new LatLng ( lat , lng ) ) ; } mMap . addMarker ( new MarkerOptions ( ) . position ( stops . get ( stops . size ( ) - 1 ) ) . icon ( BitmapDescriptorFactory . defaultMarker ( BitmapDescriptorFactory . HUE_GREEN ) ) ) ; for ( int i = stops . size ( ) - 2 ; i >= 0 ; i -- ) { mMap . addMarker ( new MarkerOptions ( ) . position ( stops . get ( i ) ) ) ; mMap . moveCamera ( CameraUpdateFactory . newLatLngZoom ( stops . get ( i ) , 16 ) ) ; } for ( int i = 0 ; i < stops . size ( ) - 1 ; i ++ ) { int SDK_INT = android . os . Build . VERSION . SDK_INT ; if ( SDK_INT > 8 ) { StrictMode . ThreadPolicy policy = new StrictMode . ThreadPolicy . Builder ( ) . permitAll ( ) . build ( ) ; StrictMode . setThreadPolicy ( policy ) ; } String origin = stops . get ( 0 ) . latitude + "," + stops . get ( 0 ) . longitude ; String destination = stops . get ( stops . size ( ) - 1 ) . latitude + "," + stops . get ( stops . size ( ) - 1 ) . longitude ; String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&key=AIzaSyBC5mZ8i142fKpuGMGTHvs95MkpZ3kBPic\n" ; URL Url = null ; HttpURLConnection urlConnection = null ; try { Url = new URL ( url ) ; } catch ( MalformedURLException e ) { e . printStackTrace ( ) ; } try { urlConnection = ( HttpURLConnection ) Url . openConnection ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } try { is = new BufferedInputStream ( urlConnection . getInputStream ( ) ) ; BufferedReader reader = new BufferedReader ( new InputStreamReader ( is ) ) ; StringBuilder total = new StringBuilder ( is . available ( ) ) ; String line ; while ( ( line = reader . readLine ( ) ) != null ) { total . append ( line ) . append ( '\n' ) ; } output = total . toString ( ) ; } catch ( IOException e ) { Log . e ( "JSON Parser" , "IO error " + e . toString ( ) ) ; } finally { urlConnection . disconnect ( ) ; } try { json = new JSONObject ( output ) ; } catch ( JSONException e ) { Log . e ( "JSON Parser" , "Error parsing data " + e . toString ( ) ) ; } List < Route > routes = new ArrayList < > ( ) ; JSONObject jsonData = json ; JSONArray jsonRoutes = null ; try { jsonRoutes = jsonData . getJSONArray ( "routes" ) ; } catch ( JSONException e ) { e . printStackTrace ( ) ; } for ( int j = 0 ; j < jsonRoutes . length ( ) ; j ++ ) { JSONObject jsonRoute = null ; try { jsonRoute = jsonRoutes . getJSONObject ( j ) ; } catch ( JSONException e ) { e . printStackTrace ( ) ; } Route route = new Route ( ) ; JSONObject overview_polylineJson = null ; try { overview_polylineJson = jsonRoute . getJSONObject ( "overview_polyline" ) ; } catch ( JSONException e ) { e . printStackTrace ( ) ; } try { route . points = decodePolyLine ( overview_polylineJson . getString ( "points" ) ) ; } catch ( JSONException e ) { e . printStackTrace ( ) ; } routes . add ( route ) ; } plot ( routes ) ; getLocation ( ) ; } }
Retrival of bus_stop name and coordinate and plot on map( Promises problem occured ) and initiate get_current_coordinate of bus request.
Retrival of bus_stop name and coordinate and plot on map ( Promises problem occured ) and initiate get_current_coordinate of bus request .
//for(Object key : map.keySet()) //Plot start //Till plot //}