Skip to content

Commit

Permalink
polygon JNI mapbox#1716
Browse files Browse the repository at this point in the history
  • Loading branch information
hallahan committed Jul 17, 2015
1 parent c608020 commit 22ea82a
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 0 deletions.
162 changes: 162 additions & 0 deletions android/cpp/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ jfieldID polylineColorId = nullptr;
jfieldID polylineWidthId = nullptr;
jfieldID polylinePointsId = nullptr;

jclass polygonClass = nullptr;
jmethodID polygonConstructorId = nullptr;
jfieldID polygonAlphaId = nullptr;
jfieldID polygonVisibleId = nullptr;
jfieldID polygonFillColorId = nullptr;
jfieldID polygonStrokeColorId = nullptr;
jfieldID polygonStrokeWidthId = nullptr;
jfieldID polygonPointsId = nullptr;
jfieldID polygonHolesId = nullptr;


jclass latLngZoomClass = nullptr;
jmethodID latLngZoomConstructorId = nullptr;
jfieldID latLngZoomLatitudeId = nullptr;
Expand Down Expand Up @@ -574,6 +585,79 @@ jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr
return (jlong) id;
}

jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject polygon) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeAddPolygon");
assert(nativeMapViewPtr != 0);
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

// ***** Java fields ***** //
// float alpha;
// boolean visible;
// int fillColor
// int strokeColor
// float strokeWidth
// List<LatLng> points
// List<List<LatLng>> holes

jfloat alpha = env->GetFloatField(polygon, polygonAlphaId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return -1;
}

jboolean visible = env->GetBooleanField(polygon, polygonVisibleId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return -1;
}
visible = JNI_TRUE;

jint fillColor = env->GetIntField(polygon, polygonFillColorId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return -1;
}

jint strokeColor = env->GetIntField(polygon, polygonStrokeColorId);
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
return -1;
}

int rF = (fillColor>>16)&0xFF;
int gF = (fillColor>>8)&0xFF;
int bF = (fillColor)&0xFF;
int aF = (fillColor>>24)&0xFF;

int rS = (strokeColor>>16)&0xFF;
int gS = (strokeColor>>8)&0xFF;
int bS = (strokeColor)&0xFF;
int aS = (strokeColor>>24)&0xFF;

// jfloat strokeWidth = env->GetFloatField(polygon, polygonStrokeWidthId);
// if (env->ExceptionCheck()) {
// env->ExceptionDescribe();
// return -1;
// }

mbgl::StyleProperties shapeProperties;
mbgl::FillProperties fillProperties;
fillProperties.opacity = alpha;
fillProperties.stroke_color = {{ (float)rS, (float)gS, (float)bS, (float)aS }};
fillProperties.fill_color = {{ (float)rF, (float)gF, (float)bF, (float)aF }};
shapeProperties.set<mbgl::FillProperties>(fillProperties);

jobject points = env->GetObjectField(polygon, polygonPointsId);
mbgl::AnnotationSegment segment = annotation_segment_from_latlng_jlist(env, points);

std::vector<mbgl::ShapeAnnotation> shapes;
shapes.emplace_back(mbgl::AnnotationSegments {{ segment }}, shapeProperties);

std::vector<uint32_t> shapeAnnotationIDs = nativeMapView->getMap().addShapeAnnotations(shapes);
uint32_t id = shapeAnnotationIDs.at(0);
return (jlong) id;
}

