diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-02-27 20:12:42 +0200 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-02-28 09:33:04 -0800 |
commit | 99786e1e3ff1cb79f6befe93e02c968e5e6b6781 (patch) | |
tree | 8a28a4e1b468f211eb535d9a2853c96f165df19d | |
parent | a9d2929819a5b4268a83e8ccd90eeb0b1f1e2f72 (diff) | |
download | qtlocation-mapboxgl-99786e1e3ff1cb79f6befe93e02c968e5e6b6781.tar.gz |
[android] insert layer at index
5 files changed, 82 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 3623872a4f..e3e33ec067 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -284,6 +284,18 @@ public final class MapboxMap { } /** + * Adds the layer to the map at the specified index. The layer must be newly + * created and not added to the map before + * + * @param layer the layer to add + * @param index the index to insert the layer at + */ + @UiThread + public void addLayerAt(@NonNull Layer layer, @IntRange(from = 0) int index) { + nativeMapView.addLayerAt(layer, index); + } + + /** * Removes the layer. Any references to the layer become invalid and should not be used anymore * * @param layerId the layer to remove diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java index 67a0c45134..914dbad32d 100755 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java @@ -783,6 +783,13 @@ final class NativeMapView { nativeAddLayerAbove(layer.getNativePtr(), above); } + public void addLayerAt(@NonNull Layer layer, @IntRange(from = 0) int index) { + if (isDestroyedOn("addLayerAt")) { + return; + } + nativeAddLayerAt(layer.getNativePtr(), index); + } + @Nullable public Layer removeLayer(@NonNull String layerId) { if (isDestroyedOn("removeLayer")) { @@ -1104,6 +1111,8 @@ final class NativeMapView { private native void nativeAddLayerAbove(long layerPtr, String above) throws CannotAddLayerException; + private native void nativeAddLayerAt(long layerPtr, int index) throws CannotAddLayerException; + private native Layer nativeRemoveLayerById(String layerId); private native void nativeRemoveLayer(long layerId); diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java index 76a9de730b..bf90949ffd 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java @@ -147,6 +147,41 @@ public class RuntimeStyleTests { }); } + public void testAddLayerAt() { + ViewUtils.checkViewIsDisplayed(R.id.mapView); + onView(withId(R.id.mapView)).perform(new BaseViewAction() { + @Override + public void perform(UiController uiController, View view) { + MapboxMap mapboxMap = rule.getActivity().getMapboxMap(); + + List<Layer> layers = mapboxMap.getLayers(); + Source source = mapboxMap.getSources().get(0); + + // Test inserting out of range + try { + mapboxMap.addLayerAt(new CircleLayer("invalid-id-layer-test", source.getId()), layers.size()); + fail("Should have thrown exception"); + } catch (CannotAddLayerException ex) { + // Yeah + assertNotNull(ex.getMessage()); + } + + // Insert at current last position + CircleLayer last = new CircleLayer("this is the last one", source.getId()); + mapboxMap.addLayerAt(last, layers.size() - 1); + layers = mapboxMap.getLayers(); + assertEquals(last.getId(), layers.get(layers.size() - 2).getId()); + + // Insert at start + CircleLayer second = new CircleLayer("this is the first one", source.getId()); + mapboxMap.addLayerAt(second, 0); + layers = mapboxMap.getLayers(); + assertEquals(second.getId(), layers.get(0).getId()); + } + }); + } + + @Test public void testListSources() { ViewUtils.checkViewIsDisplayed(R.id.mapView); diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp index 986a5f6120..996fa2d7f3 100755 --- a/platform/android/src/native_map_view.cpp +++ b/platform/android/src/native_map_view.cpp @@ -816,6 +816,29 @@ void NativeMapView::addLayerAbove(JNIEnv& env, jlong nativeLayerPtr, jni::String } } +void NativeMapView::addLayerAt(JNIEnv& env, jlong nativeLayerPtr, jni::jint index) { + assert(nativeLayerPtr != 0); + + Layer *layer = reinterpret_cast<Layer *>(nativeLayerPtr); + auto layers = map->getLayers(); + + // Check index + int numLayers = layers.size() - 1; + if (index > numLayers || index < 0) { + Log::Error(Event::JNI, "Index out of range: %i", index); + jni::ThrowNew(env, jni::FindClass(env, "com/mapbox/mapboxsdk/style/layers/CannotAddLayerException"), + std::string("Invalid index").c_str()); + return; + } + + // Insert it below the current at that index + try { + layer->addToMap(*map, layers.at(index)->getID()); + } catch (const std::runtime_error& error) { + jni::ThrowNew(env, jni::FindClass(env, "com/mapbox/mapboxsdk/style/layers/CannotAddLayerException"), error.what()); + } +} + /** * Remove by layer id. */ @@ -1439,6 +1462,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) { METHOD(&NativeMapView::getLayer, "nativeGetLayer"), METHOD(&NativeMapView::addLayer, "nativeAddLayer"), METHOD(&NativeMapView::addLayerAbove, "nativeAddLayerAbove"), + METHOD(&NativeMapView::addLayerAt, "nativeAddLayerAt"), METHOD(&NativeMapView::removeLayerById, "nativeRemoveLayerById"), METHOD(&NativeMapView::removeLayerAt, "nativeRemoveLayerAt"), METHOD(&NativeMapView::removeLayer, "nativeRemoveLayer"), diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp index 4c70b48e41..c38afd3e6c 100755 --- a/platform/android/src/native_map_view.hpp +++ b/platform/android/src/native_map_view.hpp @@ -209,6 +209,8 @@ public: void addLayerAbove(JNIEnv&, jlong, jni::String); + void addLayerAt(JNIEnv&, jni::jlong, jni::jint); + jni::Object<Layer> removeLayerById(JNIEnv&, jni::String); jni::Object<Layer> removeLayerAt(JNIEnv&, jni::jint); |