diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMarker.java b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMarker.java index a552579c9..680b7ecc9 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMarker.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/NearbyMarker.java @@ -2,7 +2,7 @@ package fr.free.nrw.commons.nearby; import com.mapbox.mapboxsdk.annotations.Marker; -class NearbyMarker extends Marker { +public class NearbyMarker extends Marker { private final Place place; private NearbyBaseMarker nearbyBaseMarker; @@ -11,13 +11,13 @@ class NearbyMarker extends Marker { * * @param baseMarkerOptions The builder used to construct the Marker. */ - NearbyMarker(NearbyBaseMarker baseMarkerOptions, Place place) { + public NearbyMarker(NearbyBaseMarker baseMarkerOptions, Place place) { super(baseMarkerOptions); this.place = place; this.nearbyBaseMarker = baseMarkerOptions; } - NearbyBaseMarker getNearbyBaseMarker() { + public NearbyBaseMarker getNearbyBaseMarker() { return nearbyBaseMarker; } diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyMapContract.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyMapContract.java index d823033ce..9c6759955 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyMapContract.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/contract/NearbyMapContract.java @@ -26,10 +26,10 @@ public interface NearbyMapContract { void showInformationBottomSheet(); void initViews(); void updateMapMarkers(LatLng latLng, List placeList); - void updateMapToTrackPosition(); + void updateMapToTrackPosition(LatLng curLatLng); void setListeners(); MapView setupMapView(Bundle savedInstanceState); - void addCurrentLocationMarker(); + void addCurrentLocationMarker(LatLng curLatLng); void setSearchThisAreaButtonVisibility(boolean visible); boolean isCurrentLocationMarkerVisible(); void addNearbyMarkersToMapBoxMap(List baseMarkerOptions); diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyMapFragment.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyMapFragment.java index 376dcd7b7..f2fe6073d 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyMapFragment.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/fragments/NearbyMapFragment.java @@ -1,6 +1,7 @@ package fr.free.nrw.commons.nearby.mvp.fragments; import android.content.Context; +import android.graphics.Color; import android.net.Uri; import android.os.Bundle; import android.util.Log; @@ -16,6 +17,11 @@ import com.google.android.material.bottomsheet.BottomSheetBehavior; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.mapbox.mapboxsdk.Mapbox; import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions; +import com.mapbox.mapboxsdk.annotations.Icon; +import com.mapbox.mapboxsdk.annotations.IconFactory; +import com.mapbox.mapboxsdk.annotations.Marker; +import com.mapbox.mapboxsdk.annotations.MarkerOptions; +import com.mapbox.mapboxsdk.annotations.PolygonOptions; import com.mapbox.mapboxsdk.camera.CameraPosition; import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.constants.Style; @@ -24,6 +30,7 @@ import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMapOptions; import com.mapbox.mapboxsdk.plugins.localization.LocalizationPlugin; +import java.util.ArrayList; import java.util.List; import javax.inject.Inject; @@ -40,6 +47,7 @@ import fr.free.nrw.commons.kvstore.JsonKvStore; import fr.free.nrw.commons.location.LatLng; import fr.free.nrw.commons.nearby.NearbyBaseMarker; import fr.free.nrw.commons.nearby.NearbyController; +import fr.free.nrw.commons.nearby.NearbyMarker; import fr.free.nrw.commons.nearby.Place; import fr.free.nrw.commons.nearby.mvp.contract.NearbyMapContract; import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract; @@ -136,7 +144,7 @@ public class NearbyMapFragment extends CommonsDaggerSupportFragment implements N } return false; }); - viewsAreReadyCallback.nearbyFragmentAndMapViewReady(); + //viewsAreReadyCallback.nearbyFragmentAndMapViewReady(); } @Override @@ -174,6 +182,7 @@ public class NearbyMapFragment extends CommonsDaggerSupportFragment implements N } this.mapboxMap = mapboxMap; + viewsAreReadyCallback.nearbyFragmentAndMapViewReady(); //addMapMovementListeners(); //updateMapSignificantlyForCurrentLocation(); }); @@ -205,18 +214,20 @@ public class NearbyMapFragment extends CommonsDaggerSupportFragment implements N CameraPosition cameraPosition = new CameraPosition.Builder().target (LocationUtils.commonsLatLngToMapBoxLatLng(curLatLng)).build(); mapboxMap.setCameraPosition(cameraPosition); - mapboxMap.animateCamera(CameraUpdateFactory - .newCameraPosition(cameraPosition), 1000); + /*mapboxMap.animateCamera(CameraUpdateFactory + .newCameraPosition(cameraPosition), 1000);*/ // TODO: set position depening to botom sheet position heere // We are trying to find nearby places around our custom searched area, thus custom parameter is nonnull addNearbyMarkersToMapBoxMap(customBaseMarkerOptions); // Re-enable mapbox gestures on custom location markers load mapboxMap.getUiSettings().setAllGesturesEnabled(true); + updateMapToTrackPosition(curLatLng); } @Override - public void updateMapToTrackPosition() { - //addCurrentLocationMarker(mapboxMap); + public void updateMapToTrackPosition(LatLng curLatLng) { + Log.d("deneme1","updateMapToTrackPosition"); + addCurrentLocationMarker(curLatLng); } @@ -224,12 +235,65 @@ public class NearbyMapFragment extends CommonsDaggerSupportFragment implements N public void setListeners() { } - + /** + * Adds a marker for the user's current position. Adds a + * circle which uses the accuracy * 2, to draw a circle + * which represents the user's position with an accuracy + * of 95%. + * + * Should be called only on creation of mapboxMap, there + * is other method to update markers location with users + * move. + */ @Override - public void addCurrentLocationMarker() { + public void addCurrentLocationMarker(LatLng curLatLng) { + Log.d("deneme1","addCurrentLocationMarker"); + Timber.d("addCurrentLocationMarker is called"); + Icon icon = IconFactory.getInstance(getContext()).fromResource(R.drawable.current_location_marker); + + MarkerOptions currentLocationMarkerOptions = new MarkerOptions() + .position(new com.mapbox.mapboxsdk.geometry.LatLng(curLatLng.getLatitude(), curLatLng.getLongitude())); + currentLocationMarkerOptions.setIcon(icon); // Set custom icon + + Marker currentLocationMarker = mapboxMap.addMarker(currentLocationMarkerOptions); + + List circle = createCircleArray(curLatLng.getLatitude(), curLatLng.getLongitude(), + curLatLng.getAccuracy() * 2, 100); + + PolygonOptions currentLocationPolygonOptions = new PolygonOptions() + .addAll(circle) + .strokeColor(Color.parseColor("#55000000")) + .fillColor(Color.parseColor("#11000000")); + mapboxMap.addPolygon(currentLocationPolygonOptions); } + //TODO: go to util + /** + * Creates a series of points that create a circle on the map. + * Takes the center latitude, center longitude of the circle, + * the radius in meter and the number of nodes of the circle. + * + * @return List List of LatLng points of the circle. + */ + private List createCircleArray( + double centerLat, double centerLong, float radius, int nodes) { + List circle = new ArrayList<>(); + float radiusKilometer = radius / 1000; + double radiusLong = radiusKilometer + / (111.320 * Math.cos(centerLat * Math.PI / 180)); + double radiusLat = radiusKilometer / 110.574; + + for (int i = 0; i < nodes; i++) { + double theta = ((double) i / (double) nodes) * (2 * Math.PI); + double nodeLongitude = centerLong + radiusLong * Math.cos(theta); + double nodeLatitude = centerLat + radiusLat * Math.sin(theta); + circle.add(new com.mapbox.mapboxsdk.geometry.LatLng(nodeLatitude, nodeLongitude)); + } + return circle; + } + + @Override public void setSearchThisAreaButtonVisibility(boolean visible) { @@ -240,9 +304,42 @@ public class NearbyMapFragment extends CommonsDaggerSupportFragment implements N return false; } - @Override - public void addNearbyMarkersToMapBoxMap(List baseMarkerOptions) { + /** + * Adds markers for nearby places to mapbox map + */ + public void addNearbyMarkersToMapBoxMap(@Nullable List baseMarkerList) { + Timber.d("addNearbyMarkersToMapBoxMap is called"); + mapboxMap.addMarkers(baseMarkerList); + mapboxMap.setOnInfoWindowCloseListener(marker -> { + /*if (marker == selected) { + bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); + }*/ + }); + mapView.getMapAsync(mapboxMap -> { + mapboxMap.addMarkers(baseMarkerList); + //fabRecenter.setVisibility(View.VISIBLE); + mapboxMap.setOnInfoWindowCloseListener(marker -> { + /*if (marker == selected) { + bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); + }*/ + }); + + mapboxMap.setOnMarkerClickListener(marker -> { + + if (marker instanceof NearbyMarker) { + //this.selected = marker; + NearbyMarker nearbyMarker = (NearbyMarker) marker; + Place place = nearbyMarker.getNearbyBaseMarker().getPlace(); + passInfoToSheet(place); + bottomSheetListBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); + bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); + + } + return false; + }); + + }); } @Override diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java index 8b22e6803..c3e79a901 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/mvp/presenter/NearbyParentFragmentPresenter.java @@ -155,7 +155,7 @@ public class NearbyParentFragmentPresenter // TODO add a search location here } else { - nearbyMapFragmentView.updateMapToTrackPosition(); + nearbyMapFragmentView.updateMapToTrackPosition(curLatLng); } } @@ -165,8 +165,9 @@ public class NearbyParentFragmentPresenter * @param nearbyPlacesInfo This variable has place list information and distances. */ public void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo) { + Log.d("deneme","updateMapMarkers npfp"); nearbyMapFragmentView.updateMapMarkers(nearbyPlacesInfo.curLatLng, nearbyPlacesInfo.placeList); - nearbyMapFragmentView.updateMapToTrackPosition(); + nearbyMapFragmentView.updateMapToTrackPosition(nearbyPlacesInfo.curLatLng); } @Override diff --git a/app/src/main/res/layout/fragment_nearby.xml b/app/src/main/res/layout/fragment_nearby.xml index e6e5f8d37..0baed1ff6 100644 --- a/app/src/main/res/layout/fragment_nearby.xml +++ b/app/src/main/res/layout/fragment_nearby.xml @@ -37,6 +37,7 @@ android:layout_below="@id/toolbar" android:layout_alignParentLeft="true" android:background="#aa969696" + android:visibility="gone" android:elevation="6dp">