void JNICALL nativeRemoveAnnotation(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jlong annotationId) {
mbgl::Log::Debug(mbgl::Event::JNI, "nativeRemoveAnnotation");
assert(nativeMapViewPtr != 0);
Expand Down Expand Up @@ -973,6 +1057,60 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

polygonClass = env->FindClass("com/mapbox/mapboxgl/annotations/Polygon");
if (polygonClass == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

polygonConstructorId = env->GetMethodID(polygonClass, "<init>", "()V");
if (polylineConstructorId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

polygonAlphaId = env->GetFieldID(polygonClass, "alpha", "F");
if (polygonAlphaId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

polygonVisibleId = env->GetFieldID(polygonClass, "visible", "Z");
if (polygonVisibleId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

polygonFillColorId = env->GetFieldID(polygonClass, "fillColor", "I");
if (polygonFillColorId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

polygonStrokeColorId = env->GetFieldID(polygonClass, "strokeColor", "I");
if (polygonStrokeColorId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

polygonStrokeWidthId = env->GetFieldID(polygonClass, "strokeWidth", "F");
if (polygonStrokeWidthId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

polygonPointsId = env->GetFieldID(polygonClass, "points", "Ljava/util/List;");
if (polygonPointsId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

polygonHolesId = env->GetFieldID(polygonClass, "holes", "Ljava/util/List;");
if (polygonHolesId == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

latLngZoomClass = env->FindClass("com/mapbox/mapboxgl/geometry/LatLngZoom");
if (latLngZoomClass == nullptr) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -1170,6 +1308,8 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
reinterpret_cast<void *>(&nativeAddMarker)},
{"nativeAddPolyline", "(JLcom/mapbox/mapboxgl/annotations/Polyline;)J",
reinterpret_cast<void *>(&nativeAddPolyline)},
{"nativeAddPolygon", "(JLcom/mapbox/mapboxgl/annotations/Polygon;)J",
reinterpret_cast<void *>(&nativeAddPolygon)},
{"nativeRemoveAnnotation", "(JJ)V", reinterpret_cast<void *>(&nativeRemoveAnnotation)},
{"nativeGetLatLng", "(J)Lcom/mapbox/mapboxgl/geometry/LatLng;",
reinterpret_cast<void *>(&nativeGetLatLng)},
Expand Down Expand Up @@ -1227,6 +1367,12 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

polygonClass = reinterpret_cast<jclass>(env->NewGlobalRef(polygonClass));
if (polygonClass == nullptr) {
env->ExceptionDescribe();
return JNI_ERR;
}

latLngZoomClass = reinterpret_cast<jclass>(env->NewGlobalRef(latLngZoomClass));
if (latLngZoomClass == nullptr) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -1314,6 +1460,22 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
polylineClass = nullptr;
polylineConstructorId = nullptr;
polylineAlphaId = nullptr;
polylineVisibleId = nullptr;
polylineColorId = nullptr;
polylineWidthId = nullptr;
polylinePointsId = nullptr;

env->DeleteGlobalRef(polygonClass);
polygonClass = nullptr;
polygonConstructorId = nullptr;
polygonAlphaId = nullptr;
polygonVisibleId = nullptr;
polygonFillColorId = nullptr;
polygonStrokeColorId = nullptr;
polygonStrokeWidthId = nullptr;
polygonPointsId = nullptr;
polygonHolesId = nullptr;


env->DeleteGlobalRef(latLngZoomClass);
latLngZoomClass = nullptr;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-122.28813171386719,38.617406963286136],[-122.26959228515624,38.6833657775237],[-122.18238830566406,38.55568323796419],[-122.10617065429688,38.51378825951165],[-122.11509704589845,38.50465406475561],[-122.18307495117188,38.542795073979015],[-122.19955444335938,38.496593518947556],[-122.28813171386719,38.617406963286136]]]}}]}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import com.mapbox.mapboxgl.annotations.Marker;
import com.mapbox.mapboxgl.annotations.MarkerOptions;
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.LatLng;
Expand Down Expand Up @@ -277,6 +279,7 @@ private void toggleMarkers(boolean enableMarkers) {
mIsMarkersOn = true;
addMarkers();
addPolyline();
addPolygon();
}
} else {
if (mIsMarkersOn) {
Expand Down Expand Up @@ -311,6 +314,23 @@ private void addPolyline() {
}
}

private void addPolygon() {
String geojsonStr = null;
try {
geojsonStr = Util.loadStringFromAssets(this, "small_polygon.geojson");
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
MapView map = mMapFragment.getMap();
Polygon polygon = map.addPolygon(new PolygonOptions()
.add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0))
.strokeColor(Color.MAGENTA)
.fillColor(Color.BLUE));
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}

private void removeAnnotations() {
mMapFragment.getMap().removeAnnotations();
}
Expand Down

0 comments on commit 22ea82a

Please sign in to comment.