Search in sources :

Example 1 with GenericTypeIndicator

use of com.google.firebase.database.GenericTypeIndicator in project GogoNew by kuldeep725.

the class MapsActivity method showDistanceInBetween.

private void showDistanceInBetween() {
    Log.e(TAG, "showDistanceInBetween fired...");
    String BUS = "b" + checkBusSelection;
    mDatabase = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference userDatabase = mDatabase.child(USER).child(BUS).child(LOCATION);
    showInternetStatus();
    if (!isInternetOn())
        return;
    userDatabase.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String bus = "b" + checkBusSelection;
            if (!userDatabase.toString().equals(mDatabase.child(USER).child(bus).child(LOCATION).toString()))
                return;
            GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
            };
            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
            // Log.d(TAG, "Data : " + dataSnapshot.getValue());
            if (mCurrentLocation == null)
                return;
            assert map != null;
            String latitudeStr = map.get("latitude");
            String longitudeStr = map.get("longitude");
            latitudeBus = Double.parseDouble(latitudeStr);
            longitudeBus = Double.parseDouble(longitudeStr);
            // Log.d(TAG, "mCurrentLocation = "+mCurrentLocation);
            if (flagShowDistanceInBetween == 0) {
                if (mCurrentLocation != null) {
                    flagShowDistanceInBetween = 1;
                    String url = "https://maps.googleapis.com/maps/api/directions/json?origin=" + mCurrentLocation.getLatitude() + "," + mCurrentLocation.getLongitude() + "&destination=" + latitudeStr + "," + longitudeStr + // + R.string.google_direction_api_key;
                    "&key=AIzaSyChXllnUaESuRZPDpSHtb3oyXgL1edHITg";
                    /*String url = "https://maps.googleapis.com/maps/api/directions/json?origin="
                                                + "40.81649,-73.907807&destination=40.819585,-73.90177"+ "&key=AIzaSyChXllnUaESuRZPDpSHtb3oyXgL1edHITg";// + R.string.google_direction_api_key;*/
                    // Log.d(TAG, "URL : " + url);
                    Log.d(TAG, url);
                    DownloadTask downloadTask = new DownloadTask();
                    // Log.d(TAG, "@busDistance (SphericalUtil.computeDistanceBetween) = " + SphericalUtil.computeDistanceBetween(mCurrentPosition, new LatLng(Double.parseDouble(latitudeStr), Double.parseDouble(longitudeStr))));
                    // Start downloading json data from Google Directions API
                    downloadTask.execute(url);
                }
            } else {
                // double dist = SphericalUtil.computeDistanceBetween(mCurrentPosition,  new LatLng(latitudeBus, longitudeBus));
                double dist = CalculationByDistance(mCurrentPosition, new LatLng(latitudeBus, longitudeBus)) * 1000;
                // Log.d(TAG, "dist = "+ dist);
                long distLong = Math.round(dist);
                String timeStr;
                if (dist < 1000) {
                    String distStr = String.valueOf(distLong) + " m";
                    // Log.d(TAG, "distStr = " + distStr);
                    distance.setText(distStr);
                    distance.setPaintFlags(distance.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                    // long timeLong =Math.round(9 * distLong / 100);          //      40km/hr into m/s
                    // 30km/hr into m/s
                    long timeLong = Math.round(12 * distLong / 100);
                    if (timeLong > 60) {
                        timeLong = Math.round(timeLong / 60);
                        timeStr = String.valueOf(timeLong) + " min";
                    } else {
                        timeStr = String.valueOf(timeLong) + " s";
                    }
                    // Log.d(TAG, "timeStr = " +timeStr);
                    duration.setText(timeStr);
                    duration.setPaintFlags(duration.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                } else {
                    distLong = Math.round(distLong / 1000);
                    String distStr = String.valueOf(distLong + " km");
                    // Log.d(TAG, "distStr = " + distStr);
                    distance.setText(distStr);
                    distance.setPaintFlags(distance.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                    // long timeLong = Math.round(3 * distLong / 2);           //      40km/hr into km/min
                    // 30km/hr into km/min
                    long timeLong = Math.round(2 * distLong);
                    if (timeLong > 60) {
                        timeLong = Math.round(timeLong / 60);
                        timeStr = String.valueOf(timeLong) + " hr";
                    } else {
                        timeStr = String.valueOf(timeLong) + " min";
                    }
                    // Log.d(TAG, "timeStr = " +timeStr);
                    duration.setText(timeStr);
                    duration.setPaintFlags(duration.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
                }
            }
        /*LatLng bus1_location = new LatLng(latitude,  longitude);
                                LatLng myLocation = new LatLng(mCurrentLocation.getLatitude(),  mCurrentLocation.getLongitude());
                                String DIFFERENCE = CalculationByDistance(myLocation, bus1_location);
                                String dist = DIFFERENCE + " Km";*/
        // distance.setText(dist);
        /*TextView time = (TextView) findViewById(R.id.time);
                String theTime = latitudeStr + ", " +  longitudeStr;
                time.setText(theTime);*/
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
// return myClass.getLongitude();
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) GenericTypeIndicator(com.google.firebase.database.GenericTypeIndicator) ValueEventListener(com.google.firebase.database.ValueEventListener) LatLng(com.google.android.gms.maps.model.LatLng) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) HashMap(java.util.HashMap) GoogleMap(com.google.android.gms.maps.GoogleMap)

Example 2 with GenericTypeIndicator

use of com.google.firebase.database.GenericTypeIndicator in project GogoNew by kuldeep725.

the class MapsActivity method findNearestBusStop.

private void findNearestBusStop() {
    showInternetStatus();
    if (!isInternetOn())
        return;
    DatabaseReference routeDatabase = FirebaseDatabase.getInstance().getReference().child(USER).child(BUS).child("route");
    routeDatabase.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            // Log.d(TAG, "onChildAdded findNearestBusStop() fired .. .. ");
            GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
            };
            // Log.d(TAG, " dataSnapshot.getValue(genericTypeIndicator) : " + dataSnapshot.getValue(genericTypeIndicator));
            // Log.d(TAG, "postDataSnapshot.getKey() = " + dataSnapshot.getKey());
            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
            assert map != null;
            String latitudeStr = map.get("latitude");
            String longitudeStr = map.get("longitude");
            // Log.d(TAG, "Latitude = " + latitudeStr);
            // Log.d(TAG, "Longitude = " + longitudeStr);
            double latitude = Double.parseDouble(latitudeStr);
            double longitude = Double.parseDouble(longitudeStr);
            LatLng busStop = new LatLng(latitude, longitude);
            // String busName = "BUS " + checkBusSelection;
            // double diff = SphericalUtil.computeDistanceBetween(mCurrentPosition,  busStop);
            double diff = CalculationByDistance(mCurrentPosition, busStop) * 1000;
            // Log.d(TAG, "diff = " + diff);
            if (diff < minDistance) {
                minDistance = diff;
                minLocation = new LatLng(latitude, longitude);
                minName = dataSnapshot.getKey();
            }
        // Log.d(TAG, "minLocation.toString() = " + minLocation.toString());
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) GenericTypeIndicator(com.google.firebase.database.GenericTypeIndicator) LatLng(com.google.android.gms.maps.model.LatLng) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) HashMap(java.util.HashMap) GoogleMap(com.google.android.gms.maps.GoogleMap) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 3 with GenericTypeIndicator

