PathLayer, VectorLayer (polygon) overlays touch events #316 #424 (#425)

This commit is contained in:
Emux
2017-10-05 20:39:30 +03:00
committed by GitHub
parent 126b98ae8c
commit e3f2f19119
7 changed files with 150 additions and 7 deletions

View File

@@ -1,7 +1,7 @@
/*
* Copyright 2012 osmdroid authors: Viesturs Zarins, Martin Pearman
* Copyright 2012 Hannes Janetzek
* Copyright 2016 devemux86
* Copyright 2016-2017 devemux86
* Copyright 2016 Bezzu
* Copyright 2016 Pedinel
* Copyright 2017 Andrey Novikov
@@ -21,12 +21,17 @@
*/
package org.oscim.layers;
import org.oscim.backend.CanvasAdapter;
import org.oscim.backend.canvas.Paint.Cap;
import org.oscim.core.GeoPoint;
import org.oscim.core.GeometryBuffer;
import org.oscim.core.MapPosition;
import org.oscim.core.MercatorProjection;
import org.oscim.core.Point;
import org.oscim.core.Tile;
import org.oscim.event.Gesture;
import org.oscim.event.GestureListener;
import org.oscim.event.MotionEvent;
import org.oscim.map.Map;
import org.oscim.renderer.BucketRenderer;
import org.oscim.renderer.GLViewport;
@@ -34,6 +39,7 @@ import org.oscim.renderer.bucket.LineBucket;
import org.oscim.renderer.bucket.RenderBuckets;
import org.oscim.theme.styles.LineStyle;
import org.oscim.utils.FastMath;
import org.oscim.utils.GeoPointUtils;
import org.oscim.utils.async.SimpleWorker;
import org.oscim.utils.geom.LineClipper;
@@ -44,7 +50,7 @@ import java.util.List;
/**
* This class draws a path line in given color or texture.
*/
public class PathLayer extends Layer {
public class PathLayer extends Layer implements GestureListener {
/**
* Stores points, converted to the map projection.
@@ -52,6 +58,9 @@ public class PathLayer extends Layer {
protected final ArrayList<GeoPoint> mPoints;
protected boolean mUpdatePoints;
private final Point mPoint1 = new Point();
private final Point mPoint2 = new Point();
/**
* Line style
*/
@@ -437,4 +446,26 @@ public class PathLayer extends Layer {
return i;
}
}
public synchronized boolean contains(float x, float y) {
// Touch min 20 px at baseline mdpi (160dpi)
double distance = Math.max(20 / 2 * CanvasAdapter.getScale(), mLineStyle.width);
for (int i = 0; i < mPoints.size() - 1; i++) {
if (i == 0)
mMap.viewport().toScreenPoint(mPoints.get(i), false, mPoint1);
else {
mPoint1.x = mPoint2.x;
mPoint1.y = mPoint2.y;
}
mMap.viewport().toScreenPoint(mPoints.get(i + 1), false, mPoint2);
if (GeoPointUtils.distanceSegmentPoint(mPoint1.x, mPoint1.y, mPoint2.x, mPoint2.y, x, y) <= distance)
return true;
}
return false;
}
@Override
public boolean onGesture(Gesture g, MotionEvent e) {
return false;
}
}

View File

@@ -15,12 +15,14 @@
package org.oscim.utils;
import org.oscim.core.GeoPoint;
import org.oscim.core.Point;
public final class GeoPointUtils {
/**
* Find if the given point lies within this polygon.<br/>
* See http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
* Find if the given point lies within this polygon.
* <p>
* http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
*
* @return true if this polygon contains the given point, false otherwise.
*/
@@ -36,6 +38,16 @@ public final class GeoPointUtils {
return result;
}
/**
* Returns the distance between the given segment and point.
* <p>
* libGDX (Apache 2.0)
*/
public static double distanceSegmentPoint(double startX, double startY, double endX, double endY, double pointX, double pointY) {
Point nearest = nearestSegmentPoint(startX, startY, endX, endY, pointX, pointY);
return Math.hypot(nearest.x - pointX, nearest.y - pointY);
}
/**
* Find if this way is closed.
*
@@ -45,6 +57,22 @@ public final class GeoPointUtils {
return geoPoints[0].distance(geoPoints[geoPoints.length - 1]) < 0.000000001;
}
/**
* Returns a point on the segment nearest to the specified point.
* <p>
* libGDX (Apache 2.0)
*/
public static Point nearestSegmentPoint(double startX, double startY, double endX, double endY, double pointX, double pointY) {
double xDiff = endX - startX;
double yDiff = endY - startY;
double length2 = xDiff * xDiff + yDiff * yDiff;
if (length2 == 0) return new Point(startX, startY);
double t = ((pointX - startX) * (endX - startX) + (pointY - startY) * (endY - startY)) / length2;
if (t < 0) return new Point(startX, startY);
if (t > 1) return new Point(endX, endY);
return new Point(startX + t * (endX - startX), startY + t * (endY - startY));
}
private GeoPointUtils() {
throw new IllegalStateException();
}