Show the distance between nearby markers

This commit is contained in:
Alexander Sytnyk 2017-10-03 15:04:30 +03:00
parent 1a81bc9144
commit 814498eda9
2 changed files with 54 additions and 109 deletions

View file

@ -33,12 +33,10 @@ import net.osmand.plus.IconsCache;
import net.osmand.plus.MapMarkersHelper;
import net.osmand.plus.MapMarkersHelper.MapMarker;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
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.PlanRouteSortByBottomSheetDialogFragment.PlanRouteSortByFragmentListener;
import net.osmand.plus.mapmarkers.adapters.MapMarkersItemTouchHelperCallback;
@ -57,7 +55,7 @@ import java.util.List;
import static net.osmand.plus.OsmandSettings.LANDSCAPE_MIDDLE_RIGHT_CONSTANT;
public class PlanRouteFragment extends Fragment implements OsmAndLocationListener {
public class PlanRouteFragment extends Fragment {
public static final String TAG = "PlanRouteFragment";
@ -71,14 +69,10 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
private int toolbarHeight;
private Location location;
private boolean locationUpdateStarted;
private boolean nightMode;
private boolean portrait;
private boolean markersListOpened;
private boolean wasCollapseButtonVisible;
private boolean uiUpdateAllowed = true;
private View mainView;
private RecyclerView markersRv;
@ -207,6 +201,7 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
}
adapter = new MapMarkersListAdapter(mapActivity);
adapter.setHasStableIds(true);
adapter.calculateStartAndFinishPos();
final ItemTouchHelper touchHelper = new ItemTouchHelper(new MapMarkersItemTouchHelperCallback(adapter));
touchHelper.attachToRecyclerView(markersRv);
@ -229,8 +224,8 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
marker.selected = !marker.selected;
markersHelper.updateMapMarker(marker, false);
}
adapter.updateStartAndFinish();
adapter.notifyItemChanged(pos);
adapter.calculateStartAndFinishPos();
adapter.notifyDataSetChanged();
updateSelectButton();
updateText();
showMarkersRouteOnMap();
@ -238,7 +233,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
@Override
public void onDragStarted(RecyclerView.ViewHolder holder) {
uiUpdateAllowed = false;
fromPosition = holder.getAdapterPosition();
touchHelper.startDrag(holder);
}
@ -260,7 +254,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
updateText();
showMarkersRouteOnMap();
}
uiUpdateAllowed = true;
}
});
boolean isSmartphone = getResources().getConfiguration().smallestScreenWidthDp < 600;
@ -272,7 +265,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
uiUpdateAllowed = newState == RecyclerView.SCROLL_STATE_IDLE;
}
});
@ -307,18 +299,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
return view;
}
@Override
public void onResume() {
super.onResume();
startLocationUpdate();
}
@Override
public void onPause() {
super.onPause();
stopLocationUpdate();
}
@Override
public void onDestroyView() {
super.onDestroyView();
@ -328,18 +308,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
}
}
@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();
}
@ -536,29 +504,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
}
}
private void updateLocationUi() {
if (!uiUpdateAllowed) {
return;
}
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) {
@ -639,23 +584,6 @@ public class PlanRouteFragment extends Fragment implements OsmAndLocationListene
}
}
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);
}
}
private void showMarkersRouteOnMap() {
MapActivity mapActivity = getMapActivity();
if (mapActivity != null) {

View file

@ -8,6 +8,7 @@ import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import net.osmand.Location;
import net.osmand.data.LatLon;
import net.osmand.plus.IconsCache;
import net.osmand.plus.MapMarkersHelper.MapMarker;
@ -26,28 +27,20 @@ import java.util.Locale;
public class MapMarkersListAdapter extends RecyclerView.Adapter<MapMarkerItemViewHolder>
implements MapMarkersItemTouchHelperCallback.ItemTouchHelperAdapter {
private static final int LOCATION_ITEM_ID = 0;
private MapActivity mapActivity;
private List<MapMarker> markers;
private MapMarkersListAdapterListener listener;
private LatLon location;
private boolean useCenter;
private int startPos = -1;
private int finishPos = -1;
private int firstSelectedMarkerPos = -1;
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();
@ -72,9 +65,14 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter<MapMarkerItemVie
IconsCache iconsCache = app.getIconsCache();
boolean locationItem = pos == 0;
boolean firstMarkerItem = pos == 1;
boolean lastMarkerItem = pos == getItemCount() - 1;
boolean start = pos == startPos;
boolean finish = pos == finishPos && startPos != finishPos;
boolean firstSelectedMarker = pos == firstSelectedMarkerPos;
Location myLoc = app.getLocationProvider().getLastStaleKnownLocation();
boolean useLocation = app.getMapMarkersHelper().isStartFromMyLocation() && myLoc != null;
MapMarker marker = locationItem ? null : getItem(pos);
@ -96,7 +94,7 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter<MapMarkerItemVie
holder.firstDescription.setVisibility((start || finish) ? View.VISIBLE : View.GONE);
if (start) {
holder.firstDescription.setText(mapActivity.getString(R.string.shared_string_control_start) + (locationItem ? "" : ""));
holder.firstDescription.setText(mapActivity.getString(R.string.shared_string_control_start) + (locationItem && !useLocation ? "" : ""));
} else if (finish) {
holder.firstDescription.setText(mapActivity.getString(R.string.shared_string_finish) + "");
}
@ -146,16 +144,28 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter<MapMarkerItemVie
descr = month + " " + day;
}
holder.description.setText(descr);
if (location != null) {
holder.distance.setVisibility(View.VISIBLE);
holder.distance.setTextColor(ContextCompat.getColor(mapActivity, useCenter
? R.color.color_distance : R.color.color_myloc_distance));
float dist = (float) MapUtils.getDistance(location.getLatitude(), location.getLongitude(),
marker.getLatitude(), marker.getLongitude());
holder.distance.setText(OsmAndFormatter.getFormattedDistance(dist, app));
}
}
boolean showDistance = locationItem ? useLocation : marker.selected;
boolean showPoint = showDistance && !locationItem;
holder.distance.setVisibility(showDistance ? View.VISIBLE : View.GONE);
holder.point.setVisibility(showPoint ? View.VISIBLE : View.GONE);
if (showDistance) {
holder.distance.setTextColor(ContextCompat.getColor(mapActivity, night ? R.color.color_distance : R.color.color_myloc_distance));
LatLon first = firstSelectedMarker && useLocation
? new LatLon(myLoc.getLatitude(), myLoc.getLongitude())
: getPreviousSelectedMarkerLatLon(pos - 1);
float dist = 0;
if (first != null && marker != null) {
dist = (float) MapUtils.getDistance(first, marker.point);
}
holder.distance.setText(OsmAndFormatter.getFormattedDistance(dist, app));
}
}
@Override
public long getItemId(int position) {
return position == 0 ? LOCATION_ITEM_ID : getItem(position).hashCode();
}
@Override
@ -192,30 +202,34 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter<MapMarkerItemVie
listener.onDragEnded(holder);
}
public void updateStartAndFinish() {
int startP = startPos;
int finishP = finishPos;
calculateStartAndFinishPos();
notifyItemChanged(startP);
notifyItemChanged(finishP);
notifyItemChanged(startPos);
notifyItemChanged(finishPos);
private LatLon getPreviousSelectedMarkerLatLon(int currentMarkerPos) {
for (int i = currentMarkerPos - 1; i >= 0; i--) {
MapMarker m = markers.get(i);
if (m.selected) {
return m.point;
}
}
return null;
}
public void calculateStartAndFinishPos() {
OsmandApplication app = mapActivity.getMyApplication();
boolean startCalculated = false;
boolean finishCalculated = false;
boolean firstSelectedMarkerCalculated = false;
if (app.getMapMarkersHelper().isStartFromMyLocation() && app.getLocationProvider().getLastStaleKnownLocation() != null) {
startPos = 0;
startCalculated = true;
} else {
for (int i = 0; i < markers.size(); i++) {
if (markers.get(i).selected) {
}
for (int i = 0; i < markers.size(); i++) {
if (markers.get(i).selected) {
if (!startCalculated) {
startPos = i + 1;
startCalculated = true;
break;
}
firstSelectedMarkerPos = i + 1;
firstSelectedMarkerCalculated = true;
break;
}
}
for (int i = markers.size() - 1; i >= 0; i--) {
@ -231,6 +245,9 @@ public class MapMarkersListAdapter extends RecyclerView.Adapter<MapMarkerItemVie
if (!finishCalculated) {
finishPos = -1;
}
if (!firstSelectedMarkerCalculated) {
firstSelectedMarkerPos = -1;
}
}
public interface MapMarkersListAdapterListener {