Skip to content

Commit

Permalink
Merge pull request #5627 from grzesiek2010/COLLECT-5565
Browse files Browse the repository at this point in the history
Remove nodes when displaying lines and shapes + make lines consistent across different map providers
  • Loading branch information
grzesiek2010 authored Jun 22, 2023
2 parents fd65e7a + 54b1274 commit 9166635
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package org.odk.collect.android.geo;

import static org.odk.collect.maps.MapConsts.POLYGON_FILL_COLOR_OPACITY;
import static org.odk.collect.maps.MapConsts.POLYLINE_STROKE_WIDTH;
import static org.odk.collect.settings.keys.ProjectKeys.KEY_GOOGLE_MAP_STYLE;

import android.annotation.SuppressLint;
Expand Down Expand Up @@ -71,6 +73,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.inject.Inject;

Expand Down Expand Up @@ -306,36 +309,40 @@ public List<Integer> addMarkers(List<MarkerDescription> markers) {

@Override public int addPolyLine(@NonNull Iterable<MapPoint> points, boolean closed, boolean draggable) {
int featureId = nextFeatureId++;
features.put(featureId, new PolyLineFeature(getActivity(), points, closed, draggable, map));
if (draggable) {
features.put(featureId, new DynamicPolyLineFeature(getActivity(), points, closed, map));
} else {
features.put(featureId, new StaticPolyLineFeature(getActivity(), points, closed, map));
}
return featureId;
}

@Override
public int addPolygon(@NonNull Iterable<MapPoint> points) {
int featureId = nextFeatureId++;
features.put(featureId, new PolygonFeature(requireActivity(), map, points, requireContext().getResources().getColor(R.color.mapLineColor)));
features.put(featureId, new StaticPolygonFeature(map, points, requireContext().getResources().getColor(R.color.mapLineColor)));
return featureId;
}

@Override public void appendPointToPolyLine(int featureId, @NonNull MapPoint point) {
MapFeature feature = features.get(featureId);
if (feature instanceof PolyLineFeature) {
((PolyLineFeature) feature).addPoint(point);
if (feature instanceof DynamicPolyLineFeature) {
((DynamicPolyLineFeature) feature).addPoint(point);
}
}

@Override public @NonNull List<MapPoint> getPolyLinePoints(int featureId) {
MapFeature feature = features.get(featureId);
if (feature instanceof PolyLineFeature) {
return ((PolyLineFeature) feature).getPoints();
if (feature instanceof DynamicPolyLineFeature) {
return ((DynamicPolyLineFeature) feature).getPoints();
}
return new ArrayList<>();
}

@Override public void removePolyLineLastPoint(int featureId) {
MapFeature feature = features.get(featureId);
if (feature instanceof PolyLineFeature) {
((PolyLineFeature) feature).removeLastPoint();
if (feature instanceof DynamicPolyLineFeature) {
((DynamicPolyLineFeature) feature).removeLastPoint();
}
}

Expand Down Expand Up @@ -788,38 +795,98 @@ public void dispose() {
}
}

/** A polyline or polygon that can not be manipulated by dragging markers at its vertices. */
private static class StaticPolyLineFeature implements MapFeature {

private Polyline polyline;

StaticPolyLineFeature(Context context, Iterable<MapPoint> points, boolean closedPolygon, GoogleMap map) {
if (map == null) { // during Robolectric tests, map will be null
return;
}

List<LatLng> latLngs = StreamSupport.stream(points.spliterator(), false).map(mapPoint -> new LatLng(mapPoint.latitude, mapPoint.longitude)).collect(Collectors.toList());
if (closedPolygon && !latLngs.isEmpty()) {
latLngs.add(latLngs.get(0));
}
if (latLngs.isEmpty()) {
clearPolyline();
} else if (polyline == null) {
polyline = map.addPolyline(new PolylineOptions()
.color(context.getResources().getColor(R.color.mapLineColor))
.zIndex(1)
.width(POLYLINE_STROKE_WIDTH)
.addAll(latLngs)
.clickable(true)
);
} else {
polyline.setPoints(latLngs);
}
}

@Override
public boolean ownsMarker(Marker givenMarker) {
return false;
}

@Override
public boolean ownsPolyline(Polyline givenPolyline) {
return polyline.equals(givenPolyline);
}

@Override
public boolean ownsPolygon(Polygon polygon) {
return false;
}

@Override
public void update() {
}

@Override
public void dispose() {
clearPolyline();
}

private void clearPolyline() {
if (polyline != null) {
polyline.remove();
polyline = null;
}
}
}

/** A polyline or polygon that can be manipulated by dragging markers at its vertices. */
private static class PolyLineFeature implements MapFeature {
public static final int STROKE_WIDTH = 5;
private static class DynamicPolyLineFeature implements MapFeature {

private final Context context;
private final GoogleMap map;
private final List<Marker> markers = new ArrayList<>();
private final boolean closedPolygon;
private final boolean draggable;
private Polyline polyline;

PolyLineFeature(Context context, Iterable<MapPoint> points, boolean closedPolygon, boolean draggable, GoogleMap map) {
DynamicPolyLineFeature(Context context, Iterable<MapPoint> points, boolean closedPolygon, GoogleMap map) {
this.context = context;
this.map = map;
this.closedPolygon = closedPolygon;
this.draggable = draggable;

if (map == null) { // during Robolectric tests, map will be null
return;
}

for (MapPoint point : points) {
markers.add(createMarker(context, new MarkerDescription(point, draggable, CENTER, new MarkerIconDescription(R.drawable.ic_map_point)), map));
markers.add(createMarker(context, new MarkerDescription(point, true, CENTER, new MarkerIconDescription(R.drawable.ic_map_point)), map));
}

update();
}

@Override
public boolean ownsMarker(Marker givenMarker) {
return markers.contains(givenMarker);
}

@Override
public boolean ownsPolyline(Polyline givenPolyline) {
return polyline.equals(givenPolyline);
}
Expand All @@ -829,6 +896,7 @@ public boolean ownsPolygon(Polygon polygon) {
return false;
}

@Override
public void update() {
List<LatLng> latLngs = new ArrayList<>();
for (Marker marker : markers) {
Expand All @@ -843,7 +911,7 @@ public void update() {
polyline = map.addPolyline(new PolylineOptions()
.color(context.getResources().getColor(R.color.mapLineColor))
.zIndex(1)
.width(STROKE_WIDTH)
.width(POLYLINE_STROKE_WIDTH)
.addAll(latLngs)
.clickable(true)
);
Expand All @@ -852,6 +920,7 @@ public void update() {
}
}

@Override
public void dispose() {
clearPolyline();
for (Marker marker : markers) {
Expand All @@ -872,7 +941,7 @@ public void addPoint(MapPoint point) {
if (map == null) { // during Robolectric tests, map will be null
return;
}
markers.add(createMarker(context, new MarkerDescription(point, draggable, CENTER, new MarkerIconDescription(R.drawable.ic_map_point)), map));
markers.add(createMarker(context, new MarkerDescription(point, true, CENTER, new MarkerIconDescription(R.drawable.ic_map_point)), map));
update();
}

Expand All @@ -893,29 +962,22 @@ private void clearPolyline() {
}
}

private static class PolygonFeature implements MapFeature {

private static class StaticPolygonFeature implements MapFeature {
private Polygon polygon;
private final List<Marker> markers = new ArrayList<>();

PolygonFeature(Context context, GoogleMap map, Iterable<MapPoint> points, int strokeLineColor) {

for (MapPoint point : points) {
markers.add(createMarker(context, new MarkerDescription(point, false, CENTER, new MarkerIconDescription(R.drawable.ic_map_point)), map));
}

StaticPolygonFeature(GoogleMap map, Iterable<MapPoint> points, int strokeLineColor) {
polygon = map.addPolygon(new PolygonOptions()
.addAll(markers.stream().map(Marker::getPosition).collect(Collectors.toList()))
.addAll(StreamSupport.stream(points.spliterator(), false).map(mapPoint -> new LatLng(mapPoint.latitude, mapPoint.longitude)).collect(Collectors.toList()))
.strokeColor(strokeLineColor)
.strokeWidth(5)
.fillColor(ColorUtils.setAlphaComponent(strokeLineColor, 68))
.strokeWidth(POLYLINE_STROKE_WIDTH)
.fillColor(ColorUtils.setAlphaComponent(strokeLineColor, POLYGON_FILL_COLOR_OPACITY))
.clickable(true)
);
}

@Override
public boolean ownsMarker(Marker marker) {
return markers.contains(marker);
return false;
}

@Override
Expand All @@ -930,7 +992,6 @@ public boolean ownsPolygon(Polygon polygon) {

@Override
public void update() {

}

@Override
Expand All @@ -939,11 +1000,6 @@ public void dispose() {
polygon.remove();
polygon = null;
}

for (Marker marker : markers) {
marker.remove();
}
markers.clear();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import com.mapbox.maps.plugin.annotation.generated.PointAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PolylineAnnotation
import com.mapbox.maps.plugin.annotation.generated.PolylineAnnotationManager
import com.mapbox.maps.plugin.annotation.generated.PolylineAnnotationOptions
import org.odk.collect.maps.MapConsts.MAPBOX_POLYLINE_STROKE_WIDTH
import org.odk.collect.maps.MapFragment
import org.odk.collect.maps.MapPoint

/** A polyline that can be manipulated by dragging Symbols at its vertices. */
internal class PolyLineFeature(
internal class DynamicPolyLineFeature(
private val context: Context,
private val pointAnnotationManager: PointAnnotationManager,
private val polylineAnnotationManager: PolylineAnnotationManager,
private val featureId: Int,
private val featureClickListener: MapFragment.FeatureListener?,
private val featureDragEndListener: MapFragment.FeatureListener?,
private val closedPolygon: Boolean,
private val draggable: Boolean,
initMapPoints: Iterable<MapPoint>
) : MapFeature {
val mapPoints = mutableListOf<MapPoint>()
Expand All @@ -38,7 +38,7 @@ internal class PolyLineFeature(
MapUtils.createPointAnnotation(
pointAnnotationManager,
it,
draggable,
true,
MapFragment.CENTER,
R.drawable.ic_map_point,
context
Expand Down Expand Up @@ -83,7 +83,7 @@ internal class PolyLineFeature(
MapUtils.createPointAnnotation(
pointAnnotationManager,
point,
draggable,
true,
MapFragment.CENTER,
R.drawable.ic_map_point,
context
Expand Down Expand Up @@ -122,7 +122,7 @@ internal class PolyLineFeature(
PolylineAnnotationOptions()
.withPoints(points)
.withLineColor(ColorUtils.colorToRgbaString(context.resources.getColor(R.color.mapLineColor)))
.withLineWidth(5.0)
.withLineWidth(MAPBOX_POLYLINE_STROKE_WIDTH.toDouble())
).also {
polylineAnnotationManager.update(it)
}
Expand Down
41 changes: 25 additions & 16 deletions mapbox/src/main/java/org/odk/collect/mapbox/MapboxMapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -333,25 +333,34 @@ class MapboxMapFragment :

override fun addPolyLine(points: MutableIterable<MapPoint>, closed: Boolean, draggable: Boolean): Int {
val featureId = nextFeatureId++
features[featureId] = PolyLineFeature(
requireContext(),
pointAnnotationManager,
polylineAnnotationManager,
featureId,
featureClickListener,
featureDragEndListener,
closed,
draggable,
points
)
if (draggable) {
features[featureId] = DynamicPolyLineFeature(
requireContext(),
pointAnnotationManager,
polylineAnnotationManager,
featureId,
featureClickListener,
featureDragEndListener,
closed,
points
)
} else {
features[featureId] = StaticPolyLineFeature(
requireContext(),
polylineAnnotationManager,
featureId,
featureClickListener,
closed,
points
)
}
return featureId
}

override fun addPolygon(points: MutableIterable<MapPoint>): Int {
val featureId = nextFeatureId++
features[featureId] = PolygonFeature(
features[featureId] = StaticPolygonFeature(
requireContext(),
pointAnnotationManager,
mapView.annotations.createPolygonAnnotationManager(),
points,
featureClickListener,
Expand All @@ -363,21 +372,21 @@ class MapboxMapFragment :

override fun appendPointToPolyLine(featureId: Int, point: MapPoint) {
val feature = features[featureId]
if (feature is PolyLineFeature) {
if (feature is DynamicPolyLineFeature) {
feature.appendPoint(point)
}
}

override fun removePolyLineLastPoint(featureId: Int) {
val feature = features[featureId]
if (feature is PolyLineFeature) {
if (feature is DynamicPolyLineFeature) {
feature.removeLastPoint()
}
}

override fun getPolyLinePoints(featureId: Int): List<MapPoint> {
val feature = features[featureId]
return if (feature is PolyLineFeature) {
return if (feature is DynamicPolyLineFeature) {
feature.mapPoints
} else {
emptyList()
Expand Down
Loading

0 comments on commit 9166635

Please sign in to comment.