Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] Add hole handling to PolygonOptions
Browse files Browse the repository at this point in the history
- Added holes to the Java SDK, JNI, and renderer
  • Loading branch information
Neal Sanche authored and 1ec5 committed Feb 5, 2017
1 parent 3212e54 commit 3faa05c
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
public abstract class MultiPoint extends Annotation {

private List<LatLng> points;
private List<List<LatLng>> holes;
private float alpha = 1.0f;

protected MultiPoint() {
super();
points = new ArrayList<>();
holes = new ArrayList<>();
}

/**
Expand All @@ -27,6 +29,15 @@ public List<LatLng> getPoints() {
return new ArrayList<>(points);
}

/*
* Returns a copy of the holes.
*
* @return holes - as a copy
*/
public List<List<LatLng>> 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.
Expand All @@ -48,6 +59,10 @@ public void addPoint(LatLng point) {
update();
}

void addHole(List<LatLng> hole) {
holes.add(hole);
}

/**
* Value between 0 and 1 defining the polyline alpha.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public PolygonOptions addAll(Iterable<LatLng> points) {
return this;
}

public PolygonOptions addHole(Iterable<LatLng> points) {
List<LatLng> hole = new ArrayList<LatLng>();
for (LatLng point : points) {
hole.add(point);
}

polygon.addHole(hole);

return this;
}

/**
* Set the alpha value of the polyline.
*
Expand Down Expand Up @@ -182,6 +193,10 @@ public List<LatLng> getPoints() {
return polygon.getPoints();
}

public List<List<LatLng>> getHoles() {
return polygon.getHoles();
}

/**
* Compares this {@link PolygonOptions} object with another {@link PolygonOptions} and
* determines if their color, alpha, stroke color, and vertices match.
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -177,6 +179,38 @@ static final class Config {
}
};

static final List<List<LatLng>> STAR_SHAPE_HOLES = new ArrayList<ArrayList<LatLng>>() {
{
add(new ArrayList<LatLng>() {
{
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<LatLng>() {
{
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<LatLng>() {
{
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<LatLng> BROKEN_SHAPE_POINTS =
STAR_SHAPE_POINTS.subList(0, STAR_SHAPE_POINTS.size() - 3);
}
Expand Down
23 changes: 22 additions & 1 deletion platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -759,7 +760,26 @@ jni::jarray<jlong>* nativeAddPolygons(JNIEnv *env, jni::jobject* obj, jlong nati
jni::jobject* polygon = jni::GetObjectArrayElement(*env, *jarray, i);
jni::jobject* points = jni::GetField<jni::jobject*>(*env, polygon, *polygonPointsId);

mbgl::FillAnnotation annotation { mbgl::Polygon<double> { toGeometry<mbgl::LinearRing<double>>(env, points) } };
mbgl::Polygon<double> geometry { toGeometry<mbgl::LinearRing<double>>(env, points) };

jni::jobject* holes = jni::GetField<jni::jobject*>(*env, polygon, *polygonHolesId);
NullCheck(*env, holes);
jni::jarray<jni::jobject>* jarrayHoles =
reinterpret_cast<jni::jarray<jni::jobject>*>(jni::CallMethod<jni::jobject*>(*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::jobject*>(jni::GetObjectArrayElement(*env, *jarrayHoles, i));
NullCheck(*env, hole);
geometry.push_back(toGeometry<mbgl::LinearRing<double>>(env, hole));
jni::DeleteLocalRef(*env, hole);
}

jni::DeleteLocalRef(*env, jarrayHoles);
jni::DeleteLocalRef(*env, holes);

mbgl::FillAnnotation annotation { geometry };
annotation.opacity = { jni::GetField<jfloat>(*env, polygon, *polygonAlphaId) };
annotation.outlineColor = { toColor(jni::GetField<jint>(*env, polygon, *polygonStrokeColorId)) };
annotation.color = { toColor(jni::GetField<jint>(*env, polygon, *polygonFillColorId)) };
Expand Down Expand Up @@ -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;");
Expand Down

0 comments on commit 3faa05c

Please sign in to comment.