summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-02-24 14:29:57 +0200
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-02-28 09:33:04 -0800
commitec1dc2622b633227321ffde962ecc7c85b751190 (patch)
tree962a9c11e0d75dfb9cd01d7aa28e5e1a60b1fedf
parent62e85c1224de6d29b3c52f7049aff7e14347211d (diff)
downloadqtlocation-mapboxgl-ec1dc2622b633227321ffde962ecc7c85b751190.tar.gz
[android] layer list accessor + example
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java16
-rwxr-xr-xplatform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/NativeMapView.java9
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/testapp/style/RuntimeStyleTests.java24
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java14
-rw-r--r--platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_runtime_style.xml4
-rwxr-xr-xplatform/android/src/native_map_view.cpp19
-rwxr-xr-xplatform/android/src/native_map_view.hpp2
7 files changed, 88 insertions, 0 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 c95d391271..cf56f63469 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
@@ -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) {
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 aed3f6b307..a3ec1cc3c3 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
@@ -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;
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 04c57636f2..f24e6dbf52 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
@@ -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;
@@ -56,6 +60,26 @@ public class RuntimeStyleTests {
}
@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);
onView(withId(R.id.mapView)).perform(new AddRemoveLayerAction());
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java
index 6e05932167..8624feaecb 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/RuntimeStyleActivity.java
@@ -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) {
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_runtime_style.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_runtime_style.xml
index 7f49dc7ba4..4b1f928d18 100644
--- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_runtime_style.xml
+++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/menu/menu_runtime_style.xml
@@ -3,6 +3,10 @@
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"
mapbox:showAsAction="never" />
diff --git a/platform/android/src/native_map_view.cpp b/platform/android/src/native_map_view.cpp
index a0bd06345f..ddc5447347 100755
--- a/platform/android/src/native_map_view.cpp
+++ b/platform/android/src/native_map_view.cpp
@@ -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"),
diff --git a/platform/android/src/native_map_view.hpp b/platform/android/src/native_map_view.hpp
index b8a7a3914c..fb6c7fa69f 100755
--- a/platform/android/src/native_map_view.hpp
+++ b/platform/android/src/native_map_view.hpp
@@ -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);