From fd9cb37fb318efeee808cf3b664e83776d0f5494 Mon Sep 17 00:00:00 2001 From: Adam Domagalski Date: Sat, 17 Nov 2018 16:30:52 +0100 Subject: [PATCH] bound area improvment, toggle bounding box in menu fab --- .../findmytutor/activity/BaseActivity.java | 1 - .../wmi/findmytutor/activity/MapActivity.java | 75 +++++++++++-------- .../uam/wmi/findmytutor/utils/mapUtils.java | 67 ++++++++++++----- app/src/main/res/layout/activity_map.xml | 8 +- app/src/main/res/values-pl/strings.xml | 2 +- app/src/main/res/values/strings.xml | 2 +- 6 files changed, 96 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java index 2f4cba0..62e13a9 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/BaseActivity.java @@ -258,7 +258,6 @@ public abstract class BaseActivity } @Override - public boolean onQueryTextChange(String input) { if (activeFragment.equals(ActiveFragment.USER_LIST)) { executeUserListSearch(input); diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java index 188d98d..370b2ed 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/MapActivity.java @@ -10,16 +10,16 @@ import android.graphics.Color; import android.location.Location; import android.os.Bundle; import android.os.Handler; -import android.support.design.widget.FloatingActionButton; + +import com.getbase.floatingactionbutton.FloatingActionButton; + import android.support.v4.content.ContextCompat; import android.support.v7.app.AlertDialog; import android.util.Log; -import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.animation.LinearInterpolator; import android.widget.Button; -import android.widget.FrameLayout; import android.widget.TextView; import android.widget.Toast; @@ -31,11 +31,7 @@ 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.geometry.LatLng; -import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.location.LocationComponent; import com.mapbox.mapboxsdk.location.LocationComponentOptions; import com.mapbox.mapboxsdk.location.modes.CameraMode; @@ -67,6 +63,7 @@ import io.reactivex.observers.DisposableSingleObserver; import io.reactivex.schedulers.Schedulers; import okhttp3.ResponseBody; import timber.log.Timber; + import java.util.Set; import static com.mapbox.mapboxsdk.style.layers.Property.NONE; @@ -125,6 +122,7 @@ public class MapActivity extends BaseActivity Bundle extras = getIntent().getExtras(); + Mapbox.getInstance(this, getString(R.string.access_token)); mapView = findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); @@ -145,12 +143,29 @@ public class MapActivity extends BaseActivity return true; }); - mapUtils.setMapBoundsArea(getApplicationContext() , mapboxMap, mapView); + setToggleMapBoundsArea(); setOnMapLongClickListener(); // addStaticLayer(); } + private void setToggleMapBoundsArea() { + + mapUtils.setMapBoundsArea(getApplicationContext(), mapboxMap, mapView, true); + + FloatingActionButton toggleBoundsAreaFab = findViewById(R.id.fab_toggle_bound_area); + toggleBoundsAreaFab.setOnClickListener(view -> { + if (toggleBoundsAreaFab.getTitle().equals("Bounds OFF")) { + toggleBoundsAreaFab.setTitle("Bounds ON"); + mapUtils.setMapBoundsArea(getApplicationContext(), mapboxMap, mapView, false); + } else { + toggleBoundsAreaFab.setTitle("Bounds OFF"); + mapUtils.setMapBoundsArea(getApplicationContext(), mapboxMap, mapView, true); + } + + }); + } + private void createMarkerModal(String userId) { disposable.add(userService.getUserById(userId) .subscribeOn(Schedulers.io()) @@ -333,7 +348,7 @@ public class MapActivity extends BaseActivity @Override public void onSuccess(List coordsList) { - if(coordsList.isEmpty()) { + if (coordsList.isEmpty()) { Log.e(tag, "200 empty []"); //mapboxMap.clear(); return; @@ -346,26 +361,24 @@ public class MapActivity extends BaseActivity } Set currentCoordsIds = new HashSet<>(tmp); - if (previousCoordsIds.isEmpty()){ + if (previousCoordsIds.isEmpty()) { previousCoordsIds.addAll(currentCoordsIds); } else { // here we clear + it returns bool if smthing was removed if (previousCoordsIds.removeAll(currentCoordsIds)) { - for (String toRemoveId: previousCoordsIds) { - Log.e(tag+ "delete: " , "removing: " + toRemoveId + ": " + markerHash.get(toRemoveId)); + for (String toRemoveId : previousCoordsIds) { + Log.e(tag + "delete: ", "removing: " + toRemoveId + ": " + markerHash.get(toRemoveId)); mapboxMap.removeMarker(markerHash.get(toRemoveId)); markerHash.remove(toRemoveId); coordsMap.remove(toRemoveId); } } else { // TODO double check when some markers api will change - Log.e(tag+ "delete: ","nothing to remove"); + Log.e(tag + "delete: ", "nothing to remove"); } } - - for (Coordinate element : coordsList) { String id = element.getUserId(); String newLabel = element.getLabel(); @@ -447,44 +460,42 @@ public class MapActivity extends BaseActivity if (PermissionsManager.areLocationPermissionsGranted(this)) { Log.e(tag, "enableLocationPlugin true"); - FloatingActionButton myLocationButton = findViewById(R.id.myLocationButton); - myLocationButton.setVisibility(View.VISIBLE); LocationComponentOptions options = LocationComponentOptions.builder(this) - .trackingGesturesManagement(true) + .trackingGesturesManagement(false) .accuracyColor(ContextCompat.getColor(this, R.color.mapboxGray)) .build(); // Get an instance of the component locationComponent = mapboxMap.getLocationComponent(); - //Log.e(tag + "Last", locationComponent.getLastKnownLocation() + ""); + Log.e(tag + "Last", locationComponent.getLastKnownLocation() + ""); // Activate with options locationComponent.activateLocationComponent(this, options); - // Enable to make component visible + + // Enable to make component visible + camera animation + // https://www.mapbox.com/android-docs/maps/overview/location-component/ locationComponent.setLocationComponentEnabled(true); // Set the component's camera mode - locationComponent.setCameraMode(CameraMode.TRACKING); + locationComponent.setCameraMode(CameraMode.NONE); locationComponent.setRenderMode(RenderMode.COMPASS); // Button 4 centring FloatingActionButton myLocFAB = findViewById(R.id.myLocationButton); + myLocFAB.setVisibility(View.VISIBLE); myLocFAB.setOnClickListener(v -> { Location lastKnownLocation = locationComponent.getLastKnownLocation(); if (lastKnownLocation != null) { - - CameraPosition position = new CameraPosition.Builder() - .target(new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude())) // Sets the new camera position - .zoom(zoomParam) // Sets the zoom - .bearing(bearingParam) // Rotate the camera - .tilt(tiltParam) // Set the camera tilt - .build(); // Creates a CameraPosition from the builder - - mapboxMap.animateCamera(CameraUpdateFactory - .newCameraPosition(position), 4000); + mapUtils.makeNewCamera(mapboxMap, + lastKnownLocation.getLatitude(), + lastKnownLocation.getLongitude(), + zoomParam, + bearingParam, + tiltParam, + 4000); } // Camera aniamtion @@ -588,7 +599,7 @@ public class MapActivity extends BaseActivity if (e instanceof HttpException) { ResponseBody responseBody = ((HttpException) e).response().errorBody(); message = RestApiHelper.getErrorMessage(responseBody); - }else { + } else { message = "Network Error!"; } diff --git a/app/src/main/java/com/uam/wmi/findmytutor/utils/mapUtils.java b/app/src/main/java/com/uam/wmi/findmytutor/utils/mapUtils.java index 0ca9381..10a651b 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/utils/mapUtils.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/utils/mapUtils.java @@ -7,11 +7,16 @@ import android.view.Gravity; import android.view.View; import android.widget.FrameLayout; +import com.mapbox.mapboxsdk.annotations.Polygon; import com.mapbox.mapboxsdk.annotations.PolygonOptions; +import com.mapbox.mapboxsdk.camera.CameraPosition; +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.geometry.LatLngBounds; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.style.layers.FillLayer; +import com.mapbox.mapboxsdk.style.layers.Layer; import java.io.IOException; import java.io.InputStream; @@ -24,30 +29,54 @@ public class mapUtils { .include(new LatLng(52.46548540224137, 16.925255680881094)) .build(); + private static final PolygonOptions boundsArea = new PolygonOptions() + .add(WMI_BOUNDS.getNorthWest()) + .add(WMI_BOUNDS.getNorthEast()) + .add(WMI_BOUNDS.getSouthEast()) + .add(WMI_BOUNDS.getSouthWest()) + .alpha(0.1f) + .fillColor(Color.YELLOW); + // For adding and removing + private static Polygon polygon; + private static View crosshair; + // Map Bounds Area - public static void setMapBoundsArea(Context context , MapboxMap mapboxMap, MapView mapView) { + public static void setMapBoundsArea(Context context, MapboxMap mapboxMap, MapView mapView, Boolean check) { - // Set bounds to WMI - mapboxMap.setLatLngBoundsForCameraTarget(WMI_BOUNDS); - mapboxMap.setMinZoomPreference(16); // TODO export to map config + if (check) { + // Set bounds to WMI + mapboxMap.setLatLngBoundsForCameraTarget(WMI_BOUNDS); + makeNewCamera(mapboxMap, 52.466799, 16.927002, 17, 0, 0,4000); + mapboxMap.setMinZoomPreference(16); // TODO export to map config - // Visualise bounds area - // showBoundsArea - PolygonOptions boundsArea = new PolygonOptions() - .add(WMI_BOUNDS.getNorthWest()) - .add(WMI_BOUNDS.getNorthEast()) - .add(WMI_BOUNDS.getSouthEast()) - .add(WMI_BOUNDS.getSouthWest()); - boundsArea.alpha(0.1f); - boundsArea.fillColor(Color.YELLOW); - mapboxMap.addPolygon(boundsArea); + // Visualise bounds area + // showBoundsArea + polygon = mapboxMap.addPolygon(boundsArea); + // showCrosshair + crosshair = new View(context); + crosshair.setLayoutParams(new FrameLayout.LayoutParams(15, 15, Gravity.CENTER)); + crosshair.setBackgroundColor(Color.GREEN); + mapView.addView(crosshair); + } else { + mapboxMap.setLatLngBoundsForCameraTarget(null); + mapboxMap.setMinZoomPreference(2); + mapboxMap.removePolygon(polygon); + mapView.removeView(crosshair); + } - // showCrosshair - View crosshair = new View(context); - crosshair.setLayoutParams(new FrameLayout.LayoutParams(15, 15, Gravity.CENTER)); - crosshair.setBackgroundColor(Color.GREEN); - mapView.addView(crosshair); + } + + public static void makeNewCamera(MapboxMap mapboxMap, double lat, double lon, int zoomParam, int bearingParam, int tiltParam, int duration){ + CameraPosition position = new CameraPosition.Builder() + .target(new LatLng(lat, lon)) // Sets the new camera position + .zoom(zoomParam) // Sets the zoom + .bearing(bearingParam) // Rotate the camera + .tilt(tiltParam) // Set the camera tilt + .build(); // Creates a CameraPosition from the builder + + mapboxMap.animateCamera(CameraUpdateFactory + .newCameraPosition(position), duration); } // Function for marker animation diff --git a/app/src/main/res/layout/activity_map.xml b/app/src/main/res/layout/activity_map.xml index f1f614e..dd40aea 100644 --- a/app/src/main/res/layout/activity_map.xml +++ b/app/src/main/res/layout/activity_map.xml @@ -42,13 +42,11 @@ mapbox:srcCompat="@android:drawable/ic_dialog_map" tools:layout_editor_absoluteX="0dp" /> - + fab:fab_title="@string/fab_title_bound_area" /> diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 60d4559..2c4cc2b 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -43,7 +43,6 @@ Hotele Parkingi - Atrakcje FindMyTutor Find My Tutor @@ -128,6 +127,7 @@ Ta aplikacja potrzebuje uprawnień do lokalizacji. Ustawienia statusu Ręczny wybór lokalizacji + Bounds OFF diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index b9c281d..b45da5c 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -170,7 +170,7 @@ TODO THIS - TOO + Bounds OFF Select a location This app needs location permissions in order to show its functionality. You didn\'t grant location permissions.