[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:
@@ -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.Marker;
|
||||
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.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.auth.FirebaseUser;
|
||||
@@ -144,11 +145,23 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
private ActiveShareAdapter mActiveShareAdapter;
|
||||
|
||||
/**
|
||||
* Flag indicating whether currently the user wants to be following another user or not.
|
||||
*/
|
||||
private boolean mIsFollowing;
|
||||
|
||||
@Override
|
||||
protected void onCreate(final Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -165,10 +178,12 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
|
||||
mInfoBoxTimeStamp = findViewById(R.id.share_location_info_timestamp);
|
||||
mNoActiveShares = findViewById(R.id.tv_no_active_shares);
|
||||
mActiveShares = findViewById(R.id.active_share_layout);
|
||||
mRecenterFollowBtn = findViewById(R.id.fab_recenter_on_follower);
|
||||
|
||||
mActiveShares.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
|
||||
mActiveShareAdapter = new ActiveShareAdapter(this);
|
||||
|
||||
mRecenterFollowBtn.setOnClickListener(v -> startFollowingAndCenterCamera());
|
||||
mActiveShares.setAdapter(mActiveShareAdapter);
|
||||
mapFragment.getMapAsync(this);
|
||||
mDbHelper = new DatabaseHelper();
|
||||
@@ -270,6 +285,11 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
|
||||
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) {
|
||||
final List<User> activeShares = new ArrayList<>();
|
||||
for (final String shareId : locationObjectMap.keySet()) {
|
||||
@@ -298,6 +318,8 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
|
||||
if (TextUtils.equals(removeKey, mCurrentlyFollowing)) {
|
||||
clearInfoBox();
|
||||
mCurrentlyFollowing = null;
|
||||
mIsFollowing = false;
|
||||
mCurrentlyFollowingLocation = null;
|
||||
}
|
||||
mMarkerMap.remove(removeKey);
|
||||
}
|
||||
@@ -315,14 +337,6 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
|
||||
final LatLng receivedLocation = new LatLng(newReceived.getLatitude(), newReceived.getLongitude());
|
||||
final String sharingId = locationSet.getKey();
|
||||
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)) {
|
||||
final Marker existing = mMarkerMap.get(sharingId);
|
||||
if (existing == null) {
|
||||
@@ -339,6 +353,14 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
|
||||
.title(locationUser == null ? "" : locationUser.getDisplayName()));
|
||||
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) {
|
||||
Log.d(TAG, "onCreate: Currently selected = [" + shareId + "]");
|
||||
mCurrentlyFollowing = shareId;
|
||||
mIsFollowing = !TextUtils.isEmpty(mCurrentlyFollowing);
|
||||
// Re-setup the subscription.
|
||||
if (mLocationUpdateSubscription != null) {
|
||||
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");
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
public void onMapReady(@NonNull final GoogleMap googleMap) {
|
||||
mGmap = googleMap;
|
||||
@@ -456,6 +491,14 @@ public class ShareLocationActivity extends AppCompatActivity implements OnMapRea
|
||||
} else {
|
||||
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));
|
||||
}
|
||||
}
|
||||
@@ -31,6 +31,17 @@
|
||||
|
||||
</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
|
||||
android:id="@+id/map"
|
||||
android:name="com.google.android.gms.maps.SupportMapFragment"
|
||||
|
||||
Reference in New Issue
Block a user