Last active
November 10, 2021 10:26
-
-
Save mitchtabian/02f3a53ac6a9dae688641a84c5fba425 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Update the GPS coordinate of a ClusterItem | |
* @param clusterMarker | |
*/ | |
public void setUpdateMarker(ClusterMarker clusterMarker) { | |
Marker marker = getMarker(clusterMarker); | |
if (marker != null) { | |
marker.setPosition(clusterMarker.getPosition()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Handler mHandler = new Handler(); | |
private Runnable mRunnable; | |
private static final int LOCATION_UPDATE_INTERVAL = 3000; | |
private void startUserLocationsRunnable(){ | |
Log.d(TAG, "startUserLocationsRunnable: starting runnable for retrieving updated locations."); | |
mHandler.postDelayed(mRunnable = new Runnable() { | |
@Override | |
public void run() { | |
retrieveUserLocations(); | |
mHandler.postDelayed(mRunnable, LOCATION_UPDATE_INTERVAL); | |
} | |
}, LOCATION_UPDATE_INTERVAL); | |
} | |
private void stopLocationUpdates(){ | |
mHandler.removeCallbacks(mRunnable); | |
} | |
private void retrieveUserLocations(){ | |
Log.d(TAG, "retrieveUserLocations: retrieving location of all users in the chatroom."); | |
try{ | |
for(final ClusterMarker clusterMarker: mClusterMarkers){ | |
DocumentReference userLocationRef = FirebaseFirestore.getInstance() | |
.collection(getString(R.string.collection_user_locations)) | |
.document(clusterMarker.getUser().getUser_id()); | |
userLocationRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() { | |
@Override | |
public void onComplete(@NonNull Task<DocumentSnapshot> task) { | |
if(task.isSuccessful()){ | |
final UserLocation updatedUserLocation = task.getResult().toObject(UserLocation.class); | |
// update the location | |
for (int i = 0; i < mClusterMarkers.size(); i++) { | |
try { | |
if (mClusterMarkers.get(i).getUser().getUser_id().equals(updatedUserLocation.getUser().getUser_id())) { | |
LatLng updatedLatLng = new LatLng( | |
updatedUserLocation.getGeo_point().getLatitude(), | |
updatedUserLocation.getGeo_point().getLongitude() | |
); | |
mClusterMarkers.get(i).setPosition(updatedLatLng); | |
} | |
} catch (NullPointerException e) { | |
Log.e(TAG, "retrieveUserLocations: NullPointerException: " + e.getMessage()); | |
} | |
} | |
} | |
} | |
}); | |
} | |
}catch (IllegalStateException e){ | |
Log.e(TAG, "retrieveUserLocations: Fragment was destroyed during Firestore query. Ending query." + e.getMessage() ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks alot