mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-28 13:23:58 +01:00
Initialize map operations if map ready and tab is selected
This commit is contained in:
parent
d858813a01
commit
a396821c2c
3 changed files with 161 additions and 6 deletions
|
|
@ -30,6 +30,9 @@ import fr.free.nrw.commons.location.LocationServiceManager;
|
||||||
import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract;
|
import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract;
|
||||||
import fr.free.nrw.commons.nearby.mvp.presenter.NearbyParentFragmentPresenter;
|
import fr.free.nrw.commons.nearby.mvp.presenter.NearbyParentFragmentPresenter;
|
||||||
import fr.free.nrw.commons.utils.PermissionUtils;
|
import fr.free.nrw.commons.utils.PermissionUtils;
|
||||||
|
import io.reactivex.Observable;
|
||||||
|
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||||
|
import io.reactivex.schedulers.Schedulers;
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
import static fr.free.nrw.commons.contributions.ContributionsFragment.CONTRIBUTION_LIST_FRAGMENT_TAG;
|
import static fr.free.nrw.commons.contributions.ContributionsFragment.CONTRIBUTION_LIST_FRAGMENT_TAG;
|
||||||
|
|
@ -41,6 +44,9 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple
|
||||||
@Inject
|
@Inject
|
||||||
LocationServiceManager locationManager;
|
LocationServiceManager locationManager;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
NearbyController nearbyController;
|
||||||
|
|
||||||
NearbyParentFragmentPresenter nearbyParentFragmentPresenter;
|
NearbyParentFragmentPresenter nearbyParentFragmentPresenter;
|
||||||
SupportMapFragment mapFragment;
|
SupportMapFragment mapFragment;
|
||||||
|
|
||||||
|
|
@ -153,7 +159,27 @@ public class NearbyTestLayersFragment extends CommonsDaggerSupportFragment imple
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void populatePlaces(fr.free.nrw.commons.location.LatLng curlatLng, fr.free.nrw.commons.location.LatLng searchLatLng) {
|
public void populatePlaces(fr.free.nrw.commons.location.LatLng curlatLng, fr.free.nrw.commons.location.LatLng searchLatLng) {
|
||||||
|
compositeDisposable.add(Observable.fromCallable(() -> nearbyController
|
||||||
|
.loadAttractionsFromLocation(curlatLng, searchLatLng, false, true))
|
||||||
|
.subscribeOn(Schedulers.io())
|
||||||
|
.observeOn(AndroidSchedulers.mainThread())
|
||||||
|
.subscribe(this::updateMapMarkers,
|
||||||
|
throwable -> {
|
||||||
|
Timber.d(throwable);
|
||||||
|
//showErrorMessage(getString(R.string.error_fetching_nearby_places));
|
||||||
|
// TODO solve first unneeded method call here
|
||||||
|
//progressBar.setVisibility(View.GONE);
|
||||||
|
//nearbyParentFragmentPresenter.lockNearby(false);
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Populates places for custom location, should be used for finding nearby places around a
|
||||||
|
* location where you are not at.
|
||||||
|
* @param nearbyPlacesInfo This variable has place list information and distances.
|
||||||
|
*/
|
||||||
|
private void updateMapMarkers(NearbyController.NearbyPlacesInfo nearbyPlacesInfo) {
|
||||||
|
nearbyParentFragmentPresenter.updateMapMarkers(nearbyPlacesInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
||||||
package fr.free.nrw.commons.nearby;
|
package fr.free.nrw.commons.nearby;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.Color;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
@ -12,6 +14,12 @@ import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
import com.google.android.material.floatingactionbutton.FloatingActionButton;
|
||||||
|
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.maps.MapFragment;
|
import com.mapbox.mapboxsdk.maps.MapFragment;
|
||||||
import com.mapbox.mapboxsdk.maps.MapView;
|
import com.mapbox.mapboxsdk.maps.MapView;
|
||||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||||
|
|
@ -22,11 +30,17 @@ import com.mapbox.mapboxsdk.utils.MapFragmentUtils;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
import dagger.android.support.DaggerFragment;
|
import dagger.android.support.DaggerFragment;
|
||||||
|
import fr.free.nrw.commons.R;
|
||||||
|
import fr.free.nrw.commons.bookmarks.locations.BookmarkLocationsDao;
|
||||||
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
import fr.free.nrw.commons.di.CommonsDaggerSupportFragment;
|
||||||
import fr.free.nrw.commons.location.LatLng;
|
import fr.free.nrw.commons.location.LatLng;
|
||||||
import fr.free.nrw.commons.nearby.mvp.contract.NearbyMapContract;
|
import fr.free.nrw.commons.nearby.mvp.contract.NearbyMapContract;
|
||||||
import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract;
|
import fr.free.nrw.commons.nearby.mvp.contract.NearbyParentFragmentContract;
|
||||||
|
import fr.free.nrw.commons.utils.LocationUtils;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Support Fragment wrapper around a map view.
|
* Support Fragment wrapper around a map view.
|
||||||
|
|
@ -46,6 +60,9 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment
|
||||||
implements OnMapReadyCallback,
|
implements OnMapReadyCallback,
|
||||||
NearbyMapContract.View{
|
NearbyMapContract.View{
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
BookmarkLocationsDao bookmarkLocationDao;
|
||||||
|
|
||||||
private final List<OnMapReadyCallback> mapReadyCallbackList = new ArrayList<>();
|
private final List<OnMapReadyCallback> mapReadyCallbackList = new ArrayList<>();
|
||||||
private MapFragment.OnMapViewReadyCallback mapViewReadyCallback;
|
private MapFragment.OnMapViewReadyCallback mapViewReadyCallback;
|
||||||
private MapboxMap mapboxMap;
|
private MapboxMap mapboxMap;
|
||||||
|
|
@ -249,12 +266,30 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateMapMarkers(LatLng latLng, List<Place> placeList) {
|
public void updateMapMarkers(LatLng latLng, List<Place> placeList) {
|
||||||
|
Log.d("denemeTest","updateMapMarkers, curLatng:"+latLng);
|
||||||
|
List<NearbyBaseMarker> customBaseMarkerOptions = NearbyController
|
||||||
|
.loadAttractionsFromLocationToBaseMarkerOptions(latLng, // Curlatlang will be used to calculate distances
|
||||||
|
placeList,
|
||||||
|
getActivity(),
|
||||||
|
bookmarkLocationDao.getAllBookmarksLocations());
|
||||||
|
mapboxMap.clear();
|
||||||
|
// TODO: set search latlang here
|
||||||
|
CameraPosition cameraPosition = new CameraPosition.Builder().target
|
||||||
|
(LocationUtils.commonsLatLngToMapBoxLatLng(latLng)).build();
|
||||||
|
mapboxMap.setCameraPosition(cameraPosition);
|
||||||
|
/*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(latLng);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateMapToTrackPosition(LatLng curLatLng) {
|
public void updateMapToTrackPosition(LatLng curLatLng) {
|
||||||
|
addCurrentLocationMarker(curLatLng);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -267,11 +302,66 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
@Override
|
||||||
public void addCurrentLocationMarker(LatLng curLatLng) {
|
public void addCurrentLocationMarker(LatLng curLatLng) {
|
||||||
|
Log.d("denemeTest","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<com.mapbox.mapboxsdk.geometry.LatLng> 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<com.mapbox.mapboxsdk.geometry.LatLng> createCircleArray(
|
||||||
|
double centerLat, double centerLong, float radius, int nodes) {
|
||||||
|
List<com.mapbox.mapboxsdk.geometry.LatLng> 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
|
@Override
|
||||||
public void setSearchThisAreaButtonVisibility(boolean visible) {
|
public void setSearchThisAreaButtonVisibility(boolean visible) {
|
||||||
|
|
||||||
|
|
@ -283,8 +373,39 @@ public class SupportMapFragment extends CommonsDaggerSupportFragment
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void addNearbyMarkersToMapBoxMap(List<NearbyBaseMarker> baseMarkerOptions) {
|
public void addNearbyMarkersToMapBoxMap(@Nullable List<NearbyBaseMarker> baseMarkerList) {
|
||||||
|
Log.d("denemeTest","add markers to map");
|
||||||
|
mapboxMap.addMarkers(baseMarkerList);
|
||||||
|
mapboxMap.setOnInfoWindowCloseListener(marker -> {
|
||||||
|
/*if (marker == selected) {
|
||||||
|
bottomSheetDetailsBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
|
||||||
|
}*/
|
||||||
|
});
|
||||||
|
map.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
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,8 @@ public class NearbyParentFragmentPresenter
|
||||||
// We will know when we went offline and online again
|
// We will know when we went offline and online again
|
||||||
//nearbyParentFragmentView.addNetworkBroadcastReceiver();
|
//nearbyParentFragmentView.addNetworkBroadcastReceiver();
|
||||||
//nearbyMapFragmentView.setupMapView(null);
|
//nearbyMapFragmentView.setupMapView(null);
|
||||||
nearbyOperationsInitialized();
|
//nearbyOperationsInitialized();
|
||||||
|
initializeMapOperations();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -139,10 +140,11 @@ public class NearbyParentFragmentPresenter
|
||||||
}
|
}
|
||||||
|
|
||||||
public void initializeMapOperations() {
|
public void initializeMapOperations() {
|
||||||
Log.d("deneme2","initializeMapOperations");
|
Log.d("denemeTest","initializeMapOperations");
|
||||||
|
|
||||||
lockNearby(false);
|
lockNearby(false);
|
||||||
nearbyParentFragmentView.addNetworkBroadcastReceiver();
|
nearbyParentFragmentView.addNetworkBroadcastReceiver();
|
||||||
|
//TODO: NETWORK RECEIVER IS NOT ASSIGNED
|
||||||
|
|
||||||
Timber.d("Nearby map view is created and ready");
|
Timber.d("Nearby map view is created and ready");
|
||||||
updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED, null);
|
updateMapAndList(LOCATION_SIGNIFICANTLY_CHANGED, null);
|
||||||
|
|
@ -187,14 +189,16 @@ public class NearbyParentFragmentPresenter
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void updateMapAndList(LocationServiceManager.LocationChangeType locationChangeType, LatLng cameraTarget) {
|
public void updateMapAndList(LocationServiceManager.LocationChangeType locationChangeType, LatLng cameraTarget) {
|
||||||
|
Log.d("denemeTest","updateMapAndList");
|
||||||
if (isNearbyLocked) {
|
if (isNearbyLocked) {
|
||||||
|
Log.d("denemeTest","isNearbyLocked");
|
||||||
Timber.d("Nearby is locked, so updateMapAndList returns");
|
Timber.d("Nearby is locked, so updateMapAndList returns");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!nearbyParentFragmentView.isNetworkConnectionEstablished()) {
|
if (!nearbyParentFragmentView.isNetworkConnectionEstablished()) {
|
||||||
Timber.d("Network connection is not established");
|
Timber.d("Network connection is not established");
|
||||||
|
Log.d("denemeTest","nearbyParentFragmentView.isNetworkConnectionEstablished()");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -218,6 +222,7 @@ public class NearbyParentFragmentPresenter
|
||||||
*/
|
*/
|
||||||
if (locationChangeType.equals(LOCATION_SIGNIFICANTLY_CHANGED)
|
if (locationChangeType.equals(LOCATION_SIGNIFICANTLY_CHANGED)
|
||||||
|| locationChangeType.equals(MAP_UPDATED)) {
|
|| locationChangeType.equals(MAP_UPDATED)) {
|
||||||
|
Log.d("denemeTest","1");
|
||||||
nearbyParentFragmentView.populatePlaces(lastLocation, lastLocation);
|
nearbyParentFragmentView.populatePlaces(lastLocation, lastLocation);
|
||||||
nearbyParentFragmentView.setSearchThisAreaProgressVisibility(false);
|
nearbyParentFragmentView.setSearchThisAreaProgressVisibility(false);
|
||||||
//nearbyMapFragmentView.updateMapToTrackPosition(curLatLng);
|
//nearbyMapFragmentView.updateMapToTrackPosition(curLatLng);
|
||||||
|
|
@ -225,10 +230,13 @@ public class NearbyParentFragmentPresenter
|
||||||
// TODO dont forget map updated state after an wikidata item is updated
|
// TODO dont forget map updated state after an wikidata item is updated
|
||||||
|
|
||||||
} else if (locationChangeType.equals(SEARCH_CUSTOM_AREA)) {
|
} else if (locationChangeType.equals(SEARCH_CUSTOM_AREA)) {
|
||||||
|
Log.d("denemeTest","2");
|
||||||
|
|
||||||
nearbyParentFragmentView.populatePlaces(lastLocation, cameraTarget);
|
nearbyParentFragmentView.populatePlaces(lastLocation, cameraTarget);
|
||||||
nearbyParentFragmentView.setSearchThisAreaProgressVisibility(false);
|
nearbyParentFragmentView.setSearchThisAreaProgressVisibility(false);
|
||||||
searchingThisArea = false;
|
searchingThisArea = false;
|
||||||
} else { // Means location changed slightly, ie user is walking or driving.
|
} else { // Means location changed slightly, ie user is walking or driving.
|
||||||
|
Log.d("denemeTest","3");
|
||||||
nearbyMapFragmentView.updateMapToTrackPosition(curLatLng);
|
nearbyMapFragmentView.updateMapToTrackPosition(curLatLng);
|
||||||
searchingThisArea = false;
|
searchingThisArea = false;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue