[ShareLocation] Added Re-Center logic

If currently following another user, and the map gets moved,
enable a recenter button to update the location to the currently
selected location of the user.
This commit is contained in:
Alexander Dörflinger
2025-04-01 12:32:20 +02:00
parent c8488ab55e
commit be6b5284df
2 changed files with 62 additions and 8 deletions

View File

@@ -35,6 +35,7 @@ import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions; import com.google.android.gms.maps.model.MapStyleOptions;
import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.materialswitch.MaterialSwitch; import com.google.android.material.materialswitch.MaterialSwitch;
import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser; import com.google.firebase.auth.FirebaseUser;
@@ -144,11 +145,23 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
*/ */
private RecyclerView mActiveShares; private RecyclerView mActiveShares;
/**
* The {@link FloatingActionButton} to recenter the camera on the followed location.
*/
private FloatingActionButton mRecenterFollowBtn;
private LocationObject mCurrentlyFollowingLocation;
/** /**
* The {@link ActiveShareAdapter} to populate the activeShare view. * The {@link ActiveShareAdapter} to populate the activeShare view.
*/ */
private ActiveShareAdapter mActiveShareAdapter; private ActiveShareAdapter mActiveShareAdapter;
/**
* Flag indicating whether currently the user wants to be following another user or not.
*/
private boolean mIsFollowing;
@Override @Override
protected void onCreate(final Bundle savedInstanceState) { protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -165,10 +178,12 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
mInfoBoxTimeStamp = findViewById(R.id.share_location_info_timestamp); mInfoBoxTimeStamp = findViewById(R.id.share_location_info_timestamp);
mNoActiveShares = findViewById(R.id.tv_no_active_shares); mNoActiveShares = findViewById(R.id.tv_no_active_shares);
mActiveShares = findViewById(R.id.active_share_layout); mActiveShares = findViewById(R.id.active_share_layout);
mRecenterFollowBtn = findViewById(R.id.fab_recenter_on_follower);
mActiveShares.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)); mActiveShares.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
mActiveShareAdapter = new ActiveShareAdapter(this); mActiveShareAdapter = new ActiveShareAdapter(this);
mRecenterFollowBtn.setOnClickListener(v -> startFollowingAndCenterCamera());
mActiveShares.setAdapter(mActiveShareAdapter); mActiveShares.setAdapter(mActiveShareAdapter);
mapFragment.getMapAsync(this); mapFragment.getMapAsync(this);
mDbHelper = new DatabaseHelper(); mDbHelper = new DatabaseHelper();
@@ -270,6 +285,11 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
updateExistingOrCreateValidMarkers(locationObjectMap); updateExistingOrCreateValidMarkers(locationObjectMap);
} }
/**
* Helper method to update the active share fragment with all available sharing users.
*
* @param locationObjectMap The {@link Map} of ids and {@link LocationObject}s.
*/
private void updateActiveShares(final Map<String, LocationObject> locationObjectMap) { private void updateActiveShares(final Map<String, LocationObject> locationObjectMap) {
final List<User> activeShares = new ArrayList<>(); final List<User> activeShares = new ArrayList<>();
for (final String shareId : locationObjectMap.keySet()) { for (final String shareId : locationObjectMap.keySet()) {
@@ -298,6 +318,8 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
if (TextUtils.equals(removeKey, mCurrentlyFollowing)) { if (TextUtils.equals(removeKey, mCurrentlyFollowing)) {
clearInfoBox(); clearInfoBox();
mCurrentlyFollowing = null; mCurrentlyFollowing = null;
mIsFollowing = false;
mCurrentlyFollowingLocation = null;
} }
mMarkerMap.remove(removeKey); mMarkerMap.remove(removeKey);
} }
@@ -315,14 +337,6 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
final LatLng receivedLocation = new LatLng(newReceived.getLatitude(), newReceived.getLongitude()); final LatLng receivedLocation = new LatLng(newReceived.getLatitude(), newReceived.getLongitude());
final String sharingId = locationSet.getKey(); final String sharingId = locationSet.getKey();
final User locationUser = mDbHelper.getUserForId(sharingId); final User locationUser = mDbHelper.getUserForId(sharingId);
if (TextUtils.equals(mCurrentlyFollowing, sharingId)) {
final LocationObject locationObject = locationSet.getValue();
updateInfoBox(locationObject);
mGmap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(locationObject.getLatitude(), locationObject.getLongitude()),
15.0f));
}
if (mMarkerMap.containsKey(sharingId)) { if (mMarkerMap.containsKey(sharingId)) {
final Marker existing = mMarkerMap.get(sharingId); final Marker existing = mMarkerMap.get(sharingId);
if (existing == null) { if (existing == null) {
@@ -339,6 +353,14 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
.title(locationUser == null ? "" : locationUser.getDisplayName())); .title(locationUser == null ? "" : locationUser.getDisplayName()));
mMarkerMap.put(locationSet.getKey(), marker); mMarkerMap.put(locationSet.getKey(), marker);
} }
if (TextUtils.equals(mCurrentlyFollowing, sharingId)) {
final LocationObject locationObject = locationSet.getValue();
mCurrentlyFollowingLocation = locationObject;
updateInfoBox(locationObject);
if (mIsFollowing) {
startFollowingAndCenterCamera();
}
}
} }
} }
@@ -422,6 +444,7 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
private void handleCurrentlyFollowingChanged(final String shareId) { private void handleCurrentlyFollowingChanged(final String shareId) {
Log.d(TAG, "onCreate: Currently selected = [" + shareId + "]"); Log.d(TAG, "onCreate: Currently selected = [" + shareId + "]");
mCurrentlyFollowing = shareId; mCurrentlyFollowing = shareId;
mIsFollowing = !TextUtils.isEmpty(mCurrentlyFollowing);
// Re-setup the subscription. // Re-setup the subscription.
if (mLocationUpdateSubscription != null) { if (mLocationUpdateSubscription != null) {
mLocationUpdateSubscription.dispose(); mLocationUpdateSubscription.dispose();
@@ -439,6 +462,18 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
Log.e(TAG, "handleUserSubscriptionFailed: Subscription to all users failed, keep cached state"); Log.e(TAG, "handleUserSubscriptionFailed: Subscription to all users failed, keep cached state");
} }
/**
* Helper method to start following and recenter the camera on the location followed.
*/
private void startFollowingAndCenterCamera() {
mIsFollowing = true;
mGmap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mCurrentlyFollowingLocation.getLatitude(),
mCurrentlyFollowingLocation.getLongitude()),
15.0f));
mRecenterFollowBtn.setVisibility(View.GONE);
}
@Override @Override
public void onMapReady(@NonNull final GoogleMap googleMap) { public void onMapReady(@NonNull final GoogleMap googleMap) {
mGmap = googleMap; mGmap = googleMap;
@@ -456,6 +491,14 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
} else { } else {
mGmapsMarker.setPosition(defaultHome); mGmapsMarker.setPosition(defaultHome);
} }
mGmap.setOnCameraMoveStartedListener(reason -> {
if (GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE == reason
&& !TextUtils.isEmpty(mCurrentlyFollowing)) {
Log.d(TAG, "onCameraMoveStarted: User moved map, set following to false.");
mIsFollowing = false;
mRecenterFollowBtn.setVisibility(View.VISIBLE);
}
});
mGmap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultHome, 8.0f)); mGmap.moveCamera(CameraUpdateFactory.newLatLngZoom(defaultHome, 8.0f));
} }
} }

View File

@@ -31,6 +31,17 @@
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_recenter_on_follower"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_compass"
app:layout_constraintBottom_toBottomOf="@id/map"
app:layout_constraintEnd_toEndOf="@id/map"
android:layout_marginBottom="25dp"
android:layout_marginEnd="25dp"
android:visibility="gone"/>
<androidx.fragment.app.FragmentContainerView <androidx.fragment.app.FragmentContainerView
android:id="@+id/map" android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment" android:name="com.google.android.gms.maps.SupportMapFragment"