From 2762f406de6b9b3da510fe40fd88b94436d84033 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Tue, 26 Sep 2017 18:03:47 +0300 Subject: [PATCH] Add distance to list item --- .../plus/mapmarkers/PlanRouteFragment.java | 71 ++++++++++++++++++- .../adapters/MapMarkersListAdapter.java | 22 ++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java index 64d08cc370..5992c29004 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/PlanRouteFragment.java @@ -20,12 +20,16 @@ import android.widget.TextView; import android.widget.Toast; import net.osmand.AndroidUtils; +import net.osmand.Location; +import net.osmand.data.LatLon; import net.osmand.plus.ApplicationMode; import net.osmand.plus.IconsCache; import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper.MapMarker; +import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.base.MapViewTrackingUtilities; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.mapmarkers.adapters.MapMarkersItemTouchHelperCallback; import net.osmand.plus.mapmarkers.adapters.MapMarkersListAdapter; @@ -40,7 +44,7 @@ import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControll import static net.osmand.plus.OsmandSettings.LANDSCAPE_MIDDLE_RIGHT_CONSTANT; import static net.osmand.plus.OsmandSettings.MIDDLE_TOP_CONSTANT; -public class PlanRouteFragment extends Fragment { +public class PlanRouteFragment extends Fragment implements OsmAndLocationListener { public static final String TAG = "PlanRouteFragment"; @@ -52,6 +56,9 @@ public class PlanRouteFragment extends Fragment { private int previousMapPosition; private int selectedCount = 0; + private Location location; + private boolean locationUpdateStarted; + private boolean nightMode; private boolean portrait; private boolean markersListOpened; @@ -204,6 +211,18 @@ public class PlanRouteFragment extends Fragment { return view; } + @Override + public void onResume() { + super.onResume(); + startLocationUpdate(); + } + + @Override + public void onPause() { + super.onPause(); + stopLocationUpdate(); + } + @Override public void onDestroyView() { super.onDestroyView(); @@ -213,6 +232,18 @@ public class PlanRouteFragment extends Fragment { } } + @Override + public void updateLocation(Location location) { + boolean newLocation = this.location == null && location != null; + boolean locationChanged = this.location != null && location != null + && this.location.getLatitude() != location.getLatitude() + && this.location.getLongitude() != location.getLongitude(); + if (newLocation || locationChanged) { + this.location = location; + updateLocationUi(); + } + } + private MapActivity getMapActivity() { return (MapActivity) getActivity(); } @@ -350,6 +381,27 @@ public class PlanRouteFragment extends Fragment { } } + + private void updateLocationUi() { + final MapActivity mapActivity = (MapActivity) getActivity(); + if (mapActivity != null && adapter != null) { + mapActivity.getMyApplication().runInUIThread(new Runnable() { + @Override + public void run() { + if (location == null) { + location = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation(); + } + MapViewTrackingUtilities utilities = mapActivity.getMapViewTrackingUtilities(); + boolean useCenter = !(utilities.isMapLinkedToLocation() && location != null); + + adapter.setUseCenter(useCenter); + adapter.setLocation(useCenter ? mapActivity.getMapLocation() : new LatLon(location.getLatitude(), location.getLongitude())); + adapter.notifyDataSetChanged(); + } + }); + } + } + private void mark(int status, int... widgets) { MapActivity mapActivity = getMapActivity(); if (mapActivity != null) { @@ -434,6 +486,23 @@ public class PlanRouteFragment extends Fragment { } } + private void startLocationUpdate() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null && !locationUpdateStarted) { + locationUpdateStarted = true; + mapActivity.getMyApplication().getLocationProvider().addLocationListener(this); + updateLocationUi(); + } + } + + private void stopLocationUpdate() { + MapActivity mapActivity = getMapActivity(); + if (mapActivity != null && locationUpdateStarted) { + locationUpdateStarted = false; + mapActivity.getMyApplication().getLocationProvider().removeLocationListener(this); + } + } + public boolean quit(boolean hideMarkersListFirst) { if (markersListOpened && hideMarkersListFirst) { hideMarkersList(); diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersListAdapter.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersListAdapter.java index b9dce0856a..c69ca5c0ae 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersListAdapter.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersListAdapter.java @@ -8,10 +8,13 @@ import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; +import net.osmand.data.LatLon; import net.osmand.plus.IconsCache; import net.osmand.plus.MapMarkersHelper.MapMarker; +import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; +import net.osmand.util.MapUtils; import java.text.SimpleDateFormat; import java.util.Collections; @@ -26,10 +29,21 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter markers; private MapMarkersListAdapterListener listener; + private LatLon location; + private boolean useCenter; + public void setAdapterListener(MapMarkersListAdapterListener listener) { this.listener = listener; } + public void setLocation(LatLon location) { + this.location = location; + } + + public void setUseCenter(boolean useCenter) { + this.useCenter = useCenter; + } + public MapMarkersListAdapter(MapActivity mapActivity) { this.mapActivity = mapActivity; markers = mapActivity.getMyApplication().getMapMarkersHelper().getMapMarkers(); @@ -116,6 +130,14 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter