This commit is contained in:
Alex Sytnyk 2018-09-13 14:41:50 +03:00
parent 5d0578b57e
commit b4d79ee28c
3 changed files with 62 additions and 15 deletions

View file

@ -90,6 +90,7 @@ public class MapActivityActions implements DialogProvider {
public static final int CHANGE_POSITION_ITEM_ORDER = 3000; public static final int CHANGE_POSITION_ITEM_ORDER = 3000;
public static final int EDIT_GPX_WAYPOINT_ITEM_ORDER = 9000; public static final int EDIT_GPX_WAYPOINT_ITEM_ORDER = 9000;
public static final int ADD_GPX_WAYPOINT_ITEM_ORDER = 9000; public static final int ADD_GPX_WAYPOINT_ITEM_ORDER = 9000;
public static final int MEASURE_DISTANCE_ITEM_ORDER = 13000;
private static final int DIALOG_ADD_FAVORITE = 100; private static final int DIALOG_ADD_FAVORITE = 100;
private static final int DIALOG_REPLACE_FAVORITE = 101; private static final int DIALOG_REPLACE_FAVORITE = 101;
@ -327,6 +328,12 @@ public class MapActivityActions implements DialogProvider {
.setListener(listener).createItem()); .setListener(listener).createItem());
} }
adapter.addItem(itemBuilder
.setTitleId(R.string.measurement_tool, mapActivity)
.setIcon(R.drawable.ic_action_ruler)
.setOrder(MEASURE_DISTANCE_ITEM_ORDER)
.createItem());
adapter.sortItemsByOrder(); adapter.sortItemsByOrder();
final ArrayAdapter<ContextMenuItem> listAdapter = final ArrayAdapter<ContextMenuItem> listAdapter =
@ -355,6 +362,9 @@ public class MapActivityActions implements DialogProvider {
getMyApplication().getTargetPointsHelper().setStartPoint(new LatLon(latitude, longitude), getMyApplication().getTargetPointsHelper().setStartPoint(new LatLon(latitude, longitude),
true, mapActivity.getContextMenu().getPointDescription()); true, mapActivity.getContextMenu().getPointDescription());
} }
} else if (standardId == R.string.measurement_tool) {
mapActivity.getContextMenu().close();
MeasurementToolFragment.showInstance(mapActivity.getSupportFragmentManager(), new LatLon(latitude, longitude));
} }
} }
}); });

View file

