I'm creating an app that makes a real time tracking of users using Firebase Database. Every user is displayed in the map using markers.
When there is a new location update, the marker has to be updated. The problem is that with Firebase method onDataChange() or similars every time a new location update is retrieved, I can't access to the old marker to remove it and create a new one or just update the old marker, because the marker doesn't exist.
I tried it saving markers in SharedPreferences using Gson, but when I pass the marker to json, the app crashes.
Does anyone know how can I update the markers?
This is written inside the onDataChange():
for (User user: usersList) {
Gson gson = new Gson();
/*
String previousJson = preferences.getString(user.getId()+"-marker", "");
Marker previousMarker = gson.fromJson(previousJson, Marker.class);
if (previousMarker!=null) markerAnterior.remove();
*/
final LatLng latlng = new LatLng(user.getLastLocation().getLatitude(), user.getLastLocation().getLongitude());
MarkerOptions markerOptions = new MarkerOptions()
.position(latlng)
.title(user.getId());
Marker marker= mMap.addMarker(markerOptions);
// String newJson = gson.toJson(marker); //CRASH
// editor.putString(user.getId()+"-marker", newJson);
// editor.commit();
}
Thank you.