Skip to content

Instantly share code, notes, and snippets.

@mitchtabian
Last active November 10, 2021 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mitchtabian/02f3a53ac6a9dae688641a84c5fba425 to your computer and use it in GitHub Desktop.
Save mitchtabian/02f3a53ac6a9dae688641a84c5fba425 to your computer and use it in GitHub Desktop.
/**
* 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());
}
}
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() );
}
}
@m07med176
Copy link

thanks alot

@NacirGithub
Copy link

NacirGithub commented Nov 10, 2021

THANK YOU SO MUCH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment