summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPablo Guardiola <guardiola31337@gmail.com>2017-04-11 19:10:46 +0200
committerGitHub <noreply@github.com>2017-04-11 19:10:46 +0200
commitb71d86599d5e7c265b320300b18cfc0ea082c6d2 (patch)
treec5e45f36dc6fd4fcaaf57799a81b131805804913
parent1d33bf774f7d0248a72529840e155c4716a99851 (diff)
downloadqtlocation-mapboxgl-b71d86599d5e7c265b320300b18cfc0ea082c6d2.tar.gz
[android] Polygon holes (#8557)
* add 1 hole support to polygon-related classes * fix no hole crash and add triangle hole shape example * add support for multiple holes
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Hole.java53
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Polygon.java37
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/PolygonOptions.java66
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/PolygonActivity.java30
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_polygon.xml4
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml1
-rw-r--r--platform/android/src/annotation/polygon.cpp20
-rw-r--r--platform/android/src/annotation/polygon.hpp2
8 files changed, 208 insertions, 5 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Hole.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Hole.java
new file mode 100644
index 0000000000..cabe16cc5c
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Hole.java
@@ -0,0 +1,53 @@
+package com.mapbox.mapboxsdk.annotations;
+
+
+import android.os.Parcel;
+import android.os.Parcelable;
+
+import com.mapbox.mapboxsdk.geometry.LatLng;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Encapsulates a {@link List} of {@link LatLng} points defining a hole
+ */
+public class Hole extends ArrayList<LatLng> implements Parcelable {
+
+ public Hole() {
+ super();
+ }
+
+ /**
+ * Creates a Hole.
+ *
+ * @param holePoints {@link List} list of {@link LatLng} points defining a hole
+ */
+ public Hole(List<LatLng> holePoints) {
+ super(holePoints);
+ }
+
+ protected Hole(Parcel in) {
+ in.readTypedList(this, LatLng.CREATOR);
+ }
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ @Override
+ public void writeToParcel(Parcel parcel, int i) {
+ parcel.writeTypedList(this);
+ }
+
+ public static final Parcelable.Creator<Hole> CREATOR = new Parcelable.Creator<Hole>() {
+ public Hole createFromParcel(Parcel in) {
+ return new Hole(in);
+ }
+
+ public Hole[] newArray(int size) {
+ return new Hole[size];
+ }
+ };
+}
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 4a72cb7d89..9245d9cdc8 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
@@ -4,6 +4,9 @@ import android.graphics.Color;
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.
*/
@@ -11,9 +14,11 @@ public final class Polygon extends BasePointCollection {
private int fillColor = Color.BLACK; // default fillColor is black
private int strokeColor = Color.BLACK; // default strokeColor is black
+ private List<Hole> holes;
Polygon() {
super();
+ holes = new ArrayList<>();
}
/**
@@ -26,7 +31,7 @@ public final class Polygon extends BasePointCollection {
}
/**
- * Get the color fo the stroke of the polygon.
+ * Get the color of the stroke of the polygon.
*
* @return The color of the stroke.
*/
@@ -35,6 +40,15 @@ public final class Polygon extends BasePointCollection {
}
/**
+ * Returns a copy of the holes.
+ *
+ * @return A {@link List} of holes.
+ */
+ public List<Hole> getHoles() {
+ return new ArrayList<>(holes);
+ }
+
+ /**
* Sets the color of the fill region of the polygon.
*
* @param color The color in ARGB format.
@@ -54,6 +68,27 @@ public final class Polygon extends BasePointCollection {
update();
}
+ /**
+ * 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 holes A {@link List} of {@link Hole} points making up the holes.
+ */
+ public void setHoles(List<? extends Hole> holes) {
+ this.holes = new ArrayList<>(holes);
+ update();
+ }
+
+ /**
+ * Add a hole to the polygon.
+ *
+ * @param hole A {@link Hole} hole to be added.
+ */
+ void addHole(Hole hole) {
+ holes.add(hole);
+ update();
+ }
+
@Override
void update() {
MapboxMap mapboxMap = getMapboxMap();
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 22f1258fc7..7405ebfb4a 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
@@ -30,6 +30,9 @@ public final class PolygonOptions implements Parcelable {
ArrayList<LatLng> pointsList = new ArrayList<>();
in.readList(pointsList, LatLng.class.getClassLoader());
addAll(pointsList);
+ ArrayList<Hole> holes = new ArrayList<>();
+ in.readTypedList(holes, Hole.CREATOR);
+ addAllHoles(holes);
alpha(in.readFloat());
fillColor(in.readInt());
strokeColor(in.readInt());
@@ -56,6 +59,7 @@ public final class PolygonOptions implements Parcelable {
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeList(getPoints());
+ out.writeTypedList(getHoles());
out.writeFloat(getAlpha());
out.writeInt(getFillColor());
out.writeInt(getStrokeColor());
@@ -109,6 +113,43 @@ public final class PolygonOptions implements Parcelable {
}
/**
+ * Adds a hole to the outline of the polygon being built.
+ *
+ * @param hole {@link Hole} 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(Hole hole) {
+ polygon.addHole(hole);
+ return this;
+ }
+
+ /**
+ * Adds holes to the outline of the polygon being built.
+ *
+ * @param holes {@link Hole} holes to be added to polygon geometry.
+ * @return This {@link PolygonOptions} object with the given holes added to the outline.
+ */
+ public PolygonOptions addHole(Hole... holes) {
+ for (Hole hole : holes) {
+ addHole(hole);
+ }
+ return this;
+ }
+
+ /**
+ * Adds holes to the outline of the polygon being built.
+ *
+ * @param holes {@link Iterable} list made up of {@link Hole} holes defining the hole geometry
+ * @return This {@link PolygonOptions} object with the given holes added to the outline.
+ */
+ public PolygonOptions addAllHoles(Iterable<Hole> holes) {
+ for (Hole hole : holes) {
+ addHole(hole);
+ }
+ return this;
+ }
+
+ /**
* Set the alpha value of the polyline.
*
* @param alpha float value between 0 (not visible) and 1.
@@ -177,18 +218,33 @@ public final class PolygonOptions implements Parcelable {
return polygon.getStrokeColor();
}
+ /**
+ * Gets the points set for this {@link PolygonOptions} object.
+ *
+ * @return The list made up of {@link LatLng} points defining the polygon.
+ */
public List<LatLng> getPoints() {
// the getter gives us a copy, which is the safe thing to do...
return polygon.getPoints();
}
/**
+ * Gets the holes set for this {@link PolygonOptions} object.
+ *
+ * @return The list made up of {@link List} of {@link LatLng} points defining the holes.
+ */
+ public List<Hole> getHoles() {
+ return polygon.getHoles();
+ }
+
+
+ /**
* Compares this {@link PolygonOptions} object with another {@link PolygonOptions} and
* determines if their color, alpha, stroke color, and vertices 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}
+ * {@link PolygonOptions} object. Else, false.
*/
@Override
public boolean equals(Object o) {
@@ -210,7 +266,10 @@ public final class PolygonOptions implements Parcelable {
if (getStrokeColor() != polygon.getStrokeColor()) {
return false;
}
- return !(getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null);
+ if (getPoints() != null ? !getPoints().equals(polygon.getPoints()) : polygon.getPoints() != null) {
+ return false;
+ }
+ return !(getHoles() != null ? !getHoles().equals(polygon.getHoles()) : polygon.getHoles() != null);
}
/**
@@ -228,6 +287,7 @@ public final class PolygonOptions implements Parcelable {
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 a2245a28e0..429509e1d2 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
@@ -6,6 +6,7 @@ import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
+import com.mapbox.mapboxsdk.annotations.Hole;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.PolygonOptions;
import com.mapbox.mapboxsdk.camera.CameraPosition;
@@ -18,6 +19,7 @@ import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.testapp.R;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.BLUE_COLOR;
@@ -26,6 +28,7 @@ import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.C
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.NO_ALPHA;
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_HOLES;
import static com.mapbox.mapboxsdk.testapp.activity.annotation.PolygonActivity.Config.STAR_SHAPE_POINTS;
/**
@@ -44,6 +47,7 @@ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCall
private boolean visible = true;
private boolean color = true;
private boolean allPoints;
+ private boolean holes;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -140,6 +144,10 @@ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCall
color = !color;
polygon.setFillColor(color ? BLUE_COLOR : RED_COLOR);
return true;
+ case R.id.action_id_holes:
+ holes = !holes;
+ polygon.setHoles(holes ? STAR_SHAPE_HOLES : Collections.<Hole>emptyList());
+ return true;
default:
return super.onOptionsItemSelected(item);
}
@@ -179,5 +187,27 @@ public class PolygonActivity extends AppCompatActivity implements OnMapReadyCall
static final List<LatLng> BROKEN_SHAPE_POINTS =
STAR_SHAPE_POINTS.subList(0, STAR_SHAPE_POINTS.size() - 3);
+
+ static final List<? extends Hole> STAR_SHAPE_HOLES = new ArrayList<Hole>() {
+ {
+ add(new Hole(new ArrayList<LatLng>() {
+ {
+ add(new LatLng(45.521743, -122.669091));
+ add(new LatLng(45.530483, -122.676833));
+ add(new LatLng(45.520483, -122.676833));
+ add(new LatLng(45.521743, -122.669091));
+ }
+ }));
+ add(new Hole(new ArrayList<LatLng>() {
+ {
+ add(new LatLng(45.529743, -122.662791));
+ add(new LatLng(45.525543, -122.662791));
+ add(new LatLng(45.525543, -122.660));
+ add(new LatLng(45.527743, -122.660));
+ add(new LatLng(45.529743, -122.662791));
+ }
+ }));
+ }
+ };
}
}
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_polygon.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_polygon.xml
index a7fdf56be5..f2cd9aedbc 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_polygon.xml
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_polygon.xml
@@ -17,4 +17,8 @@
android:id="@+id/action_id_color"
android:title="@string/action_color_polygon"
mapbox:showAsAction="never" />
+ <item
+ android:id="@+id/action_id_holes"
+ android:title="@string/action_holes_polygon"
+ mapbox:showAsAction="never" />
</menu>
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml
index ac5c528c38..d2442fb161 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml
@@ -132,6 +132,7 @@
<string name="action_alpha_polygon">Change alpha</string>
<string name="action_points_polygon">Change points</string>
<string name="action_color_polygon">Change color</string>
+ <string name="action_holes_polygon">Change holes</string>
<string name="action_width_polyline">Change width</string>
<!--Menu-->
diff --git a/platform/android/src/annotation/polygon.cpp b/platform/android/src/annotation/polygon.cpp
index ba82fc34dc..fbd849432a 100644
--- a/platform/android/src/annotation/polygon.cpp
+++ b/platform/android/src/annotation/polygon.cpp
@@ -9,13 +9,26 @@ jni::Class<Polygon> Polygon::javaClass;
mbgl::FillAnnotation Polygon::toAnnotation(jni::JNIEnv& env, jni::Object<Polygon> polygon) {
auto points = Polygon::getPoints(env, polygon);
+ auto holes = Polygon::getHoles(env, polygon);
- mbgl::FillAnnotation annotation { mbgl::Polygon<double> { MultiPoint::toGeometry<mbgl::LinearRing<double>>(env, points) } };
+ mbgl::Polygon<double> geometry { MultiPoint::toGeometry<mbgl::LinearRing<double>>(env, points) };
+
+ auto jHoleListsArray = java::util::List::toArray<java::util::List>(env, holes);
+ std::size_t jHoleListsSize = jHoleListsArray.Length(env);
+ for (std::size_t i = 0; i < jHoleListsSize; i++) {
+ auto jHoleList = jHoleListsArray.Get(env, i);
+ geometry.push_back(MultiPoint::toGeometry<mbgl::LinearRing<double>>(env, jHoleList));
+ jni::DeleteLocalRef(env, jHoleList);
+ }
+ jni::DeleteLocalRef(env, jHoleListsArray);
+
+ mbgl::FillAnnotation annotation { geometry };
annotation.opacity = { Polygon::getOpacity(env, polygon) };
annotation.color = { Polygon::getFillColor(env, polygon) };
annotation.outlineColor = { Polygon::getOutlineColor(env, polygon) };
jni::DeleteLocalRef(env, points);
+ jni::DeleteLocalRef(env, holes);
return annotation;
}
@@ -25,6 +38,11 @@ jni::Object<java::util::List> Polygon::getPoints(jni::JNIEnv& env, jni::Object<P
return polygon.Get(env, field);
}
+jni::Object<java::util::List> Polygon::getHoles(jni::JNIEnv& env, jni::Object<Polygon> polygon) {
+ static auto field = Polygon::javaClass.GetField<jni::Object<java::util::List>>(env, "holes");
+ return polygon.Get(env, field);
+}
+
float Polygon::getOpacity(jni::JNIEnv& env, jni::Object<Polygon> polygon) {
static auto field = Polygon::javaClass.GetField<float>(env, "alpha");
return polygon.Get(env, field);
diff --git a/platform/android/src/annotation/polygon.hpp b/platform/android/src/annotation/polygon.hpp
index 658aa5344b..a98b2822cf 100644
--- a/platform/android/src/annotation/polygon.hpp
+++ b/platform/android/src/annotation/polygon.hpp
@@ -28,6 +28,8 @@ private:
static jni::Object<java::util::List> getPoints(jni::JNIEnv&, jni::Object<Polygon>);
+ static jni::Object<java::util::List> getHoles(jni::JNIEnv&, jni::Object<Polygon>);
+
static float getOpacity(jni::JNIEnv&, jni::Object<Polygon>);
static mbgl::Color getFillColor(jni::JNIEnv&, jni::Object<Polygon>);