[android] source list accessor + example
This commit is contained in:
committed by
Ivo van Dongen
parent
ec1dc2622b
commit
f0de56cffa
@@ -291,6 +291,22 @@ public final class MapboxMap {
|
||||
nativeMapView.removeLayer(layer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all the sources in the style
|
||||
*
|
||||
* @return all the sources in the current style
|
||||
*/
|
||||
@UiThread
|
||||
public List<Source> getSources() {
|
||||
return nativeMapView.getSources();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a source by id
|
||||
*
|
||||
* @param sourceId the source's id
|
||||
* @return the source if present in the current style
|
||||
*/
|
||||
@Nullable
|
||||
@UiThread
|
||||
public Source getSource(@NonNull String sourceId) {
|
||||
|
||||
@@ -782,6 +782,13 @@ final class NativeMapView {
|
||||
nativeRemoveLayer(layer.getNativePtr());
|
||||
}
|
||||
|
||||
public List<Source> getSources() {
|
||||
if (isDestroyedOn("getSources")) {
|
||||
return null;
|
||||
}
|
||||
return Arrays.asList(nativeGetSources());
|
||||
}
|
||||
|
||||
public Source getSource(@NonNull String sourceId) {
|
||||
if (isDestroyedOn("getSource")) {
|
||||
return null;
|
||||
@@ -1071,6 +1078,8 @@ final class NativeMapView {
|
||||
|
||||
private native void nativeRemoveLayer(long layerId);
|
||||
|
||||
private native Source[] nativeGetSources();
|
||||
|
||||
private native Source nativeGetSource(String sourceId);
|
||||
|
||||
private native void nativeAddSource(long nativeSourcePtr) throws CannotAddSourceException;
|
||||
|
||||
@@ -85,6 +85,26 @@ public class RuntimeStyleTests {
|
||||
onView(withId(R.id.mapView)).perform(new AddRemoveLayerAction());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testListSources() {
|
||||
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<Source> sources = mapboxMap.getSources();
|
||||
assertNotNull(sources);
|
||||
assertTrue(sources.size() > 0);
|
||||
for (Source source: sources) {
|
||||
assertNotNull(source);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddRemoveSource() {
|
||||
ViewUtils.checkViewIsDisplayed(R.id.mapView);
|
||||
|
||||
@@ -154,6 +154,9 @@ public class RuntimeStyleActivity extends AppCompatActivity {
|
||||
case R.id.action_list_layers:
|
||||
listLayers();
|
||||
return true;
|
||||
case R.id.action_list_sources:
|
||||
listSources();
|
||||
return true;
|
||||
case R.id.action_water_color:
|
||||
setWaterColor();
|
||||
return true;
|
||||
@@ -211,6 +214,16 @@ public class RuntimeStyleActivity extends AppCompatActivity {
|
||||
Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
private void listSources() {
|
||||
List<Source> sources = mapboxMap.getSources();
|
||||
StringBuilder builder = new StringBuilder("Sources:");
|
||||
for (Source source : sources) {
|
||||
builder.append("\n");
|
||||
builder.append(source.getId());
|
||||
}
|
||||
Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
|
||||
private void setLayerInvisible() {
|
||||
String[] roadLayers = new String[] {"water"};
|
||||
for (String roadLayer : roadLayers) {
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
android:id="@+id/action_list_layers"
|
||||
android:title="List all layers in the style"
|
||||
mapbox:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_list_sources"
|
||||
android:title="List all sources in the style"
|
||||
mapbox:showAsAction="never" />
|
||||
<item
|
||||
android:id="@+id/action_water_color"
|
||||
android:title="Color the water"
|
||||
|
||||
@@ -799,6 +799,22 @@ void NativeMapView::removeLayer(JNIEnv&, jlong layerPtr) {
|
||||
}
|
||||
}
|
||||
|
||||
jni::Array<jni::Object<Source>> NativeMapView::getSources(JNIEnv& env) {
|
||||
// Get the core sources
|
||||
std::vector<style::Source*> sources = map->getSources();
|
||||
|
||||
// Convert
|
||||
jni::Array<jni::Object<Source>> jSources = jni::Array<jni::Object<Source>>::New(env, sources.size(), Source::javaClass);
|
||||
int index = 0;
|
||||
for (auto source : sources) {
|
||||
auto jSource = jni::Object<Source>(createJavaSourcePeer(env, *map, *source));
|
||||
jSources.Set(env, index, jSource);
|
||||
jni::DeleteLocalRef(env, jSource);
|
||||
index++;
|
||||
}
|
||||
|
||||
return jSources;
|
||||
}
|
||||
|
||||
jni::Object<Source> NativeMapView::getSource(JNIEnv& env, jni::String sourceId) {
|
||||
// Find the source
|
||||
@@ -1356,6 +1372,7 @@ void NativeMapView::registerNative(jni::JNIEnv& env) {
|
||||
METHOD(&NativeMapView::addLayer, "nativeAddLayer"),
|
||||
METHOD(&NativeMapView::removeLayerById, "nativeRemoveLayerById"),
|
||||
METHOD(&NativeMapView::removeLayer, "nativeRemoveLayer"),
|
||||
METHOD(&NativeMapView::getSources, "nativeGetSources"),
|
||||
METHOD(&NativeMapView::getSource, "nativeGetSource"),
|
||||
METHOD(&NativeMapView::addSource, "nativeAddSource"),
|
||||
METHOD(&NativeMapView::removeSourceById, "nativeRemoveSourceById"),
|
||||
|
||||
@@ -211,6 +211,8 @@ public:
|
||||
|
||||
void removeLayer(JNIEnv&, jlong);
|
||||
|
||||
jni::Array<jni::Object<Source>> getSources(JNIEnv&);
|
||||
|
||||
jni::Object<Source> getSource(JNIEnv&, jni::String);
|
||||
|
||||
void addSource(JNIEnv&, jni::jlong);
|
||||
|
||||
Reference in New Issue
Block a user