diff options
6 files changed, 77 insertions, 5 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 14eacb9a71..399e262c0f 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 @@ -155,6 +155,17 @@ public class MapboxMap { getMapView().getNativeMapView().removeLayer(layerId); } + /** + * Removes the layer. The reference is re-usable after this and can be re-added + * + * @param layer the layer to remove + * @throws NoSuchLayerException + */ + @UiThread + public void removeLayer(@NonNull Layer layer) throws NoSuchLayerException { + getMapView().getNativeMapView().removeLayer(layer); + } + @Nullable @UiThread public Source getSource(@NonNull String sourceId) { 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 dba11e167f..3e5ba99c70 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 @@ -491,7 +491,11 @@ final class NativeMapView { } public void removeLayer(@NonNull String layerId) throws NoSuchLayerException { - nativeRemoveLayer(nativeMapViewPtr, layerId); + nativeRemoveLayerById(nativeMapViewPtr, layerId); + } + + public void removeLayer(@NonNull Layer layer) throws NoSuchLayerException { + nativeRemoveLayer(nativeMapViewPtr, layer.getNativePtr()); } public Source getSource(@NonNull String sourceId) { @@ -743,7 +747,9 @@ final class NativeMapView { private native void nativeAddLayer(long nativeMapViewPtr, long layerPtr, String before); - private native void nativeRemoveLayer(long nativeMapViewPtr, String layerId) throws NoSuchLayerException; + private native void nativeRemoveLayerById(long nativeMapViewPtr, String layerId) throws NoSuchLayerException; + + private native void nativeRemoveLayer(long nativeMapViewPtr, long layerId) throws NoSuchLayerException; private native Source nativeGetSource(long nativeMapViewPtr, String sourceId); 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 d1318e713e..1ed4fdf110 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 @@ -1,5 +1,6 @@ package com.mapbox.mapboxsdk.testapp.style; +import android.graphics.Color; import android.support.test.espresso.Espresso; import android.support.test.espresso.UiController; import android.support.test.espresso.ViewAction; @@ -19,6 +20,8 @@ import com.mapbox.mapboxsdk.testapp.activity.style.RuntimeStyleTestActivity; import com.mapbox.mapboxsdk.testapp.utils.OnMapReadyIdlingResource; import com.mapbox.mapboxsdk.testapp.utils.ViewUtils; +import junit.framework.Assert; + import org.hamcrest.Matcher; import org.junit.After; import org.junit.Before; @@ -30,6 +33,7 @@ import org.junit.runner.RunWith; import static android.support.test.espresso.Espresso.onView; import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; import static android.support.test.espresso.matcher.ViewMatchers.withId; +import static junit.framework.Assert.fail; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -94,7 +98,7 @@ public class RuntimeStyleTests { try { mapboxMap.removeLayer("building"); } catch (NoSuchLayerException e) { - assertFalse(true); + fail("Definitively exists: " + e.getMessage()); } assertNull(mapboxMap.getLayer("building")); @@ -106,6 +110,22 @@ public class RuntimeStyleTests { //Assure the reference still works layer.setProperties(PropertyFactory.visibility(Property.VISIBLE)); + + //Remove, preserving the reference + try { + mapboxMap.removeLayer(layer); + } catch (NoSuchLayerException e) { + fail("Definitively exists: " + e.getMessage()); + } + + //Property setters should still work + layer.setProperties(PropertyFactory.fillColor(Color.RED)); + + //Re-add the reference... + mapboxMap.addLayer(layer); + + //Ensure it's there + Assert.assertNotNull(mapboxMap.getLayer(layer.getId())); } } diff --git a/platform/android/src/jni.cpp b/platform/android/src/jni.cpp index 1d4849dcfb..6c346ec931 100755 --- a/platform/android/src/jni.cpp +++ b/platform/android/src/jni.cpp @@ -1109,7 +1109,10 @@ void nativeAddLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlon } } -void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) { +/** + * Remove by layer id. Ownership is not transferred back + */ +void nativeRemoveLayerById(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jstring* id) { assert(nativeMapViewPtr != 0); NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); try { @@ -1119,6 +1122,22 @@ void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, j } } +/** + * Remove with wrapper object id. Ownership is transferred back to the wrapper + */ +void nativeRemoveLayer(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jlong layerPtr) { + assert(nativeMapViewPtr != 0); + NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr); + mbgl::android::Layer *layer = reinterpret_cast<mbgl::android::Layer *>(layerPtr); + try { + std::unique_ptr<mbgl::style::Layer> coreLayer = nativeMapView->getMap().removeLayer(layer->get().getID()); + layer->setLayer(std::move(coreLayer)); + } catch (const std::runtime_error& error) { + jni::ThrowNew(*env, jni::FindClass(*env, "com/mapbox/mapboxsdk/style/layers/NoSuchLayerException"), error.what()); + } +} + + jni::jobject* nativeGetSource(JNIEnv *env, jni::jobject* obj, jni::jlong nativeMapViewPtr, jni::jstring* sourceId) { assert(env); assert(nativeMapViewPtr != 0); @@ -1838,7 +1857,8 @@ extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM *vm, void *reserved) { MAKE_NATIVE_METHOD(nativeFlyTo, "(JDDDJDD)V"), MAKE_NATIVE_METHOD(nativeGetLayer, "(JLjava/lang/String;)Lcom/mapbox/mapboxsdk/style/layers/Layer;"), MAKE_NATIVE_METHOD(nativeAddLayer, "(JJLjava/lang/String;)V"), - MAKE_NATIVE_METHOD(nativeRemoveLayer, "(JLjava/lang/String;)V"), + MAKE_NATIVE_METHOD(nativeRemoveLayerById, "(JLjava/lang/String;)V"), + MAKE_NATIVE_METHOD(nativeRemoveLayer, "(JJ)V"), MAKE_NATIVE_METHOD(nativeGetSource, "(JLjava/lang/String;)Lcom/mapbox/mapboxsdk/style/sources/Source;"), MAKE_NATIVE_METHOD(nativeAddSource, "(JJ)V"), MAKE_NATIVE_METHOD(nativeRemoveSource, "(JLjava/lang/String;)V"), diff --git a/platform/android/src/style/layers/layer.cpp b/platform/android/src/style/layers/layer.cpp index aa6df40470..00a52147b8 100644 --- a/platform/android/src/style/layers/layer.cpp +++ b/platform/android/src/style/layers/layer.cpp @@ -45,6 +45,10 @@ namespace android { this->map = &_map; } + void Layer::setLayer(std::unique_ptr<mbgl::style::Layer> sourceLayer) { + this->ownedLayer = std::move(sourceLayer); + } + std::unique_ptr<mbgl::style::Layer> Layer::releaseCoreLayer() { assert(ownedLayer != nullptr); return std::move(ownedLayer); @@ -54,6 +58,10 @@ namespace android { return jni::Make<jni::String>(env, layer.getID()); } + style::Layer& Layer::get() { + return layer; + } + void Layer::setLayoutProperty(jni::JNIEnv& env, jni::String jname, jni::Object<> jvalue) { Value value(env, jvalue); diff --git a/platform/android/src/style/layers/layer.hpp b/platform/android/src/style/layers/layer.hpp index 01eac2280b..f3cd073552 100644 --- a/platform/android/src/style/layers/layer.hpp +++ b/platform/android/src/style/layers/layer.hpp @@ -34,10 +34,17 @@ public: virtual jni::jobject* createJavaPeer(jni::JNIEnv&) = 0; + /** + * Set core layer (ie return ownership after remove) + */ + void setLayer(std::unique_ptr<mbgl::style::Layer>); + void addToMap(mbgl::Map&, mbgl::optional<std::string>); jni::String getId(jni::JNIEnv&); + style::Layer& get(); + void setLayoutProperty(jni::JNIEnv&, jni::String, jni::Object<> value); void setPaintProperty(jni::JNIEnv&, jni::String, jni::Object<> value); |