summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-02-24 18:19:04 +0200
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-02-28 09:33:04 -0800
commita9d2929819a5b4268a83e8ccd90eeb0b1f1e2f72 (patch)
treecc0a0376967e7e7d4f8131c428c86a0d7392f41d
parentdc21396d71ea28b736ee6fb3dcd476fed80fbd5d (diff)
downloadqtlocation-mapboxgl-a9d2929819a5b4268a83e8ccd90eeb0b1f1e2f72.tar.gz
[android] remove layer at index
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java17
-rwxr-xr-xplatform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java11
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java25
-rwxr-xr-xplatform/android/src/native_map_view.cpp22
-rwxr-xr-xplatform/android/src/native_map_view.hpp2
5 files changed, 75 insertions, 2 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 9b40685408..3623872a4f 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
@@ -8,6 +8,7 @@ import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.FloatRange;
+import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
@@ -263,7 +264,7 @@ public final class MapboxMap {
/**
* Adds the layer to the map. The layer must be newly created and not added to the map before
*
- * @param layer the layer to add
+ * @param layer the layer to add
* @param below the layer id to add this layer before
*/
@UiThread
@@ -274,7 +275,7 @@ public final class MapboxMap {
/**
* Adds the layer to the map. The layer must be newly created and not added to the map before
*
- * @param layer the layer to add
+ * @param layer the layer to add
* @param above the layer id to add this layer above
*/
@UiThread
@@ -307,6 +308,18 @@ public final class MapboxMap {
}
/**
+ * Removes the layer. Any other references to the layer become invalid and should not be used anymore
+ *
+ * @param index the layer index
+ * @return the removed layer or null if not found
+ */
+ @UiThread
+ @Nullable
+ public Layer removeLayerAt(@IntRange(from = 0) int index) {
+ return nativeMapView.removeLayerAt(index);
+ }
+
+ /**
* Retrieve all the sources in the style
*
* @return all the sources in the current style
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 a816808f7b..67a0c45134 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
@@ -6,6 +6,7 @@ import android.graphics.Bitmap;
import android.graphics.PointF;
import android.graphics.RectF;
import android.os.Build;
+import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
@@ -799,6 +800,14 @@ final class NativeMapView {
return layer;
}
+ @Nullable
+ public Layer removeLayerAt(@IntRange(from = 0) int index) {
+ if (isDestroyedOn("removeLayerAt")) {
+ return null;
+ }
+ return nativeRemoveLayerAt(index);
+ }
+
public List<Source> getSources() {
if (isDestroyedOn("getSources")) {
return null;
@@ -1099,6 +1108,8 @@ final class NativeMapView {
private native void nativeRemoveLayer(long layerId);
+ private native Layer nativeRemoveLayerAt(int index);
+
private native Source[] nativeGetSources();
private native Source nativeGetSource(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 2014208fcf..76a9de730b 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
@@ -35,6 +35,8 @@ import org.junit.runner.RunWith;
import java.util.List;
+import timber.log.Timber;
+
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;
@@ -123,6 +125,29 @@ public class RuntimeStyleTests {
}
@Test
+ public void testRemoveLayerAt() {
+ 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();
+
+ // Remove by index
+ Layer firstLayer = mapboxMap.getLayers().get(0);
+ Layer removed = mapboxMap.removeLayerAt(0);
+ assertNotNull(removed);
+ assertNotNull(removed.getId());
+ assertEquals(firstLayer.getId(), removed.getId());
+
+ // Test remove by index bounds checks
+ Timber.i("Remove layer at index > size");
+ assertNull(mapboxMap.removeLayerAt(Integer.MAX_VALUE));
+ }
+ });
+ }
+
+ @Test
public void testListSources() {
ViewUtils.checkViewIsDisplayed(R.id.mapView);
onView(withId(R.id.mapView)).perform(new BaseViewAction() {
diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp
index 34e18754e9..986a5f6120 100755
--- a/platform/android/src/native_map_view.cpp
+++ b/platform/android/src/native_map_view.cpp
@@ -829,6 +829,27 @@ jni::Object<Layer> NativeMapView::removeLayerById(JNIEnv& env, jni::String id) {
}
/**
+ * Remove layer at index.
+ */
+jni::Object<Layer> NativeMapView::removeLayerAt(JNIEnv& env, jni::jint index) {
+ auto layers = map->getLayers();
+
+ // Check index
+ int numLayers = layers.size() - 1;
+ if (index > numLayers || index < 0) {
+ Log::Warning(Event::JNI, "Index out of range: %i", index);
+ return jni::Object<Layer>();
+ }
+
+ std::unique_ptr<mbgl::style::Layer> coreLayer = map->removeLayer(layers.at(index)->getID());
+ if (coreLayer) {
+ return jni::Object<Layer>(createJavaLayerPeer(env, *map, std::move(coreLayer)));
+ } else {
+ return jni::Object<Layer>();
+ }
+}
+
+/**
* Remove with wrapper object id. Ownership is transferred back to the wrapper
*/
void NativeMapView::removeLayer(JNIEnv&, jlong layerPtr) {
@@ -1419,6 +1440,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
METHOD(&NativeMapView::addLayer, "nativeAddLayer"),
METHOD(&NativeMapView::addLayerAbove, "nativeAddLayerAbove"),
METHOD(&NativeMapView::removeLayerById, "nativeRemoveLayerById"),
+ METHOD(&NativeMapView::removeLayerAt, "nativeRemoveLayerAt"),
METHOD(&NativeMapView::removeLayer, "nativeRemoveLayer"),
METHOD(&NativeMapView::getSources, "nativeGetSources"),
METHOD(&NativeMapView::getSource, "nativeGetSource"),
diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp
index 9f02f4a25c..4c70b48e41 100755
--- a/platform/android/src/native_map_view.hpp
+++ b/platform/android/src/native_map_view.hpp
@@ -211,6 +211,8 @@ public:
jni::Object<Layer> removeLayerById(JNIEnv&, jni::String);
+ jni::Object<Layer> removeLayerAt(JNIEnv&, jni::jint);
+
void removeLayer(JNIEnv&, jlong);
jni::Array<jni::Object<Source>> getSources(JNIEnv&);