diff options
author | Nicholas Hallahan <nick@theoutpost.io> | 2015-07-16 17:38:23 -0700 |
---|---|---|
committer | Nicholas Hallahan <nick@theoutpost.io> | 2015-07-16 17:38:23 -0700 |
commit | e84e13a85aab93d8e91dc8c94799a1076cfb892d (patch) | |
tree | 752f97a0725d6744ec71f1ecfbd58cf9da7c96e4 | |
parent | 0df1d6482375693f8c341b55eaab4ddf7783a88c (diff) | |
download | qtlocation-mapboxgl-e84e13a85aab93d8e91dc8c94799a1076cfb892d.tar.gz |
polygon JNI #1716
3 files changed, 183 insertions, 0 deletions
diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp index b98db228fa..7f4a817bdc 100644 --- a/android/cpp/jni.cpp +++ b/android/cpp/jni.cpp @@ -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; @@ -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); @@ -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(); @@ -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)}, @@ -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(); @@ -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; diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/main/assets/small_poly.geojson b/android/java/MapboxGLAndroidSDKTestApp/src/main/assets/small_poly.geojson new file mode 100644 index 0000000000..ec61303c72 --- /dev/null +++ b/android/java/MapboxGLAndroidSDKTestApp/src/main/assets/small_poly.geojson @@ -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]]]}}]}
\ No newline at end of file diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java index 4c25e241e0..a1cd2e660b 100644 --- a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java +++ b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java @@ -26,6 +26,8 @@ import android.widget.TextView; 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; @@ -277,6 +279,7 @@ public class MainActivity extends ActionBarActivity { mIsMarkersOn = true; addMarkers(); addPolyline(); + addPolygon(); } } else { if (mIsMarkersOn) { @@ -311,6 +314,23 @@ public class MainActivity extends ActionBarActivity { } } + 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(); } |