[android] layer list accessor + example
This commit is contained in:
committed by
Ivo van Dongen
parent
62e85c1224
commit
ec1dc2622b
@@ -209,6 +209,22 @@ public final class MapboxMap {
|
||||
nativeMapView.setTransitionDelay(delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all the layers in the style
|
||||
*
|
||||
* @return all the layers in the current style
|
||||
*/
|
||||
@UiThread
|
||||
public List<Layer> getLayers() {
|
||||
return nativeMapView.getLayers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the layer by id
|
||||
*
|
||||
* @param layerId the layer's id
|
||||
* @return the layer, if present in the style
|
||||
*/
|
||||
@Nullable
|
||||
@UiThread
|
||||
public Layer getLayer(@NonNull String layerId) {
|
||||
|
||||
@@ -747,6 +747,13 @@ final class NativeMapView {
|
||||
nativeSetTransitionDelay(delay);
|
||||
}
|
||||
|
||||
public List<Layer> getLayers() {
|
||||
if (isDestroyedOn("getLayers")) {
|
||||
return null;
|
||||
}
|
||||
return Arrays.asList(nativeGetLayers());
|
||||
}
|
||||
|
||||
public Layer getLayer(String layerId) {
|
||||
if (isDestroyedOn("getLayer")) {
|
||||
return null;
|
||||
@@ -1054,6 +1061,8 @@ final class NativeMapView {
|
||||
|
||||
private native void nativeSetTransitionDelay(long delay);
|
||||
|
||||
private native Layer[] nativeGetLayers();
|
||||
|
||||
private native Layer nativeGetLayer(String layerId);
|
||||
|
||||
private native void nativeAddLayer(long layerPtr, String before) throws CannotAddLayerException;
|
||||
|
||||
@@ -12,6 +12,7 @@ import android.view.View;
|
||||
import com.mapbox.mapboxsdk.maps.MapboxMap;
|
||||
import com.mapbox.mapboxsdk.style.layers.CannotAddLayerException;
|
||||
import com.mapbox.mapboxsdk.style.layers.FillLayer;
|
||||
import com.mapbox.mapboxsdk.style.layers.Layer;
|
||||
import com.mapbox.mapboxsdk.style.layers.Property;
|
||||
import com.mapbox.mapboxsdk.style.layers.PropertyFactory;
|
||||
import com.mapbox.mapboxsdk.style.sources.CannotAddSourceException;
|
||||
@@ -31,9 +32,12 @@ import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
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.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
@@ -55,6 +59,26 @@ public class RuntimeStyleTests {
|
||||
Espresso.registerIdlingResources(idlingResource);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListLayers() {
|
||||
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();
|
||||
assertNotNull(layers);
|
||||
assertTrue(layers.size() > 0);
|
||||
for (Layer layer : layers) {
|
||||
assertNotNull(layer);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetAddRemoveLayer() {
|
||||
ViewUtils.checkViewIsDisplayed(R.id.mapView);
|
||||
|
||||
@@ -40,6 +40,7 @@ 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;
|
||||
@@ -150,6 +151,9 @@ public class RuntimeStyleActivity extends AppCompatActivity {
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case R.id.action_list_layers:
|
||||
listLayers();
|
||||
return true;
|
||||
case R.id.action_water_color:
|
||||
setWaterColor();
|
||||
return true;
|
||||
@@ -197,6 +201,16 @@ public class RuntimeStyleActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
private void listLayers() {
|
||||
List<Layer> layers = mapboxMap.getLayers();
|
||||
StringBuilder builder = new StringBuilder("Layers:");
|
||||
for (Layer layer : layers) {
|
||||
builder.append("\n");
|
||||
builder.append(layer.getId());
|
||||
}
|
||||
Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
private void setLayerInvisible() {
|
||||
String[] roadLayers = new String[] {"water"};
|
||||
for (String roadLayer : roadLayers) {
|
||||
|
||||
@@ -2,6 +2,10 @@
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:mapbox="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item
|
||||
android:id="@+id/action_list_layers"
|
||||
android:title="List all layers in the style"
|
||||
mapbox:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_water_color"
|
||||
android:title="Color the water"
|
||||
|
||||
@@ -737,6 +737,24 @@ jni::Array<jni::Object<Feature>> NativeMapView::queryRenderedFeaturesForBox(JNIE
|
||||
return *convert<jni::Array<jni::Object<Feature>>, std::vector<mbgl::Feature>>(env, map->queryRenderedFeatures(box, layers));
|
||||
}
|
||||
|
||||
jni::Array<jni::Object<Layer>> NativeMapView::getLayers(JNIEnv& env) {
|
||||
|
||||
// Get the core layers
|
||||
std::vector<style::Layer*> layers = map->getLayers();
|
||||
|
||||
// Convert
|
||||
jni::Array<jni::Object<Layer>> jLayers = jni::Array<jni::Object<Layer>>::New(env, layers.size(), Layer::javaClass);
|
||||
int index = 0;
|
||||
for (auto layer : layers) {
|
||||
auto jLayer = jni::Object<Layer>(createJavaLayerPeer(env, *map, *layer));
|
||||
jLayers.Set(env, index, jLayer);
|
||||
jni::DeleteLocalRef(env, jLayer);
|
||||
index++;
|
||||
}
|
||||
|
||||
return jLayers;
|
||||
}
|
||||
|
||||
jni::Object<Layer> NativeMapView::getLayer(JNIEnv& env, jni::String layerId) {
|
||||
|
||||
// Find the layer
|
||||
@@ -1333,6 +1351,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
|
||||
METHOD(&NativeMapView::queryPointAnnotations, "nativeQueryPointAnnotations"),
|
||||
METHOD(&NativeMapView::queryRenderedFeaturesForPoint, "nativeQueryRenderedFeaturesForPoint"),
|
||||
METHOD(&NativeMapView::queryRenderedFeaturesForBox, "nativeQueryRenderedFeaturesForBox"),
|
||||
METHOD(&NativeMapView::getLayers, "nativeGetLayers"),
|
||||
METHOD(&NativeMapView::getLayer, "nativeGetLayer"),
|
||||
METHOD(&NativeMapView::addLayer, "nativeAddLayer"),
|
||||
METHOD(&NativeMapView::removeLayerById, "nativeRemoveLayerById"),
|
||||
|
||||
@@ -201,6 +201,8 @@ public:
|
||||
|
||||
jni::Array<jni::Object<Feature>> queryRenderedFeaturesForBox(JNIEnv&, jni::jfloat, jni::jfloat, jni::jfloat, jni::jfloat, jni::Array<jni::String>);
|
||||
|
||||
jni::Array<jni::Object<Layer>> getLayers(JNIEnv&);
|
||||
|
||||
jni::Object<Layer> getLayer(JNIEnv&, jni::String);
|
||||
|
||||
void addLayer(JNIEnv&, jlong, jni::String);
|
||||
|
||||
Reference in New Issue
Block a user