Added search location feature

This commit is contained in:
Kanahia 2023-12-11 22:52:35 +05:30 committed by Nicolas Raoul
parent dd8fd3fa3a
commit 09b064e9c1
3 changed files with 250 additions and 136 deletions

View file

@ -51,7 +51,7 @@ public interface NearbyParentFragmentContract {
void addCurrentLocationMarker(LatLng curLatLng); void addCurrentLocationMarker(LatLng curLatLng);
void updateMapToTrackPosition(LatLng curLatLng); void updateMapToTrackPosition(LatLng curLatLng);
void clearAllMarkers();
Context getContext(); Context getContext();
void updateMapMarkers(List<NearbyBaseMarker> nearbyBaseMarkers, Marker selectedMarker); void updateMapMarkers(List<NearbyBaseMarker> nearbyBaseMarkers, Marker selectedMarker);
@ -70,6 +70,14 @@ public interface NearbyParentFragmentContract {
LatLng getLastLocation(); LatLng getLastLocation();
LatLng getLastMapFocus();
void setLastMapFocus();
LatLng getMapCenter();
LatLng getMapFocus();
com.mapbox.mapboxsdk.geometry.LatLng getLastFocusLocation(); com.mapbox.mapboxsdk.geometry.LatLng getLastFocusLocation();
boolean isCurrentLocationMarkerVisible(); boolean isCurrentLocationMarkerVisible();

View file

@ -19,6 +19,7 @@ import android.graphics.Bitmap;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationManager; import android.location.LocationManager;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
@ -26,7 +27,6 @@ import android.preference.PreferenceManager;
import android.provider.Settings; import android.provider.Settings;
import android.text.Html; import android.text.Html;
import android.text.method.LinkMovementMethod; import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
@ -80,12 +80,8 @@ import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.UiSettings;
import com.mapbox.pluginscalebar.ScaleBarOptions;
import com.mapbox.pluginscalebar.ScaleBarPlugin;
import fr.free.nrw.commons.CommonsApplication; import fr.free.nrw.commons.CommonsApplication;
import fr.free.nrw.commons.MapController.NearbyPlacesInfo; import fr.free.nrw.commons.MapController.NearbyPlacesInfo;
import fr.free.nrw.commons.MapStyle;
import fr.free.nrw.commons.R; import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils; import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.auth.LoginActivity; import fr.free.nrw.commons.auth.LoginActivity;
@ -136,6 +132,9 @@ import javax.inject.Inject;
import javax.inject.Named; import javax.inject.Named;
import kotlin.Unit; import kotlin.Unit;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.osmdroid.events.MapListener;
import org.osmdroid.events.ScrollEvent;
import org.osmdroid.events.ZoomEvent;
import org.osmdroid.tileprovider.MapTileProviderBasic; import org.osmdroid.tileprovider.MapTileProviderBasic;
import org.osmdroid.tileprovider.tilesource.ITileSource; import org.osmdroid.tileprovider.tilesource.ITileSource;
import org.osmdroid.tileprovider.tilesource.XYTileSource; import org.osmdroid.tileprovider.tilesource.XYTileSource;
@ -254,6 +253,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private boolean isPermissionDenied; private boolean isPermissionDenied;
private boolean recenterToUserLocation; private boolean recenterToUserLocation;
private MapboxMap mapBox; private MapboxMap mapBox;
private GeoPoint mapCenter;
IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION); IntentFilter intentFilter = new IntentFilter(NETWORK_INTENT_ACTION);
private Marker currentLocationMarker; private Marker currentLocationMarker;
private Polygon currentLocationPolygon; private Polygon currentLocationPolygon;
@ -264,6 +264,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private fr.free.nrw.commons.location.LatLng lastFocusLocation; private fr.free.nrw.commons.location.LatLng lastFocusLocation;
private LatLngBounds latLngBounds; private LatLngBounds latLngBounds;
private PlaceAdapter adapter; private PlaceAdapter adapter;
private GeoPoint lastMapFocus;
private NearbyParentFragmentInstanceReadyCallback nearbyParentFragmentInstanceReadyCallback; private NearbyParentFragmentInstanceReadyCallback nearbyParentFragmentInstanceReadyCallback;
private boolean isAdvancedQueryFragmentVisible = false; private boolean isAdvancedQueryFragmentVisible = false;
private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() { private ActivityResultLauncher<String[]> inAppCameraLocationPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(), new ActivityResultCallback<Map<String, Boolean>>() {
@ -384,23 +385,29 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
compassArrow.setRotation(0); compassArrow.setRotation(0);
}); });
cameraMoveListener= () -> presenter.onCameraMove(mapBox.getCameraPosition().target); //cameraMoveListener= () -> presenter.onCameraMove(mapBox.getCameraPosition().target);
//TODO
addCheckBoxCallback(); addCheckBoxCallback();
presenter.attachView(this); presenter.attachView(this);
isPermissionDenied = false; isPermissionDenied = false;
recenterToUserLocation = false; recenterToUserLocation = false;
initRvNearbyList();
//initRvNearbyList();
initThemePreferences(); initThemePreferences();
initViews();
presenter.setActionListeners(applicationKvStore);
initNearbyFilter();
org.osmdroid.config.Configuration.getInstance().load(this.getContext(),PreferenceManager.getDefaultSharedPreferences(this.getContext())); org.osmdroid.config.Configuration.getInstance().load(this.getContext(),PreferenceManager.getDefaultSharedPreferences(this.getContext()));
mMapView.setTilesScaledToDpi(true); mMapView.setTilesScaledToDpi(true);
final GeoPoint intialGeoPoint;
if(applicationKvStore.getString("LastLocation")!=null) { // Checking for last searched location if(applicationKvStore.getString("LastLocation")!=null) { // Checking for last searched location
String[] locationLatLng = applicationKvStore.getString("LastLocation").split(","); String[] locationLatLng = applicationKvStore.getString("LastLocation").split(",");
intialGeoPoint = new GeoPoint(Double.valueOf(locationLatLng[0]),Double.valueOf(locationLatLng[1])); lastMapFocus = new GeoPoint(Double.valueOf(locationLatLng[0]),Double.valueOf(locationLatLng[1]));
}else { }else {
intialGeoPoint = new GeoPoint(51.50550,-0.07520); lastMapFocus = new GeoPoint(51.50550,-0.07520);
} }
ScaleBarOverlay scaleBarOverlay = new ScaleBarOverlay(mMapView); ScaleBarOverlay scaleBarOverlay = new ScaleBarOverlay(mMapView);
scaleBarOverlay.setScaleBarOffset(15,25); scaleBarOverlay.setScaleBarOffset(15,25);
Paint barPaint = new Paint(); Paint barPaint = new Paint();
@ -408,7 +415,6 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
scaleBarOverlay.setBackgroundPaint(barPaint); scaleBarOverlay.setBackgroundPaint(barPaint);
scaleBarOverlay.enableScaleBar(); scaleBarOverlay.enableScaleBar();
mMapView.getOverlays().add(scaleBarOverlay); mMapView.getOverlays().add(scaleBarOverlay);
mMapView.getController().setCenter(intialGeoPoint);
mMapView.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER); mMapView.getZoomController().setVisibility(CustomZoomButtonsController.Visibility.NEVER);
mMapView.getController().setZoom(ZOOM_LEVEL); mMapView.getController().setZoom(ZOOM_LEVEL);
MapTileProviderBasic tileProvider = new MapTileProviderBasic(this.getContext()); MapTileProviderBasic tileProvider = new MapTileProviderBasic(this.getContext());
@ -430,70 +436,91 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
rotationGestureOverlay.setEnabled(true); rotationGestureOverlay.setEnabled(true);
mMapView.setMultiTouchControls(true); mMapView.setMultiTouchControls(true);
mMapView.getOverlays().add(rotationGestureOverlay); mMapView.getOverlays().add(rotationGestureOverlay);
initViews(); setSearchThisAreaButtonVisibility(false);
presenter.setActionListeners(applicationKvStore); mMapView.addMapListener(new MapListener() {
initNearbyFilter(); @Override
if(nearbyParentFragmentInstanceReadyCallback!=null){ public boolean onScroll(ScrollEvent event) {
nearbyParentFragmentInstanceReadyCallback.onReady(); if (lastMapFocus != null ) {
} Location mylocation = new Location("");
performMapReadyActions(); Location dest_location = new Location("");
recenterMarkerToPosition(intialGeoPoint); dest_location.setLatitude(mMapView.getMapCenter().getLatitude());
onResume(); dest_location.setLongitude(mMapView.getMapCenter().getLongitude());
mylocation.setLatitude(lastMapFocus.getLatitude());
//TODO Remove MapBox Code mylocation.setLongitude(lastMapFocus.getLongitude());
mapView.onCreate(savedInstanceState); Float distance = mylocation.distanceTo(dest_location);//in meters
mapView.getMapAsync(mapBoxMap -> { if (lastMapFocus != null) {
mapBox =mapBoxMap; if (isNetworkConnectionEstablished()) {
initViews(); if (distance > 2000.0) {
presenter.setActionListeners(applicationKvStore); setSearchThisAreaButtonVisibility(true);
initNearbyFilter();
mapBoxMap.setStyle(isDarkTheme? MapStyle.DARK :
MapStyle.OUTDOORS, style -> {
final UiSettings uiSettings = mapBoxMap.getUiSettings();
uiSettings.setCompassGravity(Gravity.BOTTOM | Gravity.LEFT);
uiSettings.setCompassMargins(12, 0, 0, 24);
uiSettings.setLogoEnabled(false);
uiSettings.setAttributionEnabled(false);
uiSettings.setRotateGesturesEnabled(false);
isMapBoxReady =true;
if(nearbyParentFragmentInstanceReadyCallback!=null){
nearbyParentFragmentInstanceReadyCallback.onReady();
}
performMapReadyActions();
final CameraPosition cameraPosition;
if(applicationKvStore.getString("LastLocation")!=null) { // Checking for last searched location
String[] locationLatLng = applicationKvStore.getString("LastLocation").split(",");
cameraPosition = new CameraPosition.Builder()
.target(new LatLng(Double.valueOf(locationLatLng[0]),
Double.valueOf(locationLatLng[1])))
.zoom(ZOOM_LEVEL)
.build();
} else { } else {
cameraPosition = new CameraPosition.Builder() setSearchThisAreaButtonVisibility(false);
.target(new LatLng(51.50550, -0.07520)) }
.zoom(ZOOM_OUT) }
.build(); } else {
setSearchThisAreaButtonVisibility(false);
}
}
return true;
}
@Override
public boolean onZoom(ZoomEvent event) {
return false;
} }
mapBoxMap.setCameraPosition(cameraPosition);
final ScaleBarPlugin scaleBarPlugin = new ScaleBarPlugin(mapView, mapBoxMap);
final int color = isDarkTheme ? R.color.bottom_bar_light : R.color.bottom_bar_dark;
final ScaleBarOptions scaleBarOptions = new ScaleBarOptions(getContext())
.setTextColor(color)
.setTextSize(R.dimen.description_text_size)
.setBarHeight(R.dimen.tiny_gap)
.setBorderWidth(R.dimen.miniscule_margin)
.setMarginTop(R.dimen.tiny_padding)
.setMarginLeft(R.dimen.tiny_padding)
.setTextBarMargin(R.dimen.tiny_padding);
scaleBarPlugin.create(scaleBarOptions);
onResume();
});
}); });
if(nearbyParentFragmentInstanceReadyCallback!=null){
nearbyParentFragmentInstanceReadyCallback.onReady();
}
performMapReadyActions();
moveCameraToPosition(lastMapFocus);
setSearchThisAreaButtonVisibility(false);
initRvNearbyList();
//TODO Remove MapBox Code
// mapView.onCreate(savedInstanceState);
// mapView.getMapAsync(mapBoxMap -> {
// mapBox =mapBoxMap;
// initViews();
// presenter.setActionListeners(applicationKvStore);
// initNearbyFilter();
// mapBoxMap.setStyle(isDarkTheme? MapStyle.DARK :
// MapStyle.OUTDOORS, style -> {
// final UiSettings uiSettings = mapBoxMap.getUiSettings();
// uiSettings.setCompassGravity(Gravity.BOTTOM | Gravity.LEFT);
// uiSettings.setCompassMargins(12, 0, 0, 24);
// uiSettings.setLogoEnabled(false);
// uiSettings.setAttributionEnabled(false);
// uiSettings.setRotateGesturesEnabled(false);
// isMapBoxReady =true;
// if(nearbyParentFragmentInstanceReadyCallback!=null){
// nearbyParentFragmentInstanceReadyCallback.onReady();
// }
// performMapReadyActions();
// final CameraPosition cameraPosition;
// if(applicationKvStore.getString("LastLocation")!=null) { // Checking for last searched location
// String[] locationLatLng = applicationKvStore.getString("LastLocation").split(",");
// cameraPosition = new CameraPosition.Builder()
// .target(new LatLng(Double.valueOf(locationLatLng[0]),
// Double.valueOf(locationLatLng[1])))
// .zoom(ZOOM_LEVEL)
// .build();
// }else {
// cameraPosition = new CameraPosition.Builder()
// .target(new LatLng(51.50550, -0.07520))
// .zoom(ZOOM_OUT)
// .build();
// }
// mapBoxMap.setCameraPosition(cameraPosition);
// onResume();
// });
// });
tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution))); tvAttribution.setText(Html.fromHtml(getString(R.string.map_attribution)));
tvAttribution.setMovementMethod(LinkMovementMethod.getInstance()); tvAttribution.setMovementMethod(LinkMovementMethod.getInstance());
btnAdvancedOptions.setOnClickListener(v -> { btnAdvancedOptions.setOnClickListener(v -> {
searchView.clearFocus(); searchView.clearFocus();
showHideAdvancedQueryFragment(true); showHideAdvancedQueryFragment(true);
@ -540,6 +567,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
checkBoxTriStates.setTextColor(getContext().getResources().getColor(android.R.color.white)); checkBoxTriStates.setTextColor(getContext().getResources().getColor(android.R.color.white));
checkBoxTriStates.setTextColor(getContext().getResources().getColor(android.R.color.white)); checkBoxTriStates.setTextColor(getContext().getResources().getColor(android.R.color.white));
nearbyFilterList.setBackgroundColor(getContext().getResources().getColor(R.color.contributionListDarkBackground)); nearbyFilterList.setBackgroundColor(getContext().getResources().getColor(R.color.contributionListDarkBackground));
mMapView.getOverlayManager().getTilesOverlay().setColorFilter(TilesOverlay.INVERT_COLORS);
}else{ }else{
rvNearbyList.setBackgroundColor(getContext().getResources().getColor(android.R.color.white)); rvNearbyList.setBackgroundColor(getContext().getResources().getColor(android.R.color.white));
checkBoxTriStates.setTextColor(getContext().getResources().getColor(R.color.contributionListDarkBackground)); checkBoxTriStates.setTextColor(getContext().getResources().getColor(R.color.contributionListDarkBackground));
@ -591,15 +619,11 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
target=lastKnownLocation; target=lastKnownLocation;
} }
if (lastKnownLocation != null) { if (lastKnownLocation != null) {
final CameraPosition position = new CameraPosition.Builder()
.target(LocationUtils.commonsLatLngToMapBoxLatLng(target)) // Sets the new camera position
.zoom(ZOOM_LEVEL) // Same zoom level
.build();
//TODO
if(target != null){
GeoPoint targetP = new GeoPoint(target.getLatitude(), target.getLongitude()); GeoPoint targetP = new GeoPoint(target.getLatitude(), target.getLongitude());
recenterMarkerToPosition(targetP);} mapCenter = targetP;
// mapBox.moveCamera(CameraUpdateFactory.newCameraPosition(position)); mMapView.getController().setCenter(targetP);
recenterMarkerToPosition(targetP);
moveCameraToPosition(targetP);
} }
else if(locationManager.isGPSProviderEnabled()||locationManager.isNetworkProviderEnabled()){ else if(locationManager.isGPSProviderEnabled()||locationManager.isNetworkProviderEnabled()){
locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER); locationManager.requestLocationUpdatesFromProvider(LocationManager.NETWORK_PROVIDER);
@ -617,7 +641,6 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
mapView.onResume();
mMapView.onResume(); mMapView.onResume();
presenter.attachView(this); presenter.attachView(this);
registerNetworkReceiver(); registerNetworkReceiver();
@ -1082,6 +1105,30 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
return lastKnownLocation; return lastKnownLocation;
} }
@Override
public fr.free.nrw.commons.location.LatLng getLastMapFocus(){
fr.free.nrw.commons.location.LatLng latLng = new fr.free.nrw.commons.location.LatLng(
lastMapFocus.getLatitude(), lastMapFocus.getLongitude(),100);
return latLng;
}
@Override
public fr.free.nrw.commons.location.LatLng getMapCenter(){
fr.free.nrw.commons.location.LatLng latLnge = new fr.free.nrw.commons.location.LatLng(mapCenter.getLatitude(),mapCenter.getLongitude(),100);
return latLnge;
}
@Override
public fr.free.nrw.commons.location.LatLng getMapFocus(){
fr.free.nrw.commons.location.LatLng latLnge = new fr.free.nrw.commons.location.LatLng(mMapView.getMapCenter().getLatitude(),mMapView.getMapCenter().getLongitude(),100);
return latLnge;
}
@Override
public void setLastMapFocus(){
lastMapFocus = (GeoPoint) mMapView.getMapCenter();
}
@Override @Override
public LatLng getLastFocusLocation() { public LatLng getLastFocusLocation() {
return lastFocusLocation==null?null:LocationUtils.commonsLatLngToMapBoxLatLng(lastFocusLocation); return lastFocusLocation==null?null:LocationUtils.commonsLatLngToMapBoxLatLng(lastFocusLocation);
@ -1175,11 +1222,17 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override @Override
public void populatePlaces(final fr.free.nrw.commons.location.LatLng curlatLng) { public void populatePlaces(final fr.free.nrw.commons.location.LatLng curlatLng) {
if (curlatLng.equals(lastFocusLocation) || lastFocusLocation == null || recenterToUserLocation) { // Means we are checking around current location if (curlatLng.equals(getLastMapFocus())) { // Means we are checking around current location
populatePlacesForCurrentLocation(lastKnownLocation, curlatLng, null); populatePlacesForCurrentLocation(getLastMapFocus(), curlatLng, null);
} else { } else {
populatePlacesForAnotherLocation(lastKnownLocation, curlatLng, null); populatePlacesForAnotherLocation(getLastMapFocus(), curlatLng, null);
} }
// if (curlatLng.equals(lastFocusLocation) || lastFocusLocation == null || recenterToUserLocation) { // Means we are checking around current location
// populatePlacesForCurrentLocation(lastKnownLocation, curlatLng, null);
// } else {
// populatePlacesForAnotherLocation(lastKnownLocation, curlatLng, null);
// }
//TODO
if(recenterToUserLocation) { if(recenterToUserLocation) {
recenterToUserLocation = false; recenterToUserLocation = false;
} }
@ -1207,7 +1260,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
private void populatePlacesForCurrentLocation( private void populatePlacesForCurrentLocation(
final fr.free.nrw.commons.location.LatLng curlatLng, final fr.free.nrw.commons.location.LatLng curlatLng,
final fr.free.nrw.commons.location.LatLng searchLatLng, @Nullable final String customQuery){ final fr.free.nrw.commons.location.LatLng searchLatLng, @Nullable final String customQuery){
Timber.tag("PRINT").d("CURRENT");
final Observable<NearbyController.NearbyPlacesInfo> nearbyPlacesInfoObservable = Observable final Observable<NearbyController.NearbyPlacesInfo> nearbyPlacesInfoObservable = Observable
.fromCallable(() -> nearbyController .fromCallable(() -> nearbyController
.loadAttractionsFromLocation(curlatLng, searchLatLng, .loadAttractionsFromLocation(curlatLng, searchLatLng,
@ -1222,6 +1275,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
} else { } else {
updateMapMarkers(nearbyPlacesInfo, true); updateMapMarkers(nearbyPlacesInfo, true);
lastFocusLocation = searchLatLng; lastFocusLocation = searchLatLng;
lastMapFocus = new GeoPoint(searchLatLng.getLatitude(),searchLatLng.getLongitude());
} }
}, },
throwable -> { throwable -> {
@ -1249,9 +1303,11 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
showErrorMessage(getString(R.string.no_nearby_places_around)); showErrorMessage(getString(R.string.no_nearby_places_around));
} else { } else {
// Updating last searched location // Updating last searched location
Timber.tag("PRINT").e(nearbyPlacesInfo.placeList.get(0).name);
applicationKvStore.putString("LastLocation", searchLatLng.getLatitude() + "," + searchLatLng.getLongitude()); applicationKvStore.putString("LastLocation", searchLatLng.getLatitude() + "," + searchLatLng.getLongitude());
updateMapMarkers(nearbyPlacesInfo, false); updateMapMarkers(nearbyPlacesInfo, false);
lastFocusLocation = searchLatLng; lastMapFocus = new GeoPoint(searchLatLng.getLatitude(),searchLatLng.getLongitude()) ;
//lastFocusLocation = searchLatLng;
} }
}, },
throwable -> { throwable -> {
@ -1270,7 +1326,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
*/ */
private void updateMapMarkers(final NearbyController.NearbyPlacesInfo nearbyPlacesInfo, final boolean shouldUpdateSelectedMarker) { private void updateMapMarkers(final NearbyController.NearbyPlacesInfo nearbyPlacesInfo, final boolean shouldUpdateSelectedMarker) {
presenter.updateMapMarkers(nearbyPlacesInfo, selectedMarker,shouldUpdateSelectedMarker); presenter.updateMapMarkers(nearbyPlacesInfo, selectedMarker,shouldUpdateSelectedMarker);
setFilterState(); //TODO
//setFilterState();
} }
@ -1560,7 +1617,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
@Override @Override
public void updateMapMarkers(final List<NearbyBaseMarker> nearbyBaseMarkers, final Marker selectedMarker) { public void updateMapMarkers(final List<NearbyBaseMarker> nearbyBaseMarkers, final Marker selectedMarker) {
if (mMapView != null){ if (mMapView != null){
addNearbyMarkersToMapBoxMap(nearbyBaseMarkers,selectedMarker); //addMarkersToMap(nearbyBaseMarkers,selectedMarker);
presenter.updateMapMarkersToController(nearbyBaseMarkers);
} }
} }
@ -1597,6 +1655,8 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
// Remove the previous markers before updating them // Remove the previous markers before updating them
hideAllMarkers(); hideAllMarkers();
//TODO
// clearAllMarkers();
filteredMarkers = new ArrayList<>(); filteredMarkers = new ArrayList<>();
@ -1644,9 +1704,11 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
updateMarker(markerPlaceGroup.getIsBookmarked(), place, NearbyController.currentLocation); updateMarker(markerPlaceGroup.getIsBookmarked(), place, NearbyController.currentLocation);
} }
} }
// TODO
mapBox.clear(); // mapBox.clear();
mapBox.addMarkers(filteredMarkers); // mapBox.addMarkers(filteredMarkers);
// addNearbyMarkersToMapBoxMap(filteredMarkers,null);
addMarkersToMap(filteredMarkers,null);
} }
@Override @Override
@ -1721,16 +1783,17 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
/** /**
* Adds multiple markers representing places to the map and handles item gestures. * Adds multiple markers representing places to the map and handles item gestures.
* *
* @param places The list of Place objects containing information about the locations. * @param nearbyBaseMarkers The list of Place objects containing information about the locations.
*/ */
private void addMarkersToMap(ArrayList<Place> places){ private void addMarkersToMap(List<NearbyBaseMarker> nearbyBaseMarkers,final Marker selectedMarker){
Boolean isBookMarked = false; //TODO Work on bookmarks
//clearAllMarkers();
ArrayList<OverlayItem> items = new ArrayList<>(); ArrayList<OverlayItem> items = new ArrayList<>();
clearAllMarkers(); for (int i = 0;i<nearbyBaseMarkers.size();i++){
for (int i = 0;i<places.size();i++){ Drawable icon = ContextCompat.getDrawable(getContext(),getIconFor(nearbyBaseMarkers.get(i).getPlace(),false));
Drawable icon = ContextCompat.getDrawable(getContext(),getIconFor(places.get(i),isBookMarked)); GeoPoint point = new GeoPoint(nearbyBaseMarkers.get(i).getPlace().location.getLatitude(),nearbyBaseMarkers.get(i).getPlace().location.getLongitude());
GeoPoint point = new GeoPoint(places.get(i).location.getLatitude(),places.get(i).location.getLongitude()); OverlayItem item = new OverlayItem(nearbyBaseMarkers.get(i).getPlace().name,null,point);
OverlayItem item = new OverlayItem(places.get(i).name,null,point);
item.setMarker(icon); item.setMarker(icon);
items.add(item); items.add(item);
} }
@ -1738,7 +1801,7 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
new OnItemGestureListener<OverlayItem>() { new OnItemGestureListener<OverlayItem>() {
@Override @Override
public boolean onItemSingleTapUp(int index, OverlayItem item) { public boolean onItemSingleTapUp(int index, OverlayItem item) {
final Place place = places.get(index); final Place place = nearbyBaseMarkers.get(index).getPlace();
passInfoToSheet(place); passInfoToSheet(place);
hideBottomSheet(); hideBottomSheet();
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
@ -1760,32 +1823,31 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
* since grey icon may lead the users to believe that it is disabled or prohibited contribution * since grey icon may lead the users to believe that it is disabled or prohibited contribution
*/ */
private void hideAllMarkers() { private void hideAllMarkers() {
final VectorDrawableCompat vectorDrawable; // TODO
vectorDrawable = VectorDrawableCompat.create( // final VectorDrawableCompat vectorDrawable;
getContext().getResources(), R.drawable.ic_custom_greyed_out_marker, getContext().getTheme()); // vectorDrawable = VectorDrawableCompat.create(
final Bitmap icon = UiUtils.getBitmap(vectorDrawable); // getContext().getResources(), R.drawable.ic_custom_greyed_out_marker, getContext().getTheme());
for (final Marker marker : mapBox.getMarkers()) { // final Bitmap icon = UiUtils.getBitmap(vectorDrawable);
if (!marker.equals(currentLocationMarker)) { // for (final Marker marker : mapBox.getMarkers()) {
marker.setIcon(IconFactory.getInstance(getContext()).fromBitmap(icon)); // if (!marker.equals(currentLocationMarker)) {
} // marker.setIcon(IconFactory.getInstance(getContext()).fromBitmap(icon));
} // }
addCurrentLocationMarker(NearbyController.currentLocation); // }
//addCurrentLocationMarker(NearbyController.currentLocation);
//recenterMap(NearbyController.currentLocation);
} }
private void addNearbyMarkersToMapBoxMap(final List<NearbyBaseMarker> nearbyBaseMarkers, final Marker selectedMarker) { private void addNearbyMarkersToMapBoxMap(final List<NearbyBaseMarker> nearbyBaseMarkers, final Marker selectedMarker) {
if (mMapView != null){ if (mMapView != null){
allMarkers = new ArrayList<>(nearbyBaseMarkers); allMarkers = new ArrayList<>(nearbyBaseMarkers);
ArrayList<Place> places1 = new ArrayList<>(); mapBox.addMarkers(nearbyBaseMarkers);
//TODO Work on bookmarks addMarkersToMap(allMarkers,selectedMarker);
for (int i =0;i<allMarkers.size();i++){
places1.add(allMarkers.get(i).getPlace());
}
addMarkersToMap(places1);
} }
} }
@Override @Override
public void recenterMap(fr.free.nrw.commons.location.LatLng curLatLng) { public void recenterMap(fr.free.nrw.commons.location.LatLng curLatLng) {
//TODO
if (isPermissionDenied || curLatLng == null) { if (isPermissionDenied || curLatLng == null) {
recenterToUserLocation = true; recenterToUserLocation = true;
checkPermissionsAndPerformAction(); checkPermissionsAndPerformAction();
@ -2012,9 +2074,41 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
* Clears all markers from the map and resets certain map overlays and gestures. * Clears all markers from the map and resets certain map overlays and gestures.
* After clearing markers, it re-adds a scale bar overlay and rotation gesture overlay to the map. * After clearing markers, it re-adds a scale bar overlay and rotation gesture overlay to the map.
*/ */
private void clearAllMarkers(){ @Override
public void clearAllMarkers(){
mMapView.getOverlayManager().clear() ; mMapView.getOverlayManager().clear() ;
mMapView.invalidate(); mMapView.invalidate();
GeoPoint geoPoint = mapCenter ;
if(geoPoint != null) {
List<Overlay> overlays = mMapView.getOverlays();
ScaleDiskOverlay diskOverlay =
new ScaleDiskOverlay(this.getContext(),
geoPoint, 2000, GeoConstants.UnitOfMeasure.foot);
Paint circlePaint = new Paint();
circlePaint.setColor(Color.rgb(128, 128, 128));
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeWidth(2f);
diskOverlay.setCirclePaint2(circlePaint);
Paint diskPaint = new Paint();
diskPaint.setColor(Color.argb(40, 128, 128, 128));
diskPaint.setStyle(Paint.Style.FILL_AND_STROKE);
diskOverlay.setCirclePaint1(diskPaint);
diskOverlay.setDisplaySizeMin(900);
diskOverlay.setDisplaySizeMax(1700);
mMapView.getOverlays().add(diskOverlay);
org.osmdroid.views.overlay.Marker startMarker = new org.osmdroid.views.overlay.Marker(
mMapView);
startMarker.setPosition(geoPoint);
startMarker.setAnchor(org.osmdroid.views.overlay.Marker.ANCHOR_CENTER,
org.osmdroid.views.overlay.Marker.ANCHOR_BOTTOM);
startMarker.setIcon(
ContextCompat.getDrawable(this.getContext(), R.drawable.current_location_marker));
startMarker.setTitle("Your Location");
startMarker.setTextLabelFontSize(24);
mMapView.getOverlays().add(startMarker);
}
ScaleBarOverlay scaleBarOverlay = new ScaleBarOverlay(mMapView); ScaleBarOverlay scaleBarOverlay = new ScaleBarOverlay(mMapView);
scaleBarOverlay.setScaleBarOffset(15,25); scaleBarOverlay.setScaleBarOffset(15,25);
@ -2023,10 +2117,6 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
scaleBarOverlay.setBackgroundPaint(barPaint); scaleBarOverlay.setBackgroundPaint(barPaint);
scaleBarOverlay.enableScaleBar(); scaleBarOverlay.enableScaleBar();
mMapView.getOverlays().add(scaleBarOverlay); mMapView.getOverlays().add(scaleBarOverlay);
RotationGestureOverlay rotationGestureOverlay = new RotationGestureOverlay(mMapView,compassArrow,compassBg);
rotationGestureOverlay.setEnabled(true);
mMapView.setMultiTouchControls(true);
mMapView.getOverlays().add(rotationGestureOverlay);
} }
/** /**
@ -2075,6 +2165,10 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
} }
} }
private void moveCameraToPosition(GeoPoint geoPoint){
mMapView.getController().animateTo(geoPoint);
}
public interface NearbyParentFragmentInstanceReadyCallback{ public interface NearbyParentFragmentInstanceReadyCallback{
void onReady(); void onReady();
} }

View file

@ -1,5 +1,6 @@
package fr.free.nrw.commons.nearby.presenter; package fr.free.nrw.commons.nearby.presenter;
import android.location.Location;
import android.view.View; import android.view.View;
import androidx.annotation.MainThread; import androidx.annotation.MainThread;
@ -24,7 +25,6 @@ import fr.free.nrw.commons.nearby.NearbyFilterState;
import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract; import fr.free.nrw.commons.nearby.contract.NearbyParentFragmentContract;
import fr.free.nrw.commons.utils.LocationUtils; import fr.free.nrw.commons.utils.LocationUtils;
import fr.free.nrw.commons.wikidata.WikidataEditListener; import fr.free.nrw.commons.wikidata.WikidataEditListener;
import org.osmdroid.util.GeoPoint;
import timber.log.Timber; import timber.log.Timber;
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.CUSTOM_QUERY; import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.CUSTOM_QUERY;
@ -181,8 +181,13 @@ public class NearbyParentFragmentPresenter
return; return;
} }
LatLng lastLocation = nearbyParentFragmentView.getLastLocation(); LatLng lastLocation = nearbyParentFragmentView.getLastMapFocus();
if (nearbyParentFragmentView.getMapCenter() != null){
curLatLng = nearbyParentFragmentView.getMapCenter();
}else {
curLatLng = lastLocation; curLatLng = lastLocation;
}
Timber.tag("PRINT").e(curLatLng.getLatitude()+" "+nearbyParentFragmentView.getMapCenter().getLongitude());
if (curLatLng == null) { if (curLatLng == null) {
Timber.d("Skipping update of nearby places as location is unavailable"); Timber.d("Skipping update of nearby places as location is unavailable");
@ -205,16 +210,14 @@ public class NearbyParentFragmentPresenter
} }
else if (locationChangeType.equals(LOCATION_SIGNIFICANTLY_CHANGED) else if (locationChangeType.equals(LOCATION_SIGNIFICANTLY_CHANGED)
|| locationChangeType.equals(MAP_UPDATED)) { || locationChangeType.equals(MAP_UPDATED)) {
Timber.d("LOCATION_SIGNIFICANTLY_CHANGED");
lockUnlockNearby(true); lockUnlockNearby(true);
nearbyParentFragmentView.setProgressBarVisibility(true); nearbyParentFragmentView.setProgressBarVisibility(true);
nearbyParentFragmentView.populatePlaces(lastLocation); nearbyParentFragmentView.populatePlaces(nearbyParentFragmentView.getMapCenter());
} else if (locationChangeType.equals(SEARCH_CUSTOM_AREA)) { } else if (locationChangeType.equals(SEARCH_CUSTOM_AREA)) {
Timber.d("SEARCH_CUSTOM_AREA"); Timber.d("SEARCH_CUSTOM_AREA");
lockUnlockNearby(true); lockUnlockNearby(true);
nearbyParentFragmentView.setProgressBarVisibility(true); nearbyParentFragmentView.setProgressBarVisibility(true);
nearbyParentFragmentView.populatePlaces(nearbyParentFragmentView.getCameraTarget()); nearbyParentFragmentView.populatePlaces(nearbyParentFragmentView.getMapFocus());
} else { // Means location changed slightly, ie user is walking or driving. } else { // Means location changed slightly, ie user is walking or driving.
Timber.d("Means location changed slightly"); Timber.d("Means location changed slightly");
if (nearbyParentFragmentView.isCurrentLocationMarkerVisible()){ // Means user wants to see their live location if (nearbyParentFragmentView.isCurrentLocationMarkerVisible()){ // Means user wants to see their live location
@ -230,22 +233,24 @@ public class NearbyParentFragmentPresenter
*/ */
public void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo, Marker selectedMarker, boolean shouldTrackPosition) { public void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo, Marker selectedMarker, boolean shouldTrackPosition) {
if(null!=nearbyParentFragmentView) { if(null!=nearbyParentFragmentView) {
Timber.tag("PRINT").d("UPDATING...");
nearbyParentFragmentView.clearAllMarkers();
List<NearbyBaseMarker> nearbyBaseMarkers = NearbyController List<NearbyBaseMarker> nearbyBaseMarkers = NearbyController
.loadAttractionsFromLocationToBaseMarkerOptions(nearbyPlacesInfo.curLatLng, // Curlatlang will be used to calculate distances .loadAttractionsFromLocationToBaseMarkerOptions(nearbyPlacesInfo.curLatLng, // Curlatlang will be used to calculate distances
nearbyPlacesInfo.placeList, nearbyPlacesInfo.placeList,
nearbyParentFragmentView.getContext(), nearbyParentFragmentView.getContext(),
bookmarkLocationDao.getAllBookmarksLocations()); bookmarkLocationDao.getAllBookmarksLocations());
nearbyParentFragmentView.updateMapMarkers(nearbyBaseMarkers, selectedMarker); nearbyParentFragmentView.updateMapMarkers(nearbyBaseMarkers, selectedMarker);
nearbyParentFragmentView.addCurrentLocationMarker(nearbyPlacesInfo.curLatLng);
if(shouldTrackPosition){ // nearbyParentFragmentView.addCurrentLocationMarker(nearbyPlacesInfo.curLatLng);
nearbyParentFragmentView.updateMapToTrackPosition(nearbyPlacesInfo.curLatLng);
}
lockUnlockNearby(false); // So that new location updates wont come lockUnlockNearby(false); // So that new location updates wont come
nearbyParentFragmentView.setProgressBarVisibility(false); nearbyParentFragmentView.setProgressBarVisibility(false);
nearbyParentFragmentView.updateListFragment(nearbyPlacesInfo.placeList); nearbyParentFragmentView.updateListFragment(nearbyPlacesInfo.placeList);
handleCenteringTaskIfAny(); handleCenteringTaskIfAny();
//TODO
nearbyParentFragmentView.centerMapToPosition(nearbyPlacesInfo.searchLatLng); // if (!shouldTrackPosition) {
// nearbyParentFragmentView.centerMapToPosition(nearbyPlacesInfo.searchLatLng);
// }
} }
} }
@ -370,8 +375,8 @@ public class NearbyParentFragmentPresenter
public View.OnClickListener onSearchThisAreaClicked() { public View.OnClickListener onSearchThisAreaClicked() {
return v -> { return v -> {
// Lock map operations during search this area operation // Lock map operations during search this area operation
// nearbyParentFragmentView.setMapCenter();
nearbyParentFragmentView.setSearchThisAreaButtonVisibility(false); nearbyParentFragmentView.setSearchThisAreaButtonVisibility(false);
if (searchCloseToCurrentLocation()){ if (searchCloseToCurrentLocation()){
updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED); updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED);
} else { } else {
@ -386,12 +391,19 @@ public class NearbyParentFragmentPresenter
* @return Returns true if search this area button is used around our current location * @return Returns true if search this area button is used around our current location
*/ */
public boolean searchCloseToCurrentLocation() { public boolean searchCloseToCurrentLocation() {
if (null == nearbyParentFragmentView.getLastFocusLocation()) { if (null == nearbyParentFragmentView.getLastMapFocus()) {
return true; return true;
} }
double distance = LocationUtils.commonsLatLngToMapBoxLatLng(nearbyParentFragmentView.getCameraTarget()) //TODO
.distanceTo(nearbyParentFragmentView.getLastFocusLocation()); Location mylocation = new Location("");
if (distance > NearbyController.currentLocationSearchRadius * 3 / 4) { Location dest_location = new Location("");
dest_location.setLatitude(nearbyParentFragmentView.getMapFocus().getLatitude());
dest_location.setLongitude(nearbyParentFragmentView.getMapFocus().getLongitude());
mylocation.setLatitude(nearbyParentFragmentView.getLastMapFocus().getLatitude());
mylocation.setLongitude(nearbyParentFragmentView.getLastMapFocus().getLongitude());
Float distance = mylocation.distanceTo(dest_location);
if (distance > 2000.0* 3 / 4) {
return false; return false;
} else { } else {
return true; return true;