[android] insert layer above
This commit is contained in:
committed by
Ivo van Dongen
parent
3040d40772
commit
dc21396d71
@@ -257,18 +257,29 @@ public final class MapboxMap {
|
||||
*/
|
||||
@UiThread
|
||||
public void addLayer(@NonNull Layer layer) {
|
||||
addLayer(layer, null);
|
||||
nativeMapView.addLayer(layer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 before the layer id to add this layer before
|
||||
* @param below the layer id to add this layer before
|
||||
*/
|
||||
@UiThread
|
||||
public void addLayer(@NonNull Layer layer, String before) {
|
||||
nativeMapView.addLayer(layer, before);
|
||||
public void addLayerBelow(@NonNull Layer layer, @NonNull String below) {
|
||||
nativeMapView.addLayerBelow(layer, below);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 above the layer id to add this layer above
|
||||
*/
|
||||
@UiThread
|
||||
public void addLayerAbove(@NonNull Layer layer, @NonNull String above) {
|
||||
nativeMapView.addLayerAbove(layer, above);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -761,11 +761,25 @@ final class NativeMapView {
|
||||
return nativeGetLayer(layerId);
|
||||
}
|
||||
|
||||
public void addLayer(@NonNull Layer layer, @Nullable String before) {
|
||||
if (isDestroyedOn("")) {
|
||||
public void addLayer(@NonNull Layer layer) {
|
||||
if (isDestroyedOn("addLayer")) {
|
||||
return;
|
||||
}
|
||||
nativeAddLayer(layer.getNativePtr(), before);
|
||||
nativeAddLayer(layer.getNativePtr(), null);
|
||||
}
|
||||
|
||||
public void addLayerBelow(@NonNull Layer layer, @NonNull String below) {
|
||||
if (isDestroyedOn("addLayerBelow")) {
|
||||
return;
|
||||
}
|
||||
nativeAddLayer(layer.getNativePtr(), below);
|
||||
}
|
||||
|
||||
public void addLayerAbove(@NonNull Layer layer, @NonNull String above) {
|
||||
if (isDestroyedOn("addLayerAbove")) {
|
||||
return;
|
||||
}
|
||||
nativeAddLayerAbove(layer.getNativePtr(), above);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@@ -1079,6 +1093,8 @@ final class NativeMapView {
|
||||
|
||||
private native void nativeAddLayer(long layerPtr, String before) throws CannotAddLayerException;
|
||||
|
||||
private native void nativeAddLayerAbove(long layerPtr, String above) throws CannotAddLayerException;
|
||||
|
||||
private native Layer nativeRemoveLayerById(String layerId);
|
||||
|
||||
private native void nativeRemoveLayer(long layerId);
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.view.View;
|
||||
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException;
|
||||
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
|
||||
import com.mapbox.mapboxsdk.style.layers.FillLayer;
|
||||
import com.mapbox.mapboxsdk.style.layers.Layer;
|
||||
import com.mapbox.mapboxsdk.style.layers.Property;
|
||||
@@ -37,10 +38,11 @@ import java.util.List;
|
||||
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.assertTrue;
|
||||
import static junit.framework.Assert.fail;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* Basic smoke tests for Layer and Source
|
||||
@@ -85,6 +87,41 @@ public class RuntimeStyleTests {
|
||||
onView(withId(R.id.mapView)).perform(new AddRemoveLayerAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddLayerAbove() {
|
||||
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 with invalid above-id
|
||||
try {
|
||||
mapboxMap.addLayerAbove(new CircleLayer("invalid-id-layer-test", source.getId()), "no-such-layer-here-man");
|
||||
fail("Should have thrown exception");
|
||||
} catch (CannotAddLayerException ex) {
|
||||
// Yeah
|
||||
assertNotNull(ex.getMessage());
|
||||
}
|
||||
|
||||
// Insert as last
|
||||
CircleLayer last = new CircleLayer("this is the last one", source.getId());
|
||||
mapboxMap.addLayerAbove(last, layers.get(layers.size() - 1).getId());
|
||||
layers = mapboxMap.getLayers();
|
||||
assertEquals(last.getId(), layers.get(layers.size() - 1).getId());
|
||||
|
||||
// Insert
|
||||
CircleLayer second = new CircleLayer("this is the second one", source.getId());
|
||||
mapboxMap.addLayerAbove(second, layers.get(0).getId());
|
||||
layers = mapboxMap.getLayers();
|
||||
assertEquals(second.getId(), layers.get(1).getId());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListSources() {
|
||||
ViewUtils.checkViewIsDisplayed(R.id.mapView);
|
||||
@@ -97,7 +134,7 @@ public class RuntimeStyleTests {
|
||||
List<Source> sources = mapboxMap.getSources();
|
||||
assertNotNull(sources);
|
||||
assertTrue(sources.size() > 0);
|
||||
for (Source source: sources) {
|
||||
for (Source source : sources) {
|
||||
assertNotNull(source);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,7 +70,7 @@ public class CustomLayerActivity extends AppCompatActivity {
|
||||
ExampleCustomLayer.InitializeFunction,
|
||||
ExampleCustomLayer.RenderFunction,
|
||||
ExampleCustomLayer.DeinitializeFunction);
|
||||
mapboxMap.addLayer(customLayer, "building");
|
||||
mapboxMap.addLayerBelow(customLayer, "building");
|
||||
fab.setImageResource(R.drawable.ic_layers_clear);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ public class CircleLayerActivity extends AppCompatActivity {
|
||||
Timber.e("That's not an url... ", malformedUrlException);
|
||||
}
|
||||
LineLayer lineLayer = new LineLayer("route_layer", "bus_route");
|
||||
mapboxMap.addLayer(lineLayer, "stops_layer");
|
||||
mapboxMap.addLayerBelow(lineLayer, "stops_layer");
|
||||
|
||||
// move camera to start route
|
||||
mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(
|
||||
|
||||
@@ -77,7 +77,7 @@ public class CustomSpriteActivity extends AppCompatActivity {
|
||||
);
|
||||
|
||||
// lets add a circle below labels!
|
||||
mapboxMap.addLayer(layer, "waterway-label");
|
||||
mapboxMap.addLayerBelow(layer, "waterway-label");
|
||||
|
||||
fab.setImageResource(R.drawable.ic_directions_car_black);
|
||||
} else {
|
||||
|
||||
@@ -40,7 +40,6 @@ import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import timber.log.Timber;
|
||||
@@ -302,7 +301,7 @@ public class RuntimeStyleActivity extends AppCompatActivity {
|
||||
// Only show me parks (except westerpark with stroke-width == 3)
|
||||
layer.setFilter(all(eq("type", "park"), eq("stroke-width", 2)));
|
||||
|
||||
mapboxMap.addLayer(layer, "building");
|
||||
mapboxMap.addLayerBelow(layer, "building");
|
||||
// layer.setPaintProperty(fillColor(Color.RED)); // XXX But not after the object is attached
|
||||
|
||||
// Or get the object later and set it. It's all good.
|
||||
|
||||
@@ -779,6 +779,43 @@ void NativeMapView::addLayer(JNIEnv& env, jlong nativeLayerPtr, jni::String befo
|
||||
}
|
||||
}
|
||||
|
||||
void NativeMapView::addLayerAbove(JNIEnv& env, jlong nativeLayerPtr, jni::String above) {
|
||||
assert(nativeLayerPtr != 0);
|
||||
|
||||
Layer *layer = reinterpret_cast<Layer *>(nativeLayerPtr);
|
||||
|
||||
// Find the sibling
|
||||
auto layers = map->getLayers();
|
||||
auto siblingId = jni::Make<std::string>(env, above);
|
||||
|
||||
size_t index = 0;
|
||||
for (auto l : layers) {
|
||||
if (l->getID() == siblingId) {
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
|
||||
// Check if we found a sibling to place before
|
||||
mbgl::optional<std::string> before;
|
||||
if (index + 1 > layers.size()) {
|
||||
// Not found
|
||||
jni::ThrowNew(env, jni::FindClass(env, "com/mapbox/mapboxsdk/style/layers/CannotAddLayerException"),
|
||||
std::string("Could not find layer: ").append(siblingId).c_str());
|
||||
return;
|
||||
} else if (index + 1 < layers.size()) {
|
||||
// Place before the sibling
|
||||
before = { layers.at(index + 1)->getID() };
|
||||
}
|
||||
|
||||
// Add the layer
|
||||
try {
|
||||
layer->addToMap(*map, before);
|
||||
} catch (const std::runtime_error& error) {
|
||||
jni::ThrowNew(env, jni::FindClass(env, "com/mapbox/mapboxsdk/style/layers/CannotAddLayerException"), error.what());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove by layer id.
|
||||
*/
|
||||
@@ -1380,6 +1417,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
|
||||
METHOD(&NativeMapView::getLayers, "nativeGetLayers"),
|
||||
METHOD(&NativeMapView::getLayer, "nativeGetLayer"),
|
||||
METHOD(&NativeMapView::addLayer, "nativeAddLayer"),
|
||||
METHOD(&NativeMapView::addLayerAbove, "nativeAddLayerAbove"),
|
||||
METHOD(&NativeMapView::removeLayerById, "nativeRemoveLayerById"),
|
||||
METHOD(&NativeMapView::removeLayer, "nativeRemoveLayer"),
|
||||
METHOD(&NativeMapView::getSources, "nativeGetSources"),
|
||||
|
||||
@@ -207,6 +207,8 @@ public:
|
||||
|
||||
void addLayer(JNIEnv&, jlong, jni::String);
|
||||
|
||||
void addLayerAbove(JNIEnv&, jlong, jni::String);
|
||||
|
||||
jni::Object<Layer> removeLayerById(JNIEnv&, jni::String);
|
||||
|
||||
void removeLayer(JNIEnv&, jlong);
|
||||
|
||||
Reference in New Issue
Block a user