use of com.google.firebase.database.GenericTypeIndicator in project GogoNew by kuldeep725.

the class MapsActivity method showMarkers.

private void showMarkers() {
    if (checkBusSelection != 0) {
        showInternetStatus();
        if (!isInternetOn())
            return;
        // Log.d(TAG, "showMarkers IS FIRED...");
        String BUS = "b" + checkBusSelection;
        final DatabaseReference routeDatabase = mDatabase.child(USER).child(BUS).child(ROUTE);
        routeDatabase.addChildEventListener(new ChildEventListener() {

            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String bus = "b" + checkBusSelection;
                if (!routeDatabase.toString().equals(mDatabase.child(USER).child(bus).child(ROUTE).toString()))
                    return;
                GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
                };
                // Log.d(TAG, "Data : " + dataSnapshot.getValue(genericTypeIndicator));
                Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
                assert map != null;
                String latitudeStr = map.get("latitude");
                String longitudeStr = map.get("longitude");
                // Log.d(TAG, "Latitude = " + latitudeStr);
                // Log.d(TAG, "Longitude = " + longitudeStr);
                double latitude = Double.parseDouble(latitudeStr);
                double longitude = Double.parseDouble(longitudeStr);
                String busName = "BUS " + checkBusSelection;
                // Geocoder geocoder = new Geocoder(getApplicationContext());
                // try {
                // List<android.location.Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
                // String str = "";
                // if (addressList.get(0).getSubLocality() != null) {
                // str = addressList.get(0).getSubLocality()+",";
                // }
                // str += addressList.get(0).getLocality();
                // str += addressList.get(0).getCountryName();
                String str = "";
                str += /*" (" + */
                dataSnapshot.getKey();
                LatLng latlng = new LatLng(latitude, longitude);
                Marker marker = mMap.addMarker(new MarkerOptions().position(latlng).title(str));
                marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.red_stop_10));
                markerMap.put(latlng, marker);
                markerNameList.put(latlng, str);
            // mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));
            // }
            // catch (IOException e) {
            // e.printStackTrace();
            // mMap.addMarker(new MarkerOptions()
            // .position(new LatLng(latitude, longitude))
            // .title(busName))
            // .setIcon(BitmapDescriptorFactory.fromResource(R.drawable.red_stop_10));
            // //                          mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 12.0f));
            // Log.e(TAG, "GEOCODER DIDN'T WORK.");
            // }
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) DatabaseError(com.google.firebase.database.DatabaseError) DatabaseReference(com.google.firebase.database.DatabaseReference) GenericTypeIndicator(com.google.firebase.database.GenericTypeIndicator) LatLng(com.google.android.gms.maps.model.LatLng) Marker(com.google.android.gms.maps.model.Marker) DataSnapshot(com.google.firebase.database.DataSnapshot) Map(java.util.Map) HashMap(java.util.HashMap) GoogleMap(com.google.android.gms.maps.GoogleMap) ChildEventListener(com.google.firebase.database.ChildEventListener)

