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 Dec 13, 2016
1 parent b1e9a3f commit c356dd7
Show file tree
Hide file tree
Showing 4 changed files with 97 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,14 @@ 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 +58,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 All @@ -200,6 +215,7 @@ public boolean equals(Object o) {
if (Float.compare(polygon.getAlpha(), getAlpha()) != 0) return false;
if (getFillColor() != polygon.getFillColor()) return false;
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 @@ -218,6 +234,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 @@ -148,6 +148,50 @@ public boolean onOptionsItemSelected(MenuItem item) {
case R.id.action_id_points:
allPoints = !allPoints;
polygon.setPoints(allPoints ? STAR_SHAPE_POINTS : BROKEN_SHAPE_POINTS);

double lat = 0;
double lon = 0;
int points = 0;
for (LatLng latLng : polygon) {
points++;
lat += latLng.getLatitude();
lon += latLng.getLongitude();
}

lat = lat / points;
lon = lon / points;

List<LatLng> hole1 = new ArrayList<>();
hole1.add(new LatLng(lat, lon));
hole1.add(new LatLng(lat + 0.01, lon));
hole1.add(new LatLng(lat + 0.01, lon + 0.01));
hole1.add(new LatLng(lat, lon + 0.01));
hole1.add(new LatLng(lat, lon));

lat = lat - 0.001;
lon = lon - 0.001;

List<LatLng> hole2 = new ArrayList<>();
hole2.add(new LatLng(lat, lon));
hole2.add(new LatLng(lat - 0.005, lon));
hole2.add(new LatLng(lat - 0.005, lon - 0.005));
hole2.add(new LatLng(lat, lon - 0.005));
hole2.add(new LatLng(lat, lon));

lat = lat + 0.008;
lon = lon - 0.01;

List<LatLng> hole3 = new ArrayList<>();
hole3.add(new LatLng(lat, lon));
hole3.add(new LatLng(lat - 0.005, lon));
hole3.add(new LatLng(lat - 0.005, lon - 0.005));
hole3.add(new LatLng(lat, lon - 0.005));
hole3.add(new LatLng(lat, lon));

polygon.addHole(hole1)
.addHole(hole2)
.addHole(hole3);

return true;

case R.id.action_id_color:
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 @@ -87,6 +87,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 @@ -757,7 +758,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 @@ -1765,6 +1785,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 c356dd7

Please sign in to comment.