I am fetching data from Firebase
and after fetching the supposed data, I stored it on an ArrayList
. So here is the sample code.
markerArray = new ArrayList<>();
Firebase ref = new Firebase(Config.FIREBASE_URL_DRIVER);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.getChildrenCount() == 0) {
markerInfo();
} else {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
name = snapshot.child("driversName").getValue().toString().trim();
busNum = snapshot.child("busNum").getValue().toString().trim();
latitude = Double.valueOf(snapshot.child("latitude").getValue().toString().trim());
longitude = Double.valueOf(snapshot.child("longitude").getValue().toString().trim());
availableSeat = snapshot.child("availableSeat").getValue().toString().trim();
estimatedTime = snapshot.child("estimatedTime").getValue().toString().trim();
if ((!latitude.equals(null) || latitude.equals(0)) && (!longitude.equals(null) || longitude.equals(0)) && availableSeat.equals("") && (!estimatedTime.equals("") || estimatedTime.equals("0"))) {
convertLatLong();
getTotalPass();
Toast.makeText(MainMapActivity.this, currentLocation+" :currentLocation", Toast.LENGTH_SHORT).show();
markerArray.add(new Driver(name, totalPassenger, busNum, latitude, longitude, currentLocation, estimatedTime));
}
}
Toast.makeText(MainMapActivity.this, "markerArraySize: "+markerArray.size(), Toast.LENGTH_SHORT).show();
for (i = 0; i < markerArray.size(); i++) {
createMarker(markerArray.get(i).getDriversName(), markerArray.get(i).getTotalPassenger(), markerArray.get(i).getBusNum(), markerArray.get(i).getLatitude(), markerArray.get(i).getLongitude(), markerArray .get(i).getLocation(), markerArray.get(i).getEstimatedTime());
}
}
}
}
I want to update my ArrayList
so that whenever there are changes on the Firebase (especially the latitude and longitude) being fetched, the markers will not be redundant/multiply.
So what did I understand from your code is that, you want to uniquely identify the changes of the location of each driver you have there in your application.
So yes, keeping a HashMap
is a nice idea as you won't have to be worried about the unique key that you're using to identify the location change of a specific driver.
But hence, I would like to suggest to keep the data fetch from your Firebase in a local sqlite database file and each time it gets updated, you can re-analyze your data to create the markers again (i.e. refreshing the markers).
For example, you can have a three tables.
driver_id
)trip_id
and each row in the Trip table is associated with a driver_id
)The tables may look like
Drivers table
driver_id - 1
name - Driver 1
driver_id - 2
name - Driver 2
Trips table
trip_id - 1
driver_id - 1
trip_id - 2
driver_id - 1
trip_id - 3
driver_id - 2
Rides table
trip_id - 1
driver_id - 1
longitude - 9.4
latitude - 38
updateTime - 78783232
trip_id - 1
driver_id - 1
longitude - 5.8
latitude - 53
updateTime - 78783242
trip_id - 1
driver_id - 1
longitude - 78
latitude - 56
updateTime - 78783252
trip_id - 2
driver_id - 1
longitude - 8
latitude - 77
updateTime - 78783262
Now from the Rides table you re-analyze your data each time Firebase updates the local database and redraw the markers for each driver in the map.
You can have simple sql queries to make a path the driver has travelled based on the updateTime
. Sort the path points based on updateTime
and remove any consecutive data which have the same longitude and latitude.
Hope that helps.
using set(position,data) method of arraylist to consume your demand.
ArrayList<String> alstr = new ArrayList<>();
alstr.add("khan");
alstr.add("yogesh");
alstr.add("kapil");
alstr.add("rajoria");
for(String str : alstr) {
System.out.println(str);
}
// update value here
alstr.set(3, "Ramveer");
System.out.println("with Iterator");
Iterator<String> itr = alstr.iterator();
while (itr.hasNext()) {
Object obj = itr.next();
System.out.println(obj);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With