Example 4 with GenericTypeIndicator

use of com.google.firebase.database.GenericTypeIndicator in project GogoNew by kuldeep725.

the class MapsActivity method makeMarkerOnTheLocation.

private void makeMarkerOnTheLocation() {
    String BUS = "b" + checkBusSelection;
    // Log.d(TAG, "CHECKBUSSELECTION = " + checkBusSelection);
    // Log.d(TAG, "BUS NOW = " + BUS);
    mDatabase = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference locationDatabase = mDatabase.child(USER).child(BUS).child(LOCATION);
    // Log.d(TAG, "USERdatabase = " + userDatabase.toString());
    showInternetStatus();
    if (!isInternetOn())
        return;
    locationDatabase.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String bus = "b" + checkBusSelection;
            if (!locationDatabase.toString().equals(mDatabase.child(USER).child(bus).child(LOCATION).toString()))
                return;
            GenericTypeIndicator<Map<String, String>> genericTypeIndicator = new GenericTypeIndicator<Map<String, String>>() {
            };
            Map<String, String> map = dataSnapshot.getValue(genericTypeIndicator);
            assert map != null;
            String latitudeStr = map.get("latitude");
            String longitudeStr = map.get("longitude");
            // Log.d(TAG, "Latitude = " + latitudeStr);
            // Log.d(TAG, "Longitude = " + longitudeStr);
            double latitude = Double.parseDouble(latitudeStr);
            double longitude = Double.parseDouble(longitudeStr);
            LatLng midLocation = null;
            if (mCurrentLocation != null) {
                double midLatitude = (latitude + mCurrentLocation.getLatitude()) / 2;
                double midLongitude = (longitude + mCurrentLocation.getLongitude()) / 2;
                midLocation = new LatLng(midLatitude, midLongitude);
            }
            String busName = "BUS " + checkBusSelection;
            Geocoder geocoder = new Geocoder(getApplicationContext());
            try {
                List<android.location.Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
                String str = "";
                if (addressList.get(0).getSubLocality() != null) {
                    str = addressList.get(0).getSubLocality() + ",";
                }
                str += addressList.get(0).getLocality();
                str += " (" + busName + ")";
                // Log.d(TAG, "str = "+str);
                if (previousSelection != -1) {
                    markerList[previousSelection - 1].remove();
                }
                markerList[checkBusSelection - 1] = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(str));
                markerList[checkBusSelection - 1].setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_directions_bus_black_18dp));
                if (previousSelection != checkBusSelection) {
                    if (midLocation != null)
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(midLocation, 11.0f));
                    else
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10.0f));
                }
                previousSelection = checkBusSelection;
            } catch (IOException e) {
                e.printStackTrace();
                if (previousSelection != -1) {
                    markerList[previousSelection - 1].remove();
                }
                markerList[checkBusSelection - 1] = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(busName));
                markerList[checkBusSelection - 1].setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ic_directions_bus_black_18dp));
                if (previousSelection != checkBusSelection) {
                    if (midLocation != null)
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(midLocation, 11.0f));
                    else
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10.0f));
                }
                Log.e(TAG, "GEOCODER DIDN'T WORK.");
                previousSelection = checkBusSelection;
            }
        // Log.d(TAG, "MARKER HAS BEEN MADE TO " + busName);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
Also used : MarkerOptions(com.google.android.gms.maps.model.MarkerOptions) DatabaseReference(com.google.firebase.database.DatabaseReference) IOException(java.io.IOException) DataSnapshot(com.google.firebase.database.DataSnapshot) Geocoder(android.location.Geocoder) DatabaseError(com.google.firebase.database.DatabaseError) GenericTypeIndicator(com.google.firebase.database.GenericTypeIndicator) ColorStateList(android.content.res.ColorStateList) List(java.util.List) ArrayList(java.util.ArrayList) ValueEventListener(com.google.firebase.database.ValueEventListener) LatLng(com.google.android.gms.maps.model.LatLng) Map(java.util.Map) HashMap(java.util.HashMap) GoogleMap(com.google.android.gms.maps.GoogleMap)

Aggregations

GoogleMap (com.google.android.gms.maps.GoogleMap)4 LatLng (com.google.android.gms.maps.model.LatLng)4 DataSnapshot (com.google.firebase.database.DataSnapshot)4 DatabaseError (com.google.firebase.database.DatabaseError)4 DatabaseReference (com.google.firebase.database.DatabaseReference)4 GenericTypeIndicator (com.google.firebase.database.GenericTypeIndicator)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MarkerOptions (com.google.android.gms.maps.model.MarkerOptions)2 ChildEventListener (com.google.firebase.database.ChildEventListener)2 ValueEventListener (com.google.firebase.database.ValueEventListener)2 ColorStateList (android.content.res.ColorStateList)1 Geocoder (android.location.Geocoder)1 Marker (com.google.android.gms.maps.model.Marker)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1