From 3faa05cee0caa8282029e7b2ec99759e096ee69a Mon Sep 17 00:00:00 2001 From: Neal Sanche Date: Thu, 26 May 2016 12:27:21 -0500 Subject: [PATCH 1/2] [android] Add hole handling to PolygonOptions - Added holes to the Java SDK, JNI, and renderer --- .../mapboxsdk/annotations/MultiPoint.java | 15 ++++++++ .../mapboxsdk/annotations/PolygonOptions.java | 19 +++++++++++ .../activity/annotation/PolygonActivity.java | 34 +++++++++++++++++++ platform/android/src/jni.cpp | 23 ++++++++++++- 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java index 2bd3c82786c..9803c50d0e8 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java @@ -11,11 +11,13 @@ public abstract class MultiPoint extends Annotation { private List points; + private List> holes; private float alpha = 1.0f; protected MultiPoint() { super(); points = new ArrayList<>(); + holes = new ArrayList<>(); } /** @@ -27,6 +29,15 @@ public List getPoints() { return new ArrayList<>(points); } + /* + * Returns a copy of the holes. + * + * @return holes - as a copy + */ + public List> getHoles() { + return new ArrayList<>(holes); + } + /** * Sets the points of this polyline. This method will take a copy of the points, so further * mutations to points will have no effect on this polyline. @@ -48,6 +59,10 @@ public void addPoint(LatLng point) { update(); } + void addHole(List hole) { + holes.add(hole); + } + /** * Value between 0 and 1 defining the polyline alpha. * diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java index 22f1258fc71..46ad1f03a4d 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java @@ -108,6 +108,17 @@ public PolygonOptions addAll(Iterable points) { return this; } + public PolygonOptions addHole(Iterable points) { + List hole = new ArrayList(); + for (LatLng point : points) { + hole.add(point); + } + + polygon.addHole(hole); + + return this; + } + /** * Set the alpha value of the polyline. * @@ -182,6 +193,10 @@ public List getPoints() { return polygon.getPoints(); } + public List> getHoles() { + return polygon.getHoles(); + } + /** * Compares this {@link PolygonOptions} object with another {@link PolygonOptions} and * determines if their color, alpha, stroke color, and vertices match. @@ -210,6 +225,9 @@ public boolean equals(Object o) { if (getStrokeColor() != polygon.getStrokeColor()) { return false; } + if (getHoles() != null ? !getHoles().equals(polygon.getHoles()) : polygon.getHoles() != null) { + return false; + } return !(getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null); } @@ -228,6 +246,7 @@ public int hashCode() { result = 31 * result + getFillColor(); result = 31 * result + getStrokeColor(); result = 31 * result + (getPoints() != null ? getPoints().hashCode() : 0); + result = 31 * result + (getHoles() != null ? getHoles().hashCode() : 0); return result; } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java index c37d9bb92e0..5cd19d1fea3 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java @@ -27,6 +27,7 @@ import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.PARTIAL_ALPHA; import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.RED_COLOR; import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.STAR_SHAPE_POINTS; +import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.STAR_SHAPE_HOLES; /** * Activity to test the Polygon annotation API & programmatically creating a MapView. @@ -135,6 +136,7 @@ public boolean onOptionsItemSelected(MenuItem item) { case R.id.action_id_points: allPoints = !allPoints; polygon.setPoints(allPoints ? STAR_SHAPE_POINTS : BROKEN_SHAPE_POINTS); + polygon.setHoles(STAR_SHAPE_HOLES); return true; case R.id.action_id_color: color = !color; @@ -177,6 +179,38 @@ static final class Config { } }; + static final List> STAR_SHAPE_HOLES = new ArrayList>() { + { + add(new ArrayList() { + { + add(new LatLng(45.522584, -122.672653)); + add(new LatLng(45.532584, -122.672653)); + add(new LatLng(45.532584, -122.682653)); + add(new LatLng(45.522584, -122.682653)); + add(new LatLng(45.522584, -122.672653)); + } + }); + add(new ArrayList() { + { + add(new LatLng(45.521584, -122.671653)); + add(new LatLng(45.516584, -122.671653)); + add(new LatLng(45.516584, -122.676653)); + add(new LatLng(45.521584, -122.676653)); + add(new LatLng(45.521584, -122.671653)); + } + }); + add(new ArrayList() { + { + add(new LatLng(45.529584, -122.661653)); + add(new LatLng(45.524584, -122.661653)); + add(new LatLng(45.524584, -122.656653)); + add(new LatLng(45.529584, -122.656653)); + add(new LatLng(45.529584, -122.661653)); + } + }); + } + }; + static final List BROKEN_SHAPE_POINTS = STAR_SHAPE_POINTS.subList(0, STAR_SHAPE_POINTS.size() - 3); } diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index f273210c778..9450bb3002e 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -89,6 +89,7 @@ jni::jfieldID* polygonAlphaId = nullptr; jni::jfieldID* polygonFillColorId = nullptr; jni::jfieldID* polygonStrokeColorId = nullptr; jni::jfieldID* polygonPointsId = nullptr; +jni::jfieldID* polygonHolesId = nullptr; jni::jmethodID* listToArrayId = nullptr; @@ -759,7 +760,26 @@ jni::jarray* nativeAddPolygons(JNIEnv *env, jni::jobject* obj, jlong nati jni::jobject* polygon = jni::GetObjectArrayElement(*env, *jarray, i); jni::jobject* points = jni::GetField(*env, polygon, *polygonPointsId); - mbgl::FillAnnotation annotation { mbgl::Polygon { toGeometry>(env, points) } }; + mbgl::Polygon geometry { toGeometry>(env, points) }; + + jni::jobject* holes = jni::GetField(*env, polygon, *polygonHolesId); + NullCheck(*env, holes); + jni::jarray* jarrayHoles = + reinterpret_cast*>(jni::CallMethod(*env, holes, *listToArrayId)); + NullCheck(*env, jarrayHoles); + + std::size_t size = jni::GetArrayLength(*env, *jarrayHoles); + for (std::size_t i = 0; i < size; i++) { + jni::jobject* hole = reinterpret_cast(jni::GetObjectArrayElement(*env, *jarrayHoles, i)); + NullCheck(*env, hole); + geometry.push_back(toGeometry>(env, hole)); + jni::DeleteLocalRef(*env, hole); + } + + jni::DeleteLocalRef(*env, jarrayHoles); + jni::DeleteLocalRef(*env, holes); + + mbgl::FillAnnotation annotation { geometry }; annotation.opacity = { jni::GetField(*env, polygon, *polygonAlphaId) }; annotation.outlineColor = { toColor(jni::GetField(*env, polygon, *polygonStrokeColorId)) }; annotation.color = { toColor(jni::GetField(*env, polygon, *polygonFillColorId)) }; @@ -1811,6 +1831,7 @@ void registerNatives(JavaVM *vm) { polygonFillColorId = &jni::GetFieldID(env, *polygonClass, "fillColor", "I"); polygonStrokeColorId = &jni::GetFieldID(env, *polygonClass, "strokeColor", "I"); polygonPointsId = &jni::GetFieldID(env, *polygonClass, "points", "Ljava/util/List;"); + polygonHolesId = &jni::GetFieldID(env, *polygonClass, "holes", "Ljava/util.List;"); jni::jclass* listClass = &jni::FindClass(env, "java/util/List"); listToArrayId = &jni::GetMethodID(env, *listClass, "toArray", "()[Ljava/lang/Object;"); From 6da5e07e48bff61502f3c3da78a2f25f6e703afa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Minh=20Nguy=E1=BB=85n?= Date: Tue, 13 Dec 2016 00:27:43 -0800 Subject: [PATCH 2/2] [android] Cleaned up documentation; fixed build --- .../mapboxsdk/annotations/MultiPoint.java | 15 --------- .../mapbox/mapboxsdk/annotations/Polygon.java | 32 ++++++++++++++++++- .../mapboxsdk/annotations/PolygonOptions.java | 19 ++++++++--- .../activity/annotation/PolygonActivity.java | 2 +- platform/android/src/jni.cpp | 4 +-- 5 files changed, 48 insertions(+), 24 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java index 9803c50d0e8..2bd3c82786c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MultiPoint.java @@ -11,13 +11,11 @@ public abstract class MultiPoint extends Annotation { private List points; - private List> holes; private float alpha = 1.0f; protected MultiPoint() { super(); points = new ArrayList<>(); - holes = new ArrayList<>(); } /** @@ -29,15 +27,6 @@ public List getPoints() { return new ArrayList<>(points); } - /* - * Returns a copy of the holes. - * - * @return holes - as a copy - */ - public List> getHoles() { - return new ArrayList<>(holes); - } - /** * Sets the points of this polyline. This method will take a copy of the points, so further * mutations to points will have no effect on this polyline. @@ -59,10 +48,6 @@ public void addPoint(LatLng point) { update(); } - void addHole(List hole) { - holes.add(hole); - } - /** * Value between 0 and 1 defining the polyline alpha. * diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java index 7b9de86bc4c..94dbb55bb4c 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java @@ -2,18 +2,48 @@ import android.graphics.Color; +import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.MapboxMap; +import java.util.ArrayList; +import java.util.List; + /** * Polygon is a geometry annotation that's a closed loop of coordinates. */ public final class Polygon extends MultiPoint { + private List> holes; private int fillColor = Color.BLACK; // default fillColor is black private int strokeColor = Color.BLACK; // default strokeColor is black Polygon() { super(); + holes = new ArrayList<>(); + } + + /** + * Returns a copy of the holes. + * + * @return A {@link List} of holes. + */ + public List> getHoles() { + return new ArrayList<>(holes); + } + + /** + * Sets the holes of this polygon. This method will take a copy of the holes, so further + * mutations to holes will have no effect on this polygon. + * + * @param points A {@link List} {@link List}s of {@link LatLng} points making up the holes. + */ + public void setHoles(List> holes) { + this.holes = new ArrayList<>(holes); + update(); + } + + void addHole(List hole) { + holes.add(hole); } /** @@ -26,7 +56,7 @@ public int getFillColor() { } /** - * Get the color fo the stroke of the polygon. + * Get the color for the stroke of the polygon. * * @return The color of the stroke. */ diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java index 46ad1f03a4d..6ed95990ebc 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java @@ -108,6 +108,12 @@ public PolygonOptions addAll(Iterable points) { return this; } + /** + * Adds a hole to the outline of the polygon being built. + * + * @param points {@link Iterable} list made up of {@link LatLng} points defining the hole + * @return This {@link PolygonOptions} object with the given hole added to the outline. + */ public PolygonOptions addHole(Iterable points) { List hole = new ArrayList(); for (LatLng point : points) { @@ -193,17 +199,20 @@ public List getPoints() { return polygon.getPoints(); } + /** + * Gets the holes set for this {@link PolygonOptions} object. + */ public List> getHoles() { return polygon.getHoles(); } /** * Compares this {@link PolygonOptions} object with another {@link PolygonOptions} and - * determines if their color, alpha, stroke color, and vertices match. + * determines if their color, alpha, stroke color, vertices, and holes match. * * @param o Another {@link PolygonOptions} to compare with this object. - * @return True if color, alpha, stroke color, and vertices match this {@link PolygonOptions} - * object. Else, false. + * @return True if color, alpha, stroke color, vertices, and holes match this + * {@link PolygonOptions} object. Else, false. */ @Override public boolean equals(Object o) { @@ -225,10 +234,10 @@ public boolean equals(Object o) { if (getStrokeColor() != polygon.getStrokeColor()) { return false; } - if (getHoles() != null ? !getHoles().equals(polygon.getHoles()) : polygon.getHoles() != null) { + if (getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null) { return false; } - return !(getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null); + return !(getHoles() != null ? !getHoles().equals(polygon.getHoles()) : polygon.getHoles() != null); } /** diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java index 5cd19d1fea3..451152a38c2 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java @@ -179,7 +179,7 @@ static final class Config { } }; - static final List> STAR_SHAPE_HOLES = new ArrayList>() { + static final List> STAR_SHAPE_HOLES = new ArrayList>() { { add(new ArrayList() { { diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 9450bb3002e..bdfd85a0b56 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -769,8 +769,8 @@ jni::jarray* nativeAddPolygons(JNIEnv *env, jni::jobject* obj, jlong nati NullCheck(*env, jarrayHoles); std::size_t size = jni::GetArrayLength(*env, *jarrayHoles); - for (std::size_t i = 0; i < size; i++) { - jni::jobject* hole = reinterpret_cast(jni::GetObjectArrayElement(*env, *jarrayHoles, i)); + for (std::size_t j = 0; j < size; j++) { + jni::jobject* hole = jni::GetObjectArrayElement(*env, *jarrayHoles, j); NullCheck(*env, hole); geometry.push_back(toGeometry>(env, hole)); jni::DeleteLocalRef(*env, hole);