diff options
author | Leith Bade <leith@mapbox.com> | 2015-08-04 12:45:10 +1000 |
---|---|---|
committer | Leith Bade <leith@mapbox.com> | 2015-08-04 13:52:08 +1000 |
commit | ab7456075c7744c8528c2b86cfa77f80d6a04ef1 (patch) | |
tree | 6ec0d8584c6ceaad84888dbd8055295ea33ae33a /android | |
parent | 02527ff3f9ac8e7273745061c24ce8e45c1cf801 (diff) | |
download | qtlocation-mapboxgl-ab7456075c7744c8528c2b86cfa77f80d6a04ef1.tar.gz |
First pass at tidying up Android annotation code
Diffstat (limited to 'android')
4 files changed, 411 insertions, 415 deletions
diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp index 85022dbc0d..eee6bd8f6d 100644 --- a/android/cpp/jni.cpp +++ b/android/cpp/jni.cpp @@ -42,6 +42,12 @@ jmethodID latLngConstructorId = nullptr; jfieldID latLngLatitudeId = nullptr; jfieldID latLngLongitudeId = nullptr; +jclass latLngZoomClass = nullptr; +jmethodID latLngZoomConstructorId = nullptr; +jfieldID latLngZoomLatitudeId = nullptr; +jfieldID latLngZoomLongitudeId = nullptr; +jfieldID latLngZoomZoomId = nullptr; + jclass markerClass = nullptr; jmethodID markerConstructorId = nullptr; jfieldID markerPositionId = nullptr; @@ -65,13 +71,6 @@ jfieldID polygonStrokeWidthId = nullptr; jfieldID polygonPointsId = nullptr; jfieldID polygonHolesId = nullptr; - -jclass latLngZoomClass = nullptr; -jmethodID latLngZoomConstructorId = nullptr; -jfieldID latLngZoomLatitudeId = nullptr; -jfieldID latLngZoomLongitudeId = nullptr; -jfieldID latLngZoomZoomId = nullptr; - jclass runtimeExceptionClass = nullptr; jclass nullPointerExceptionClass = nullptr; @@ -156,21 +155,21 @@ std::vector<std::string> std_vector_string_from_jobject(JNIEnv *env, jobject jli return vector; } - jobjectArray array = + jobjectArray jarray = reinterpret_cast<jobjectArray>(env->CallObjectMethod(jlist, listToArrayId)); - if (env->ExceptionCheck() || (array == nullptr)) { + if (env->ExceptionCheck() || (jarray == nullptr)) { env->ExceptionDescribe(); return vector; } - jsize len = env->GetArrayLength(array); + jsize len = env->GetArrayLength(jarray); if (len < 0) { env->ExceptionDescribe(); return vector; } for (jsize i = 0; i < len; i++) { - jstring jstr = reinterpret_cast<jstring>(env->GetObjectArrayElement(array, i)); + jstring jstr = reinterpret_cast<jstring>(env->GetObjectArrayElement(jarray, i)); if (jstr == nullptr) { env->ExceptionDescribe(); return vector; @@ -179,9 +178,51 @@ std::vector<std::string> std_vector_string_from_jobject(JNIEnv *env, jobject jli vector.push_back(std_string_from_jstring(env, jstr)); } + env->DeleteLocalRef(jarray); + jarray = nullptr; + return vector; } +jobject std_vector_string_to_jobject(JNIEnv *env, std::vector<std::string> vector) { + jobject jlist = env->NewObject(arrayListClass, arrayListConstructorId); + if (jlist == nullptr) { + env->ExceptionDescribe(); + return nullptr; + } + + for (const auto& str : vector) { + env->CallBooleanMethod(jlist, arrayListAddId, std_string_to_jstring(env, str)); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + return nullptr; + } + } + + return jlist; +} + +jlongArray std_vector_uint_to_jobject(JNIEnv *env, std::vector<uint32_t> vector) { + jlongArray jarray = env->NewLongArray(vector.size()); + if (jarray == nullptr) { + env->ExceptionDescribe(); + return nullptr; + } + + std::vector<jlong> v; + for (const uint32_t& id : vector) { + v.push_back((jlong)id); + } + + env->SetLongArrayRegion(jarray, 0, v.size(), &(v[0])); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + return nullptr; + } + + return jarray; +} + mbgl::AnnotationSegment annotation_segment_from_latlng_jlist(JNIEnv *env, jobject jlist) { mbgl::AnnotationSegment segment; @@ -193,14 +234,14 @@ mbgl::AnnotationSegment annotation_segment_from_latlng_jlist(JNIEnv *env, jobjec return segment; } - jobjectArray array = + jobjectArray jarray = reinterpret_cast<jobjectArray>(env->CallObjectMethod(jlist, listToArrayId)); - if (env->ExceptionCheck() || (array == nullptr)) { + if (env->ExceptionCheck() || (jarray == nullptr)) { env->ExceptionDescribe(); return segment; } - jsize len = env->GetArrayLength(array); + jsize len = env->GetArrayLength(jarray); if (len < 0) { env->ExceptionDescribe(); return segment; @@ -209,7 +250,7 @@ mbgl::AnnotationSegment annotation_segment_from_latlng_jlist(JNIEnv *env, jobjec segment.reserve(len); for (jsize i = 0; i < len; i++) { - jobject latLng = reinterpret_cast<jobject>(env->GetObjectArrayElement(array, i)); + jobject latLng = reinterpret_cast<jobject>(env->GetObjectArrayElement(jarray, i)); if (latLng == nullptr) { env->ExceptionDescribe(); return segment; @@ -230,47 +271,41 @@ mbgl::AnnotationSegment annotation_segment_from_latlng_jlist(JNIEnv *env, jobjec segment.push_back(mbgl::LatLng(latitude, longitude)); env->DeleteLocalRef(latLng); } - env->DeleteLocalRef(array); + + env->DeleteLocalRef(jarray); + jarray = nullptr; + return segment; } -jobject std_vector_string_to_jobject(JNIEnv *env, std::vector<std::string> vector) { - jobject jlist = env->NewObject(arrayListClass, arrayListConstructorId); - if (jlist == nullptr) { - env->ExceptionDescribe(); - return nullptr; - } - - for (const auto& str : vector) { - env->CallBooleanMethod(jlist, arrayListAddId, std_string_to_jstring(env, str)); - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - return nullptr; - } - } +std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> annotation_std_pair_from_polygon_jobject(JNIEnv *env, jobject polygon) { + jfloat alpha = env->GetFloatField(polygon, polygonAlphaId); + //jboolean visible = env->GetBooleanField(polygon, polygonVisibleId); + jint fillColor = env->GetIntField(polygon, polygonFillColorId); + jint strokeColor = env->GetIntField(polygon, polygonStrokeColorId); - return jlist; -} + int rF = (fillColor >> 16) & 0xFF; + int gF = (fillColor >> 8) & 0xFF; + int bF = (fillColor) & 0xFF; + int aF = (fillColor >> 24) & 0xFF; -jlongArray std_vector_uint_to_jobject(JNIEnv *env, std::vector<uint32_t> vector) { - jlongArray jarray = env->NewLongArray(vector.size()); - if (jarray == nullptr) { - env->ExceptionDescribe(); - return nullptr; - } + int rS = (strokeColor >> 16) & 0xFF; + int gS = (strokeColor >> 8) & 0xFF; + int bS = (strokeColor) & 0xFF; + int aS = (strokeColor >> 24) & 0xFF; - std::vector<jlong> v; - for (const uint32_t& id : vector) { - v.push_back((jlong)id); - } + mbgl::StyleProperties shapeProperties; + mbgl::FillProperties fillProperties; + fillProperties.opacity = alpha; + fillProperties.stroke_color = {{ static_cast<float>(rS) / 255.0f, static_cast<float>(gS) / 255.0f, static_cast<float>(bS) / 255.0f, static_cast<float>(aS) / 255.0f }}; + fillProperties.fill_color = {{ static_cast<float>(rF) / 255.0f, static_cast<float>(gF) / 255.0f, static_cast<float>(bF) / 255.0f, static_cast<float>(aF) / 255.0f }}; + shapeProperties.set<mbgl::FillProperties>(fillProperties); - env->SetLongArrayRegion(jarray, 0, v.size(), &(v[0])); - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - return nullptr; - } + jobject points = env->GetObjectField(polygon, polygonPointsId); + mbgl::AnnotationSegment segment = annotation_segment_from_latlng_jlist(env, points); + env->DeleteLocalRef(points); - return jarray; + return std::make_pair(segment, shapeProperties); } } @@ -530,25 +565,165 @@ void JNICALL nativeSetLatLng(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, j nativeMapView->getMap().setLatLng(mbgl::LatLng(latitude, longitude), std::chrono::milliseconds(duration)); } -void JNICALL nativeSetSprite(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, - jstring symbol, jint width, jint height, jfloat scale, jbyteArray jpixels) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetSprite"); +jobject JNICALL nativeGetLatLng(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetLatLng"); assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + mbgl::LatLng latLng = nativeMapView->getMap().getLatLng(); - const std::string symbolName = std_string_from_jstring(env, symbol); + jobject ret = env->NewObject(latLngClass, latLngConstructorId, latLng.latitude, latLng.longitude); + if (ret == nullptr) { + env->ExceptionDescribe(); + return nullptr; + } - jbyte* pixelData = env->GetByteArrayElements(jpixels, nullptr); - std::string pixels((char*)pixelData, width * height * 4); - env->ReleaseByteArrayElements(jpixels, pixelData, JNI_ABORT); + return ret; +} - auto spriteImage = std::make_shared<mbgl::SpriteImage>( - uint16_t(width), - uint16_t(height), - float(scale), - std::move(pixels)); +void JNICALL nativeResetPosition(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeResetPosition"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().resetPosition(); +} - nativeMapView->getMap().setSprite(symbolName, spriteImage); +void JNICALL nativeScaleBy(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble ds, jdouble cx, + jdouble cy, jlong duration) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeScaleBy"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().scaleBy(ds, cx, cy, std::chrono::milliseconds(duration)); +} + +void JNICALL nativeSetScale(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble scale, + jdouble cx, jdouble cy, jlong duration) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetScale"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().setScale(scale, cx, cy, std::chrono::milliseconds(duration)); +} + +jdouble JNICALL nativeGetScale(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetScale"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + return nativeMapView->getMap().getScale(); +} + +void JNICALL nativeSetZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble zoom, jlong duration) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetZoom"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().setZoom(zoom, std::chrono::milliseconds(duration)); +} + +jdouble JNICALL nativeGetZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetZoom"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + return nativeMapView->getMap().getZoom(); +} + +void JNICALL nativeSetLatLngZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, + jobject latLngZoom, jlong duration) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetLatLngZoom"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + + jdouble latitude = env->GetDoubleField(latLngZoom, latLngZoomLatitudeId); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + return; + } + + jdouble longitude = env->GetDoubleField(latLngZoom, latLngZoomLongitudeId); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + return; + } + + jdouble zoom = env->GetDoubleField(latLngZoom, latLngZoomZoomId); + if (env->ExceptionCheck()) { + env->ExceptionDescribe(); + return; + } + + nativeMapView->getMap().setLatLngZoom(mbgl::LatLng(latitude, longitude), zoom, std::chrono::milliseconds(duration)); +} + +jobject JNICALL nativeGetLatLngZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetLatLngZoom"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + mbgl::LatLng latLng = nativeMapView->getMap().getLatLng(); + jdouble zoom = nativeMapView->getMap().getZoom(); + + jobject ret = env->NewObject(latLngZoomClass, latLngZoomConstructorId, latLng.longitude, latLng.latitude, zoom); + if (ret == nullptr) { + env->ExceptionDescribe(); + return nullptr; + } + + return ret; +} + +void JNICALL nativeResetZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeResetZoom"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().resetZoom(); +} + +jdouble JNICALL nativeGetMinZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetMinZoom"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + return nativeMapView->getMap().getMinZoom(); +} + +jdouble JNICALL nativeGetMaxZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetMaxZoom"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + return nativeMapView->getMap().getMaxZoom(); +} + +void JNICALL nativeRotateBy(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble sx, + jdouble sy, jdouble ex, jdouble ey, jlong duration) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeRotateBy"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().rotateBy(sx, sy, ex, ey, std::chrono::milliseconds(duration)); +} + +void JNICALL nativeSetBearing(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble degrees, + jlong duration) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetBearing"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().setBearing(degrees, std::chrono::milliseconds(duration)); +} + +void JNICALL nativeSetBearing(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble degrees, + jdouble cx, jdouble cy) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetBearing"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().setBearing(degrees, cx, cy); +} + +jdouble JNICALL nativeGetBearing(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetBearing"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + return nativeMapView->getMap().getBearing(); +} + +void JNICALL nativeResetNorth(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeResetNorth"); + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + nativeMapView->getMap().resetNorth(); } jlong JNICALL nativeAddMarker(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject marker) { @@ -562,7 +737,7 @@ jlong JNICALL nativeAddMarker(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, return -1; } - jstring jsprite = (jstring)env->GetObjectField(marker, markerSpriteId); + jstring jsprite = reinterpret_cast<jstring>(env->GetObjectField(marker, markerSpriteId)); std::string sprite = std_string_from_jstring(env, jsprite); jdouble latitude = env->GetDoubleField(position, latLngLatitudeId); @@ -578,7 +753,7 @@ jlong JNICALL nativeAddMarker(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, } // Because Java only has int, not unsigned int, we need to bump the annotation id up to a long. - return (jlong) nativeMapView->getMap().addPointAnnotation(mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), sprite)); + return nativeMapView->getMap().addPointAnnotation(mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), sprite)); } jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject polyline) { @@ -586,25 +761,17 @@ jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - // ***** Java fields ***** // - // float alpha; - // boolean visible; - // int color - // float width - // List<LatLng> points - jfloat alpha = env->GetFloatField(polyline, polylineAlphaId); if (env->ExceptionCheck()) { env->ExceptionDescribe(); return -1; } - jboolean visible = env->GetBooleanField(polyline, polylineVisibleId); + /*jboolean visible = env->GetBooleanField(polyline, polylineVisibleId); if (env->ExceptionCheck()) { env->ExceptionDescribe(); return -1; - } - visible = JNI_TRUE; + }*/ jint color = env->GetIntField(polyline, polylineColorId); if (env->ExceptionCheck()) { @@ -612,10 +779,10 @@ jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr return -1; } - int r = (color>>16)&0xFF; - int g = (color>>8)&0xFF; - int b = (color)&0xFF; - int a = (color>>24)&0xFF; + int r = (color >> 16) & 0xFF; + int g = (color >> 8) & 0xFF; + int b = (color) & 0xFF; + int a = (color >> 24) & 0xFF; jfloat width = env->GetFloatField(polyline, polylineWidthId); if (env->ExceptionCheck()) { @@ -626,7 +793,7 @@ jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr mbgl::StyleProperties shapeProperties; mbgl::LineProperties lineProperties; lineProperties.opacity = alpha; - lineProperties.color = {{ (float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, (float)a / 255.0f }}; + lineProperties.color = {{ static_cast<float>(r) / 255.0f, static_cast<float>(g) / 255.0f, static_cast<float>(b) / 255.0f, static_cast<float>(a) / 255.0f }}; lineProperties.width = width; shapeProperties.set<mbgl::LineProperties>(lineProperties); @@ -634,58 +801,12 @@ jlong JNICALL nativeAddPolyline(JNIEnv *env, jobject obj, jlong nativeMapViewPtr mbgl::AnnotationSegment segment = annotation_segment_from_latlng_jlist(env, points); std::vector<mbgl::ShapeAnnotation> shapes; - shapes.emplace_back(mbgl::AnnotationSegments {{ segment }}, shapeProperties); + 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; -} - - -std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> readPolygon(JNIEnv *env, jobject polygon) { - // ***** 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); - jboolean visible = env->GetBooleanField(polygon, polygonVisibleId); - visible = JNI_TRUE; - jint fillColor = env->GetIntField(polygon, polygonFillColorId); - jint strokeColor = env->GetIntField(polygon, polygonStrokeColorId); - - 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 / 255.0f, (float)gS / 255.0f, (float)bS / 255.0f, (float)aS / 255.0f }}; - fillProperties.fill_color = {{ (float)rF / 255.0f, (float)gF / 255.0f, (float)bF / 255.0f, (float)aF / 255.0f }}; - shapeProperties.set<mbgl::FillProperties>(fillProperties); - - jobject points = env->GetObjectField(polygon, polygonPointsId); - mbgl::AnnotationSegment segment = annotation_segment_from_latlng_jlist(env, points); - env->DeleteLocalRef(points); - return std::make_pair(segment, shapeProperties); + return id; } jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject polygon) { @@ -694,18 +815,18 @@ jlong JNICALL nativeAddPolygon(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); std::vector<mbgl::ShapeAnnotation> shapes; - std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> segment = readPolygon(env, polygon); + std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> segment = annotation_std_pair_from_polygon_jobject(env, polygon); if (env->ExceptionCheck()) { env->ExceptionDescribe(); return -1; } - shapes.emplace_back(mbgl::AnnotationSegments {{ segment.first }}, segment.second); + shapes.emplace_back(mbgl::AnnotationSegments { segment.first }, segment.second); std::vector<uint32_t> shapeAnnotationIDs = nativeMapView->getMap().addShapeAnnotations(shapes); uint32_t id = shapeAnnotationIDs.at(0); - return (jlong) id; + return id; } jlongArray JNICALL nativeAddPolygons(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jobject jlist) { @@ -723,14 +844,14 @@ jlongArray JNICALL nativeAddPolygons(JNIEnv *env, jobject obj, jlong nativeMapVi return nullptr; } - jobjectArray array = + jobjectArray jarray = reinterpret_cast<jobjectArray>(env->CallObjectMethod(jlist, listToArrayId)); - if (env->ExceptionCheck() || (array == nullptr)) { + if (env->ExceptionCheck() || (jarray == nullptr)) { env->ExceptionDescribe(); return nullptr; } - jsize len = env->GetArrayLength(array); + jsize len = env->GetArrayLength(jarray); if (len < 0) { env->ExceptionDescribe(); return nullptr; @@ -739,19 +860,21 @@ jlongArray JNICALL nativeAddPolygons(JNIEnv *env, jobject obj, jlong nativeMapVi shapes.reserve(len); for (jsize i = 0; i < len; i++) { - jobject polygon = reinterpret_cast<jobject>(env->GetObjectArrayElement(array, i)); + jobject polygon = reinterpret_cast<jobject>(env->GetObjectArrayElement(jarray, i)); - std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> segment = readPolygon(env, polygon); + std::pair<mbgl::AnnotationSegment, mbgl::StyleProperties> segment = annotation_std_pair_from_polygon_jobject(env, polygon); if (env->ExceptionCheck()) { env->ExceptionDescribe(); return nullptr; } - shapes.emplace_back(mbgl::AnnotationSegments {{ segment.first }}, segment.second); + shapes.emplace_back(mbgl::AnnotationSegments { segment.first }, segment.second); env->DeleteLocalRef(polygon); } + env->DeleteLocalRef(jarray); + std::vector<uint32_t> shapeAnnotationIDs = nativeMapView->getMap().addShapeAnnotations(shapes); return std_vector_uint_to_jobject(env, shapeAnnotationIDs); } @@ -760,200 +883,60 @@ void JNICALL nativeRemoveAnnotation(JNIEnv *env, jobject obj, jlong nativeMapVie mbgl::Log::Debug(mbgl::Event::JNI, "nativeRemoveAnnotation"); assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().removeAnnotation((uint32_t)annotationId); + nativeMapView->getMap().removeAnnotation(static_cast<uint32_t>(annotationId)); } -void JNICALL nativeRemoveAnnotations(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jlongArray array) { +void JNICALL nativeRemoveAnnotations(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jlongArray jarray) { mbgl::Log::Debug(mbgl::Event::JNI, "nativeRemoveAnnotations"); assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); std::vector<uint32_t> ids; - if (env->ExceptionCheck() || (array == nullptr)) { + if (env->ExceptionCheck() || (jarray == nullptr)) { env->ExceptionDescribe(); return; } - jsize len = env->GetArrayLength(array); + jsize len = env->GetArrayLength(jarray); if (len < 0) { env->ExceptionDescribe(); return; } ids.reserve(len); - jlong* jids = env->GetLongArrayElements(array, nullptr); + jlong* jids = env->GetLongArrayElements(jarray, nullptr); for (jsize i = 0; i < len; i++) { if(jids[i] == -1L) continue; - ids.push_back((uint32_t) jids[i]); + ids.push_back(static_cast<uint32_t>(jids[i])); } - env->ReleaseLongArrayElements(array, jids, JNI_ABORT); + env->ReleaseLongArrayElements(jarray, jids, JNI_ABORT); nativeMapView->getMap().removeAnnotations(ids); } -jobject JNICALL nativeGetLatLng(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetLatLng"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - mbgl::LatLng latLng = nativeMapView->getMap().getLatLng(); - - jobject ret = env->NewObject(latLngClass, latLngConstructorId, latLng.latitude, latLng.longitude); - if (ret == nullptr) { - env->ExceptionDescribe(); - return nullptr; - } - - return ret; -} - -void JNICALL nativeResetPosition(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeResetPosition"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().resetPosition(); -} - -void JNICALL nativeScaleBy(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble ds, jdouble cx, - jdouble cy, jlong duration) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeScaleBy"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().scaleBy(ds, cx, cy, std::chrono::milliseconds(duration)); -} - -void JNICALL nativeSetScale(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble scale, - jdouble cx, jdouble cy, jlong duration) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetScale"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().setScale(scale, cx, cy, std::chrono::milliseconds(duration)); -} - -jdouble JNICALL nativeGetScale(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetScale"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - return nativeMapView->getMap().getScale(); -} - -void JNICALL nativeSetZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble zoom, jlong duration) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetZoom"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().setZoom(zoom, std::chrono::milliseconds(duration)); -} - -jdouble JNICALL nativeGetZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetZoom"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - return nativeMapView->getMap().getZoom(); -} - -void JNICALL nativeSetLatLngZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, - jobject latLngZoom, jlong duration) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetLatLngZoom"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - - jdouble latitude = env->GetDoubleField(latLngZoom, latLngZoomLatitudeId); - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - return; - } - - jdouble longitude = env->GetDoubleField(latLngZoom, latLngZoomLongitudeId); - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - return; - } - - jdouble zoom = env->GetDoubleField(latLngZoom, latLngZoomZoomId); - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - return; - } - - nativeMapView->getMap().setLatLngZoom(mbgl::LatLng(latitude, longitude), zoom, std::chrono::milliseconds(duration)); -} - -jobject JNICALL nativeGetLatLngZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetLatLngZoom"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - mbgl::LatLng latLng = nativeMapView->getMap().getLatLng(); - jdouble zoom = nativeMapView->getMap().getZoom(); - - jobject ret = env->NewObject(latLngZoomClass, latLngZoomConstructorId, latLng.longitude, latLng.latitude, zoom); - if (ret == nullptr) { - env->ExceptionDescribe(); - return nullptr; - } - - return ret; -} - -void JNICALL nativeResetZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeResetZoom"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().resetZoom(); -} - -jdouble JNICALL nativeGetMinZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetMinZoom"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - return nativeMapView->getMap().getMinZoom(); -} - -jdouble JNICALL nativeGetMaxZoom(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetMaxZoom"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - return nativeMapView->getMap().getMaxZoom(); -} - -void JNICALL nativeRotateBy(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble sx, - jdouble sy, jdouble ex, jdouble ey, jlong duration) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeRotateBy"); +void JNICALL nativeSetSprite(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, + jstring symbol, jint width, jint height, jfloat scale, jbyteArray jpixels) { + mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetSprite"); assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().rotateBy(sx, sy, ex, ey, std::chrono::milliseconds(duration)); -} -void JNICALL nativeSetBearing(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble degrees, - jlong duration) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetBearing"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().setBearing(degrees, std::chrono::milliseconds(duration)); -} + const std::string symbolName = std_string_from_jstring(env, symbol); -void JNICALL nativeSetBearing(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jdouble degrees, - jdouble cx, jdouble cy) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetBearing"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().setBearing(degrees, cx, cy); -} + jbyte* pixelData = env->GetByteArrayElements(jpixels, nullptr); + std::string pixels(reinterpret_cast<char*>(pixelData), width * height * 4); + env->ReleaseByteArrayElements(jpixels, pixelData, JNI_ABORT); -jdouble JNICALL nativeGetBearing(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetBearing"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - return nativeMapView->getMap().getBearing(); -} + auto spriteImage = std::make_shared<mbgl::SpriteImage>( + uint16_t(width), + uint16_t(height), + float(scale), + std::move(pixels)); -void JNICALL nativeResetNorth(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { - mbgl::Log::Debug(mbgl::Event::JNI, "nativeResetNorth"); - assert(nativeMapViewPtr != 0); - NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); - nativeMapView->getMap().resetNorth(); + nativeMapView->getMap().setSprite(symbolName, spriteImage); } void JNICALL nativeOnLowMemory(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) { @@ -1179,6 +1162,36 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } + latLngZoomClass = env->FindClass("com/mapbox/mapboxgl/geometry/LatLngZoom"); + if (latLngZoomClass == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + + latLngZoomConstructorId = env->GetMethodID(latLngZoomClass, "<init>", "(DDD)V"); + if (latLngZoomConstructorId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + + latLngZoomLatitudeId = env->GetFieldID(latLngZoomClass, "latitude", "D"); + if (latLngZoomLatitudeId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + + latLngZoomLongitudeId = env->GetFieldID(latLngZoomClass, "longitude", "D"); + if (latLngZoomLongitudeId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + + latLngZoomZoomId = env->GetFieldID(latLngZoomClass, "zoom", "D"); + if (latLngZoomZoomId == nullptr) { + env->ExceptionDescribe(); + return JNI_ERR; + } + markerClass = env->FindClass("com/mapbox/mapboxgl/annotations/Marker"); if (markerClass == nullptr) { env->ExceptionDescribe(); @@ -1299,36 +1312,6 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } - latLngZoomClass = env->FindClass("com/mapbox/mapboxgl/geometry/LatLngZoom"); - if (latLngZoomClass == nullptr) { - env->ExceptionDescribe(); - return JNI_ERR; - } - - latLngZoomConstructorId = env->GetMethodID(latLngZoomClass, "<init>", "(DDD)V"); - if (latLngZoomConstructorId == nullptr) { - env->ExceptionDescribe(); - return JNI_ERR; - } - - latLngZoomLatitudeId = env->GetFieldID(latLngZoomClass, "latitude", "D"); - if (latLngZoomLatitudeId == nullptr) { - env->ExceptionDescribe(); - return JNI_ERR; - } - - latLngZoomLongitudeId = env->GetFieldID(latLngZoomClass, "longitude", "D"); - if (latLngZoomLongitudeId == nullptr) { - env->ExceptionDescribe(); - return JNI_ERR; - } - - latLngZoomZoomId = env->GetFieldID(latLngZoomClass, "zoom", "D"); - if (latLngZoomZoomId == nullptr) { - env->ExceptionDescribe(); - return JNI_ERR; - } - jclass nativeMapViewClass = env->FindClass("com/mapbox/mapboxgl/views/NativeMapView"); if (nativeMapViewClass == nullptr) { env->ExceptionDescribe(); @@ -1492,17 +1475,6 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { {"nativeMoveBy", "(JDDJ)V", reinterpret_cast<void *>(&nativeMoveBy)}, {"nativeSetLatLng", "(JLcom/mapbox/mapboxgl/geometry/LatLng;J)V", reinterpret_cast<void *>(&nativeSetLatLng)}, - {"nativeSetSprite", "(JLjava/lang/String;IIF[B)V", reinterpret_cast<void *>(&nativeSetSprite)}, - {"nativeAddMarker", "(JLcom/mapbox/mapboxgl/annotations/Marker;)J", - 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)}, - {"nativeAddPolygons", "(JLjava/util/List;)[J", - reinterpret_cast<void *>(&nativeAddPolygons)}, - {"nativeRemoveAnnotation", "(JJ)V", reinterpret_cast<void *>(&nativeRemoveAnnotation)}, - {"nativeRemoveAnnotations", "(J[J)V", reinterpret_cast<void *>(&nativeRemoveAnnotations)}, {"nativeGetLatLng", "(J)Lcom/mapbox/mapboxgl/geometry/LatLng;", reinterpret_cast<void *>(&nativeGetLatLng)}, {"nativeResetPosition", "(J)V", reinterpret_cast<void *>(&nativeResetPosition)}, @@ -1529,6 +1501,17 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { &nativeSetBearing))}, {"nativeGetBearing", "(J)D", reinterpret_cast<void *>(&nativeGetBearing)}, {"nativeResetNorth", "(J)V", reinterpret_cast<void *>(&nativeResetNorth)}, + {"nativeAddMarker", "(JLcom/mapbox/mapboxgl/annotations/Marker;)J", + 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)}, + {"nativeAddPolygons", "(JLjava/util/List;)[J", + reinterpret_cast<void *>(&nativeAddPolygons)}, + {"nativeRemoveAnnotation", "(JJ)V", reinterpret_cast<void *>(&nativeRemoveAnnotation)}, + {"nativeRemoveAnnotations", "(J[J)V", reinterpret_cast<void *>(&nativeRemoveAnnotations)}, + {"nativeSetSprite", "(JLjava/lang/String;IIF[B)V", reinterpret_cast<void *>(&nativeSetSprite)}, {"nativeOnLowMemory", "(J)V", reinterpret_cast<void *>(&nativeOnLowMemory)}, {"nativeSetDebug", "(JZ)V", reinterpret_cast<void *>(&nativeSetDebug)}, {"nativeToggleDebug", "(J)V", reinterpret_cast<void *>(&nativeToggleDebug)}, @@ -1558,28 +1541,37 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } + latLngZoomClass = reinterpret_cast<jclass>(env->NewGlobalRef(latLngZoomClass)); + if (latLngZoomClass == nullptr) { + env->ExceptionDescribe(); + env->DeleteGlobalRef(latLngClass); + return JNI_ERR; + } + markerClass = reinterpret_cast<jclass>(env->NewGlobalRef(markerClass)); if (markerClass == nullptr) { env->ExceptionDescribe(); + env->DeleteGlobalRef(latLngClass); + env->DeleteGlobalRef(latLngZoomClass); return JNI_ERR; } polylineClass = reinterpret_cast<jclass>(env->NewGlobalRef(polylineClass)); if (polylineClass == nullptr) { env->ExceptionDescribe(); + env->DeleteGlobalRef(latLngClass); + env->DeleteGlobalRef(latLngZoomClass); + env->DeleteGlobalRef(markerClass); 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(); env->DeleteGlobalRef(latLngClass); + env->DeleteGlobalRef(latLngZoomClass); + env->DeleteGlobalRef(markerClass); + env->DeleteGlobalRef(polylineClass); return JNI_ERR; } @@ -1588,6 +1580,9 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { env->ExceptionDescribe(); env->DeleteGlobalRef(latLngClass); env->DeleteGlobalRef(latLngZoomClass); + env->DeleteGlobalRef(markerClass); + env->DeleteGlobalRef(polylineClass); + env->DeleteGlobalRef(polygonClass); return JNI_ERR; } @@ -1597,6 +1592,9 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { env->ExceptionDescribe(); env->DeleteGlobalRef(latLngClass); env->DeleteGlobalRef(latLngZoomClass); + env->DeleteGlobalRef(markerClass); + env->DeleteGlobalRef(polylineClass); + env->DeleteGlobalRef(polygonClass); env->DeleteGlobalRef(runtimeExceptionClass); return JNI_ERR; } @@ -1606,6 +1604,9 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { env->ExceptionDescribe(); env->DeleteGlobalRef(latLngClass); env->DeleteGlobalRef(latLngZoomClass); + env->DeleteGlobalRef(markerClass); + env->DeleteGlobalRef(polylineClass); + env->DeleteGlobalRef(polygonClass); env->DeleteGlobalRef(runtimeExceptionClass); env->DeleteGlobalRef(nullPointerExceptionClass); return JNI_ERR; @@ -1616,18 +1617,23 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { env->ExceptionDescribe(); env->DeleteGlobalRef(latLngClass); env->DeleteGlobalRef(latLngZoomClass); + env->DeleteGlobalRef(markerClass); + env->DeleteGlobalRef(polylineClass); + env->DeleteGlobalRef(polygonClass); env->DeleteGlobalRef(runtimeExceptionClass); env->DeleteGlobalRef(nullPointerExceptionClass); env->DeleteGlobalRef(arrayListClass); return JNI_ERR; } - pointFClass = reinterpret_cast<jclass>(env->NewGlobalRef(pointFClass)); if (pointFClass == nullptr) { env->ExceptionDescribe(); env->DeleteGlobalRef(latLngClass); + env->DeleteGlobalRef(markerClass); env->DeleteGlobalRef(latLngZoomClass); + env->DeleteGlobalRef(polylineClass); + env->DeleteGlobalRef(polygonClass); env->DeleteGlobalRef(runtimeExceptionClass); env->DeleteGlobalRef(nullPointerExceptionClass); env->DeleteGlobalRef(arrayListClass); @@ -1635,7 +1641,6 @@ extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) { return JNI_ERR; } - char release[PROP_VALUE_MAX] = ""; __system_property_get("ro.build.version.release", release); androidRelease = std::string(release); @@ -1659,6 +1664,13 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) { latLngLongitudeId = nullptr; latLngLatitudeId = nullptr; + env->DeleteGlobalRef(latLngZoomClass); + latLngZoomClass = nullptr; + latLngZoomConstructorId = nullptr; + latLngZoomLongitudeId = nullptr; + latLngZoomLatitudeId = nullptr; + latLngZoomZoomId = nullptr; + env->DeleteGlobalRef(markerClass); markerClass = nullptr; markerConstructorId = nullptr; @@ -1685,14 +1697,6 @@ extern "C" JNIEXPORT void JNICALL JNI_OnUnload(JavaVM *vm, void *reserved) { polygonPointsId = nullptr; polygonHolesId = nullptr; - - env->DeleteGlobalRef(latLngZoomClass); - latLngZoomClass = nullptr; - latLngZoomConstructorId = nullptr; - latLngZoomLongitudeId = nullptr; - latLngZoomLatitudeId = nullptr; - latLngZoomZoomId = nullptr; - onInvalidateId = nullptr; onMapChangedId = nullptr; onFpsChangedId = nullptr; diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/annotations/Annotation.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/annotations/Annotation.java index 6feb733f08..4f172c6e2b 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/annotations/Annotation.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/annotations/Annotation.java @@ -10,20 +10,19 @@ public abstract class Annotation { * * Internal C++ id is stored as unsigned int. */ - protected Long id; // null unless added to a MapView + protected long id = -1; // -1 unless added to a MapView private MapView mapView; - float alpha = 1; + float alpha = 1.0f; boolean visible = true; - public Annotation() {} public float getAlpha() { return alpha; } - public Long getId() { + public long getId() { return id; } @@ -40,7 +39,7 @@ public abstract class Annotation { this.alpha = alpha; } - public void setId(Long id) { + public void setId(long id) { this.id = id; } diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java index bc4b823c0e..473f33a1ad 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java @@ -241,7 +241,7 @@ public class MapView extends SurfaceView { public Marker addMarker(MarkerOptions markerOptions) { Marker marker = markerOptions.getMarker(); - Long id = mNativeMapView.addMarker(marker); + long id = mNativeMapView.addMarker(marker); marker.setId(id); // the annotation needs to know its id marker.setMapView(this); // the annotation needs to know which map view it is in annotations.add(marker); @@ -250,7 +250,7 @@ public class MapView extends SurfaceView { public Polyline addPolyline(PolylineOptions polylineOptions) { Polyline polyline = polylineOptions.getPolyline(); - Long id = mNativeMapView.addPolyline(polyline); + long id = mNativeMapView.addPolyline(polyline); polyline.setId(id); polyline.setMapView(this); annotations.add(polyline); @@ -259,7 +259,7 @@ public class MapView extends SurfaceView { public Polygon addPolygon(PolygonOptions polygonOptions) { Polygon polygon = polygonOptions.getPolygon(); - Long id = mNativeMapView.addPolygon(polygon); + long id = mNativeMapView.addPolygon(polygon); polygon.setId(id); polygon.setMapView(this); annotations.add(polygon); @@ -274,7 +274,7 @@ public class MapView extends SurfaceView { long[] ids = mNativeMapView.addPolygons(polygons); - for(int i=0; i<polygons.size(); i++) { + for(int i=0; i < polygons.size(); i++) { polygons.get(i).setId(ids[i]); polygons.get(i).setMapView(this); annotations.add(polygons.get(i)); @@ -294,13 +294,9 @@ public class MapView extends SurfaceView { public void removeAnnotations() { long[] ids = new long[annotations.size()]; - for(int i=0; i<annotations.size(); i++) { - Long id = annotations.get(i).getId(); - if(id == null) { - ids[i] = -1; - } else { - ids[i] = id; - } + for(int i = 0; i < annotations.size(); i++) { + long id = annotations.get(i).getId(); + ids[i] = id; } mNativeMapView.removeAnnotations(ids); } diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java index 3bf123f88e..cc65de52f4 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/NativeMapView.java @@ -224,37 +224,6 @@ class NativeMapView { nativeSetLatLng(mNativeMapViewPtr, latLng, duration); } - public void setSprite(String symbol, int width, int height, float scale, byte[] pixels) { - nativeSetSprite(mNativeMapViewPtr, symbol, width, height, scale, pixels); - } - - public long addMarker(Marker marker) { - return nativeAddMarker(mNativeMapViewPtr, marker); - } - - public long addPolyline(Polyline polyline) { - // NH TODO Throw exception if returns -1 - return nativeAddPolyline(mNativeMapViewPtr, polyline); - } - - public long addPolygon(Polygon polygon) { - // NH TODO Throw exception if returns -1 - return nativeAddPolygon(mNativeMapViewPtr, polygon); - } - - public long[] addPolygons(List<Polygon> polygon) { - // NH TODO Throw exception if returns -1 - return nativeAddPolygons(mNativeMapViewPtr, polygon); - } - - public void removeAnnotation(long id) { - nativeRemoveAnnotation(mNativeMapViewPtr, id); - } - - public void removeAnnotations(long[] ids) { - nativeRemoveAnnotations(mNativeMapViewPtr, ids); - } - public LatLng getLatLng() { return nativeGetLatLng(mNativeMapViewPtr); } @@ -356,6 +325,34 @@ class NativeMapView { nativeResetNorth(mNativeMapViewPtr); } + public long addMarker(Marker marker) { + return nativeAddMarker(mNativeMapViewPtr, marker); + } + + public long addPolyline(Polyline polyline) { + return nativeAddPolyline(mNativeMapViewPtr, polyline); + } + + public long addPolygon(Polygon polygon) { + return nativeAddPolygon(mNativeMapViewPtr, polygon); + } + + public long[] addPolygons(List<Polygon> polygon) { + return nativeAddPolygons(mNativeMapViewPtr, polygon); + } + + public void removeAnnotation(long id) { + nativeRemoveAnnotation(mNativeMapViewPtr, id); + } + + public void removeAnnotations(long[] ids) { + nativeRemoveAnnotations(mNativeMapViewPtr, ids); + } + + public void setSprite(String symbol, int width, int height, float scale, byte[] pixels) { + nativeSetSprite(mNativeMapViewPtr, symbol, width, height, scale, pixels); + } + public void onLowMemory() { nativeOnLowMemory(mNativeMapViewPtr); } @@ -392,7 +389,7 @@ class NativeMapView { nativeSetReachability(mNativeMapViewPtr, status); } - //public void getWorldBoundsMeters(); + //public void (); //public void getWorldBoundsLatLng(); @@ -507,21 +504,6 @@ class NativeMapView { private native void nativeSetLatLng(long nativeMapViewPtr, LatLng latLng, long duration); - private native void nativeSetSprite(long nativeMapViewPtr, String symbol, - int width, int height, float scale, byte[] pixels); - - private native long nativeAddMarker(long nativeMapViewPtr, Marker marker); - - private native long nativeAddPolyline(long nativeMapViewPtr, Polyline polyline); - - private native long nativeAddPolygon(long mNativeMapViewPtr, Polygon polygon); - - private native long[] nativeAddPolygons(long mNativeMapViewPtr, List<Polygon> polygon); - - private native void nativeRemoveAnnotation(long nativeMapViewPtr, long id); - - private native void nativeRemoveAnnotations(long nativeMapViewPtr, long[] id); - private native LatLng nativeGetLatLng(long nativeMapViewPtr); private native void nativeResetPosition(long nativeMapViewPtr); @@ -563,6 +545,21 @@ class NativeMapView { private native void nativeResetNorth(long nativeMapViewPtr); + private native long nativeAddMarker(long nativeMapViewPtr, Marker marker); + + private native long nativeAddPolyline(long nativeMapViewPtr, Polyline polyline); + + private native long nativeAddPolygon(long mNativeMapViewPtr, Polygon polygon); + + private native long[] nativeAddPolygons(long mNativeMapViewPtr, List<Polygon> polygon); + + private native void nativeRemoveAnnotation(long nativeMapViewPtr, long id); + + private native void nativeRemoveAnnotations(long nativeMapViewPtr, long[] id); + + private native void nativeSetSprite(long nativeMapViewPtr, String symbol, + int width, int height, float scale, byte[] pixels); + private native void nativeOnLowMemory(long nativeMapViewPtr); private native void nativeSetDebug(long nativeMapViewPtr, boolean debug); |