@ -35,6 +35,7 @@ import android.widget.Toast;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.CallbackWithObject; import net.osmand.CallbackWithObject;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.data.LatLon;
import net.osmand.plus.ApplicationMode; import net.osmand.plus.ApplicationMode;
import net.osmand.plus.GPXUtilities; import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile; import net.osmand.plus.GPXUtilities.GPXFile;
@ -110,6 +111,8 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
private MeasurementEditingContext editingCtx = new MeasurementEditingContext(); private MeasurementEditingContext editingCtx = new MeasurementEditingContext();
private LatLon initialPoint;
private enum SaveType { private enum SaveType {
ROUTE_POINT, ROUTE_POINT,
LINE LINE
@ -119,6 +122,10 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
this.editingCtx = editingCtx; this.editingCtx = editingCtx;
} }
private void setInitialPoint(LatLon initialPoint) {
this.initialPoint = initialPoint;
}
@Nullable @Nullable
@Override @Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -464,6 +471,7 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
getMapActivity().getMapLayers().getMapControlsLayer().showMapControlsIfHidden(); getMapActivity().getMapLayers().getMapControlsLayer().showMapControlsIfHidden();
cachedMapPosition = getMapActivity().getMapView().getMapPosition(); cachedMapPosition = getMapActivity().getMapView().getMapPosition();
setDefaultMapPosition(); setDefaultMapPosition();
addInitialPoint();
} }
@Override @Override
@ -978,6 +986,17 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
R.id.measure_mode_controls); R.id.measure_mode_controls);
} }
private void addInitialPoint() {
if (initialPoint != null) {
MeasurementToolLayer layer = getMeasurementLayer();
if (layer != null) {
editingCtx.getCommandManager().execute(new AddPointCommand(layer, initialPoint));
doAddOrMovePointCommonStuff();
initialPoint = null;
}
}
}
private void addPoint() { private void addPoint() {
MeasurementToolLayer measurementLayer = getMeasurementLayer(); MeasurementToolLayer measurementLayer = getMeasurementLayer();
if (measurementLayer != null) { if (measurementLayer != null) {
@ -1508,23 +1527,24 @@ public class MeasurementToolFragment extends BaseOsmAndFragment {
} }
} }
public static boolean showInstance(FragmentManager fragmentManager, LatLon initialPoint) {
MeasurementToolFragment fragment = new MeasurementToolFragment();
fragment.setInitialPoint(initialPoint);
return showFragment(fragment, fragmentManager);
}
public static boolean showInstance(FragmentManager fragmentManager, MeasurementEditingContext editingCtx) { public static boolean showInstance(FragmentManager fragmentManager, MeasurementEditingContext editingCtx) {
try { MeasurementToolFragment fragment = new MeasurementToolFragment();
MeasurementToolFragment fragment = new MeasurementToolFragment(); fragment.setEditingCtx(editingCtx);
fragment.setEditingCtx(editingCtx); return showFragment(fragment, fragmentManager);
fragment.setRetainInstance(true);
fragmentManager.beginTransaction()
.add(R.id.bottomFragmentContainer, fragment, MeasurementToolFragment.TAG)
.commitAllowingStateLoss();
return true;
} catch (Exception e) {
return false;
}
} }
public static boolean showInstance(FragmentManager fragmentManager) { public static boolean showInstance(FragmentManager fragmentManager) {
return showFragment(new MeasurementToolFragment(), fragmentManager);
}
private static boolean showFragment(MeasurementToolFragment fragment, FragmentManager fragmentManager) {
try { try {
MeasurementToolFragment fragment = new MeasurementToolFragment();
fragment.setRetainInstance(true); fragment.setRetainInstance(true);
fragmentManager.beginTransaction() fragmentManager.beginTransaction()
.add(R.id.bottomFragmentContainer, fragment, MeasurementToolFragment.TAG) .add(R.id.bottomFragmentContainer, fragment, MeasurementToolFragment.TAG)

View file

@ -1,23 +1,40 @@
package net.osmand.plus.measurementtool.command; package net.osmand.plus.measurementtool.command;
import net.osmand.data.LatLon;
import net.osmand.plus.GPXUtilities.WptPt; import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.measurementtool.MeasurementToolLayer; import net.osmand.plus.measurementtool.MeasurementToolLayer;
public class AddPointCommand extends MeasurementModeCommand { public class AddPointCommand extends MeasurementModeCommand {
private final int position; private int position;
private WptPt point; private WptPt point;
private final boolean center; private boolean center;
public AddPointCommand(MeasurementToolLayer measurementLayer, boolean center) { public AddPointCommand(MeasurementToolLayer measurementLayer, boolean center) {
init(measurementLayer, null, center);
}
public AddPointCommand(MeasurementToolLayer measurementLayer, LatLon latLon) {
init(measurementLayer, latLon, false);
}
private void init(MeasurementToolLayer measurementLayer, LatLon latLon, boolean center) {
this.measurementLayer = measurementLayer; this.measurementLayer = measurementLayer;
if (latLon != null) {
point = new WptPt();
point.lat = latLon.getLatitude();
point.lon = latLon.getLongitude();
}
this.center = center; this.center = center;
position = measurementLayer.getEditingCtx().getPointsCount(); position = measurementLayer.getEditingCtx().getPointsCount();
} }
@Override @Override
public boolean execute() { public boolean execute() {
if (center) { if (point != null) {
measurementLayer.getEditingCtx().addPoint(point);
measurementLayer.moveMapToPoint(position);
} else if (center) {
point = measurementLayer.addCenterPoint(); point = measurementLayer.addCenterPoint();
} else { } else {
point = measurementLayer.addPoint(); point = measurementLayer.addPoint();