Skip to content

Commit e7efd7e

Browse files
Fix auto zoom issue commons-app#3391 Zoom level gets reset at every second. (commons-app#3564)
* Add method to check if curr location marker is vsible or not * Recenter map if users see their current location marker * Add new methods to Contract
1 parent 350e95b commit e7efd7e

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/contract/NearbyParentFragmentContract.java

+3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ interface View {
7171
LatLng getLastLocation();
7272

7373
com.mapbox.mapboxsdk.geometry.LatLng getLastFocusLocation();
74+
75+
boolean isCurrentLocationMarkerVisible();
76+
void setProjectorLatLngBounds();
7477
}
7578

7679
interface NearbyListView {

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java

+20-11
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.mapbox.mapboxsdk.camera.CameraPosition;
5151
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
5252
import com.mapbox.mapboxsdk.geometry.LatLng;
53+
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
5354
import com.mapbox.mapboxsdk.maps.MapView;
5455
import com.mapbox.mapboxsdk.maps.MapboxMap;
5556
import com.mapbox.mapboxsdk.maps.Style;
@@ -201,6 +202,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
201202
private boolean isVisibleToUser;
202203
private MapboxMap.OnCameraMoveListener cameraMoveListener;
203204
private fr.free.nrw.commons.location.LatLng lastFocusLocation;
205+
private LatLngBounds latLngBounds;
204206

205207

206208
@Override
@@ -632,6 +634,22 @@ public LatLng getLastFocusLocation() {
632634
return lastFocusLocation==null?null:LocationUtils.commonsLatLngToMapBoxLatLng(lastFocusLocation);
633635
}
634636

637+
@Override
638+
public boolean isCurrentLocationMarkerVisible() {
639+
if (latLngBounds == null) {
640+
Timber.d("Map projection bounds are null");
641+
return false;
642+
} else {
643+
Timber.d("Current location marker %s" , latLngBounds.contains(currentLocationMarker.getPosition()) ? "visible" : "invisible");
644+
return latLngBounds.contains(currentLocationMarker.getPosition());
645+
}
646+
}
647+
648+
@Override
649+
public void setProjectorLatLngBounds() {
650+
latLngBounds = mapBox.getProjection().getVisibleRegion().latLngBounds;
651+
}
652+
635653
@Override
636654
public boolean isNetworkConnectionEstablished() {
637655
return NetworkUtils.isInternetConnectionEstablished(getActivity());
@@ -906,9 +924,8 @@ public void displayLoginSkippedWarning() {
906924
}
907925

908926
private void handleLocationUpdate(fr.free.nrw.commons.location.LatLng latLng, LocationServiceManager.LocationChangeType locationChangeType){
909-
setMapBoxPosition(latLng);
910-
this.lastKnownLocation=latLng;
911-
NearbyController.currentLocation=lastKnownLocation;
927+
this.lastKnownLocation = latLng;
928+
NearbyController.currentLocation = lastKnownLocation;
912929
presenter.updateMapAndList(locationChangeType);
913930
}
914931

@@ -941,14 +958,6 @@ public void onLocationChangedMedium(fr.free.nrw.commons.location.LatLng latLng)
941958
}
942959
}
943960

944-
void setMapBoxPosition(fr.free.nrw.commons.location.LatLng latLng){
945-
CameraPosition position = new CameraPosition.Builder()
946-
.target(LocationUtils.commonsLatLngToMapBoxLatLng(latLng)) // Sets the new camera position
947-
.zoom(ZOOM_LEVEL) // Same zoom level
948-
.build();
949-
mapBox.moveCamera(CameraUpdateFactory.newCameraPosition(position));
950-
}
951-
952961
public void backButtonClicked() {
953962
presenter.backButtonClicked();
954963
}

app/src/main/java/fr/free/nrw/commons/nearby/presenter/NearbyParentFragmentPresenter.java

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import java.util.HashMap;
99
import java.util.List;
1010

11-
import javax.inject.Inject;
12-
1311
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao;
1412
import fr.free.nrw.commons.kvstore.JsonKvStore;
1513
import fr.free.nrw.commons.location.LatLng;
@@ -21,9 +19,7 @@
2119
import fr.free.nrw.commons.nearby.NearbyBaseMarker;
2220
import fr.free.nrw.commons.nearby.NearbyController;
2321
import fr.free.nrw.commons.nearby.NearbyFilterState;
24-
import fr.free.nrw.commons.nearby.Place;
2522
import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract;
26-
import fr.free.nrw.commons.upload.UploadContract;
2723
import fr.free.nrw.commons.utils.LocationUtils;
2824
import fr.free.nrw.commons.wikidata.WikidataEditListener;
2925
import timber.log.Timber;
@@ -194,7 +190,7 @@ public void updateMapAndList(LocationServiceManager.LocationChangeType locationC
194190
nearbyParentFragmentView.populatePlaces(nearbyParentFragmentView.getCameraTarget());
195191
} else { // Means location changed slightly, ie user is walking or driving.
196192
Timber.d("Means location changed slightly");
197-
if (!nearbyParentFragmentView.isSearchThisAreaButtonVisible()) { // Do not track users position if the user is checking around
193+
if (nearbyParentFragmentView.isCurrentLocationMarkerVisible()){ // Means user wants to see their live location
198194
nearbyParentFragmentView.recenterMap(curLatLng);
199195
}
200196
}
@@ -259,6 +255,7 @@ public void onLocationChangedMedium(LatLng latLng) {
259255

260256
@Override
261257
public void onCameraMove(com.mapbox.mapboxsdk.geometry.LatLng latLng) {
258+
nearbyParentFragmentView.setProjectorLatLngBounds();
262259
// If our nearby markers are calculated at least once
263260
if (NearbyController.latestSearchLocation != null) {
264261
double distance =latLng.distanceTo

0 commit comments

Comments
 (0)