implement annotation marker tap recognition in Android

This commit is contained in:
Justin R. Miller
2015-08-28 15:29:04 -07:00
parent f3cd47eded
commit bcfce808ca
4 changed files with 156 additions and 0 deletions

View File

@@ -50,6 +50,7 @@ import com.mapbox.mapboxgl.annotations.Polygon;
import com.mapbox.mapboxgl.annotations.PolygonOptions;
import com.mapbox.mapboxgl.annotations.Polyline;
import com.mapbox.mapboxgl.annotations.PolylineOptions;
import com.mapbox.mapboxgl.geometry.BoundingBox;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.geometry.LatLngZoom;
import com.mapzen.android.lost.api.LocationListener;
@@ -428,6 +429,25 @@ public class MapView extends FrameLayout implements LocationListener {
return Collections.unmodifiableList(mAnnotations);
}
public List<Annotation> getAnnotationsInBounds(BoundingBox bbox) {
List<Annotation> annotations = new ArrayList<>();
long[] ids = mNativeMapView.getAnnotationsInBounds(bbox);
List<Long> idsList = new ArrayList<>();
for(int i = 0; i < ids.length; i++) {
idsList.add(new Long(ids[i]));
}
for(int i = 0; i < mAnnotations.size(); i++) {
Annotation annotation = mAnnotations.get(i);
if (annotation instanceof Marker && idsList.contains(annotation.getId())) {
annotations.add(annotation);
}
}
for(int i = 0; i < annotations.size(); i++) {
Log.d(TAG, "tapped: " + Long.toString(annotations.get(i).getId()));
}
return annotations;
}
//
// Property methods
//
@@ -919,6 +939,22 @@ public class MapView extends FrameLayout implements LocationListener {
// Cancel any animation
mNativeMapView.cancelTransitions();
// Select or deselect point annotations
PointF tapPoint = new PointF(e.getX(), e.getY());
float toleranceWidth = 60 * mScreenDensity;
float toleranceHeight = 80 * mScreenDensity;
PointF tr = new PointF(tapPoint.x + toleranceWidth / 2, tapPoint.y + 2 * toleranceHeight / 3);
PointF bl = new PointF(tapPoint.x - toleranceWidth / 2, tapPoint.y - 1 * toleranceHeight / 3);
LatLng sw = fromScreenLocation(bl);
LatLng ne = fromScreenLocation(tr);
BoundingBox bbox = new BoundingBox(ne, sw);
List<Annotation> annotations = getAnnotationsInBounds(bbox);
return true;
}

View File

@@ -3,13 +3,16 @@ package com.mapbox.mapboxgl.views;
import android.graphics.PointF;
import android.view.Surface;
import com.mapbox.mapboxgl.annotations.Annotation;
import com.mapbox.mapboxgl.annotations.Marker;
import com.mapbox.mapboxgl.annotations.Polygon;
import com.mapbox.mapboxgl.annotations.Polyline;
import com.mapbox.mapboxgl.geometry.BoundingBox;
import com.mapbox.mapboxgl.geometry.LatLng;
import com.mapbox.mapboxgl.geometry.LatLngZoom;
import com.mapbox.mapboxgl.geometry.ProjectedMeters;
import java.util.ArrayList;
import java.util.List;
// Class that wraps the native methods for convenience
@@ -353,6 +356,10 @@ class NativeMapView {
nativeRemoveAnnotations(mNativeMapViewPtr, ids);
}
public long[] getAnnotationsInBounds(BoundingBox bbox) {
return nativeGetAnnotationsInBounds(mNativeMapViewPtr, bbox);
}
public void setSprite(String symbol, int width, int height, float scale, byte[] pixels) {
nativeSetSprite(mNativeMapViewPtr, symbol, width, height, scale, pixels);
}
@@ -563,6 +570,8 @@ class NativeMapView {
private native void nativeRemoveAnnotations(long nativeMapViewPtr, long[] id);
private native long[] nativeGetAnnotationsInBounds(long mNativeMapViewPtr, BoundingBox bbox);
private native void nativeSetSprite(long nativeMapViewPtr, String symbol,
int width, int height, float scale, byte[] pixels);