Add improvements
This commit is contained in:
parent
396dec1482
commit
9821decb1f
2 changed files with 115 additions and 51 deletions
|
@ -10,12 +10,23 @@ import android.view.LayoutInflater;
|
|||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
import net.osmand.Location;
|
||||
import net.osmand.data.LatLon;
|
||||
import net.osmand.plus.MapMarkersHelper.MapMarker;
|
||||
import net.osmand.plus.OsmAndLocationProvider.OsmAndCompassListener;
|
||||
import net.osmand.plus.OsmAndLocationProvider.OsmAndLocationListener;
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
import net.osmand.plus.base.MapViewTrackingUtilities;
|
||||
import net.osmand.plus.dashboard.DashLocationFragment;
|
||||
import net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter;
|
||||
import net.osmand.plus.mapmarkers.adapters.MapMarkersActiveAdapter.MapMarkersActiveAdapterListener;
|
||||
|
||||
public class MapMarkersActiveFragment extends Fragment {
|
||||
public class MapMarkersActiveFragment extends Fragment implements OsmAndCompassListener, OsmAndLocationListener {
|
||||
|
||||
private MapMarkersActiveAdapter adapter;
|
||||
private Location location;
|
||||
private float heading;
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
|
@ -23,21 +34,88 @@ public class MapMarkersActiveFragment extends Fragment {
|
|||
final RecyclerView recyclerView = new RecyclerView(getContext());
|
||||
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
||||
final MapActivity mapActivity = (MapActivity) getActivity();
|
||||
if (mapActivity != null) {
|
||||
final MapMarkersActiveAdapter adapter = new MapMarkersActiveAdapter(mapActivity);
|
||||
adapter.setAdapterListener(new MapMarkersActiveAdapterListener() {
|
||||
@Override
|
||||
public void onItemClick(View view) {
|
||||
int pos = recyclerView.indexOfChild(view);
|
||||
MapMarker marker = adapter.getItem(pos);
|
||||
mapActivity.getMyApplication().getSettings().setMapLocationToShow(marker.getLatitude(), marker.getLongitude(),
|
||||
15, marker.getPointDescription(mapActivity), true, marker);
|
||||
MapActivity.launchMapActivityMoveToTop(mapActivity);
|
||||
((DialogFragment) getParentFragment()).dismiss();
|
||||
}
|
||||
});
|
||||
recyclerView.setAdapter(adapter);
|
||||
}
|
||||
|
||||
adapter = new MapMarkersActiveAdapter(mapActivity);
|
||||
adapter.setAdapterListener(new MapMarkersActiveAdapterListener() {
|
||||
@Override
|
||||
public void onItemClick(View view) {
|
||||
int pos = recyclerView.indexOfChild(view);
|
||||
MapMarker marker = adapter.getItem(pos);
|
||||
mapActivity.getMyApplication().getSettings().setMapLocationToShow(marker.getLatitude(), marker.getLongitude(),
|
||||
15, marker.getPointDescription(mapActivity), true, marker);
|
||||
MapActivity.launchMapActivityMoveToTop(mapActivity);
|
||||
((DialogFragment) getParentFragment()).dismiss();
|
||||
}
|
||||
});
|
||||
recyclerView.setAdapter(adapter);
|
||||
|
||||
return recyclerView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
adapter.setScreenOrientation(DashLocationFragment.getScreenOrientation(getActivity()));
|
||||
startLocationUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
stopLocationUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateLocation(Location location) {
|
||||
this.location = location;
|
||||
updateLocation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateCompassValue(float heading) {
|
||||
this.heading = heading;
|
||||
updateLocation();
|
||||
}
|
||||
|
||||
private OsmandApplication getMyApplication() {
|
||||
if (getActivity() != null) {
|
||||
return ((MapActivity) getActivity()).getMyApplication();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void updateLocation() {
|
||||
MapActivity mapActivity = (MapActivity) getActivity();
|
||||
if (mapActivity != null) {
|
||||
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.setHeading(useCenter ? -mapActivity.getMapRotate() : heading);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
}
|
||||
|
||||
private void startLocationUpdate() {
|
||||
OsmandApplication app = getMyApplication();
|
||||
if (app != null) {
|
||||
app.getLocationProvider().removeCompassListener(app.getLocationProvider().getNavigationInfo());
|
||||
app.getLocationProvider().addCompassListener(this);
|
||||
app.getLocationProvider().addLocationListener(this);
|
||||
updateLocation();
|
||||
}
|
||||
}
|
||||
|
||||
private void stopLocationUpdate() {
|
||||
OsmandApplication app = getMyApplication();
|
||||
if (app != null) {
|
||||
app.getLocationProvider().removeLocationListener(this);
|
||||
app.getLocationProvider().removeCompassListener(this);
|
||||
app.getLocationProvider().addCompassListener(app.getLocationProvider().getNavigationInfo());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,17 @@
|
|||
package net.osmand.plus.mapmarkers.adapters;
|
||||
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.widget.RecyclerView;
|
||||
import android.view.LayoutInflater;
|
||||
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;
|
||||
import net.osmand.plus.OsmAndFormatter;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.activities.MapActivity;
|
||||
import net.osmand.plus.base.MapViewTrackingUtilities;
|
||||
import net.osmand.plus.dashboard.DashLocationFragment;
|
||||
import net.osmand.plus.views.DirectionDrawable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -29,6 +24,7 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
|
|||
private LatLon location;
|
||||
private Float heading;
|
||||
private boolean useCenter;
|
||||
private int screenOrientation;
|
||||
|
||||
public MapMarkersActiveAdapter(MapActivity mapActivity) {
|
||||
this.mapActivity = mapActivity;
|
||||
|
@ -39,6 +35,22 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
|
|||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void setLocation(LatLon location) {
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
public void setHeading(Float heading) {
|
||||
this.heading = heading;
|
||||
}
|
||||
|
||||
public void setUseCenter(boolean useCenter) {
|
||||
this.useCenter = useCenter;
|
||||
}
|
||||
|
||||
public void setScreenOrientation(int screenOrientation) {
|
||||
this.screenOrientation = screenOrientation;
|
||||
}
|
||||
|
||||
@Override
|
||||
public MapMarkerItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
|
||||
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.map_marker_item_new, viewGroup, false);
|
||||
|
@ -55,7 +67,6 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
|
|||
public void onBindViewHolder(MapMarkerItemViewHolder holder, int pos) {
|
||||
IconsCache iconsCache = mapActivity.getMyApplication().getIconsCache();
|
||||
MapMarker marker = markers.get(pos);
|
||||
calculateLocationParams();
|
||||
|
||||
holder.iconReorder.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_reorder));
|
||||
holder.iconReorder.setOnTouchListener(new View.OnTouchListener() {
|
||||
|
@ -70,21 +81,10 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
|
|||
|
||||
holder.title.setText(marker.getName(mapActivity));
|
||||
|
||||
float[] mes = new float[2];
|
||||
if (location != null && marker.point != null) {
|
||||
Location.distanceBetween(marker.getLatitude(), marker.getLongitude(), location.getLatitude(), location.getLongitude(), mes);
|
||||
}
|
||||
DirectionDrawable dd = new DirectionDrawable(mapActivity, holder.iconDirection.getWidth(), holder.iconDirection.getHeight());
|
||||
dd.setImage(R.drawable.ic_direction_arrow, useCenter ? R.color.color_distance : R.color.color_myloc_distance);
|
||||
if (location == null || heading == null || marker.point == null) {
|
||||
dd.setAngle(0);
|
||||
} else {
|
||||
dd.setAngle(mes[1] - heading + 180 + DashLocationFragment.getScreenOrientation(mapActivity));
|
||||
}
|
||||
holder.iconDirection.setImageDrawable(dd);
|
||||
|
||||
holder.distance.setTextColor(ContextCompat.getColor(mapActivity, useCenter ? R.color.color_distance : R.color.color_myloc_distance));
|
||||
holder.distance.setText(OsmAndFormatter.getFormattedDistance((int) mes[0], mapActivity.getMyApplication()));
|
||||
DashLocationFragment.updateLocationView(useCenter, location,
|
||||
heading, holder.iconDirection, holder.distance,
|
||||
marker.getLatitude(), marker.getLongitude(),
|
||||
screenOrientation, mapActivity.getMyApplication(), mapActivity);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -96,20 +96,6 @@ public class MapMarkersActiveAdapter extends RecyclerView.Adapter<MapMarkerItemV
|
|||
return markers.get(position);
|
||||
}
|
||||
|
||||
private void calculateLocationParams() {
|
||||
MapViewTrackingUtilities utilities = mapActivity.getMapViewTrackingUtilities();
|
||||
|
||||
Float utHeading = utilities.getHeading();
|
||||
float mapRotation = mapActivity.getMapRotate();
|
||||
LatLon mapLoc = mapActivity.getMapLocation();
|
||||
Location lastLoc = mapActivity.getMyApplication().getLocationProvider().getLastKnownLocation();
|
||||
boolean mapLinked = utilities.isMapLinkedToLocation() && lastLoc != null;
|
||||
LatLon myLoc = lastLoc == null ? null : new LatLon(lastLoc.getLatitude(), lastLoc.getLongitude());
|
||||
useCenter = !mapLinked;
|
||||
location = (useCenter ? mapLoc : myLoc);
|
||||
heading = useCenter ? -mapRotation : utHeading;
|
||||
}
|
||||
|
||||
public interface MapMarkersActiveAdapterListener {
|
||||
|
||||
void onItemClick(View view);
|
||||
|
|
Loading…
Reference in a new issue