title: How to update marker positions with data from Firebase Google Maps API Android
body: <p>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. </p>
  
  <p>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.</p>
  
  <p>I tried it saving markers in SharedPreferences using Gson, but when I pass the marker to json, the app crashes.</p>
  
  <p>Does anyone know how can I update the markers?</p>
  
  <p>This is written inside the onDataChange():</p>
  
  <pre><code>           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();
  
              }
  </code></pre>
  
  <p>Thank you.</p>