[android] insert layer at index
This commit is contained in:
committed by
Ivo van Dongen
parent
a9d2929819
commit
99786e1e3f
@@ -283,6 +283,18 @@ public final class MapboxMap {
|
||||
nativeMapView.addLayerAbove(layer, above);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user