Skip to content

Commit

Permalink
able to get a field from Polyline in JNI mapbox#1716
Browse files Browse the repository at this point in the history
  • Loading branch information
hallahan committed Jul 16, 2015
1 parent 441c84c commit 8107501
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
48 changes: 47 additions & 1 deletion android/cpp/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ jmethodID latLngConstructorId = nullptr;
jfieldID latLngLatitudeId = nullptr;
jfieldID latLngLongitudeId = nullptr;

jclass polylineClass = nullptr;
jmethodID polylineConstructorId = nullptr;
jfieldID polylineAlphaId = nullptr;

jclass latLngZoomClass = nullptr;
jmethodID latLngZoomConstructorId = nullptr;
jfieldID latLngZoomLatitudeId = nullptr;
Expand Down Expand Up @@ -460,8 +464,21 @@ jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr
assert(nativeMapViewPtr != 0);
// NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);

// ***** Java fields ***** //

// float alpha;
// boolean visible;
// List<LatLng> points
// int color
// float width

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

return -1;
return (jlong)alpha;
}

void JNICALL nativeRemoveAnnotation(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jlong annotationId) {
Expand Down Expand Up @@ -821,6 +838,24 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

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

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

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

latLngZoomClass = env->FindClass("com/mapbox/mapboxgl/geometry/LatLngZoom");
if (latLngZoomClass == nullptr) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -1069,6 +1104,12 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
return JNI_ERR;
}

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

latLngZoomClass = reinterpret_cast<jclass>(env->NewGlobalRef(latLngZoomClass));
if (latLngZoomClass == nullptr) {
env->ExceptionDescribe();
Expand Down Expand Up @@ -1152,6 +1193,11 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) {
latLngLongitudeId = nullptr;
latLngLatitudeId = nullptr;

env->DeleteGlobalRef(polylineClass);
polylineClass = nullptr;
polylineConstructorId = nullptr;
polylineAlphaId = nullptr;

env->DeleteGlobalRef(latLngZoomClass);
latLngZoomClass = nullptr;
latLngZoomConstructorId = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public long addMarker(Marker marker) {
}

public long addPolyline(Polyline polyline) {
// NH TODO Throw exception if returns -1
return nativeAddPolyline(mNativeMapViewPtr, polyline);
}

Expand Down

0 comments on commit 8107501

Please sign in to comment.