From 6f71c469c6fdfc2c7a9f462606e2cffba46fa0e5 Mon Sep 17 00:00:00 2001 From: Langston Smith Date: Tue, 19 May 2020 09:37:55 -0700 Subject: [PATCH] adding example of removing feature visibility with layer filter (#1308) --- .../mapboxandroiddemo/MainActivity.java | 9 ++ .../src/main/AndroidManifest.xml | 7 ++ .../dds/KotlinFilterFeaturesActivity.kt | 103 ++++++++++++++++++ .../layout/activity_dds_filter_feature.xml | 26 +++++ .../res/layout/activity_places_plugin.xml | 1 - .../src/main/res/values/activity_strings.xml | 4 + .../main/res/values/descriptions_strings.xml | 1 + .../src/main/res/values/titles_strings.xml | 1 + .../src/main/res/values/urls_strings.xml | 1 + 9 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/KotlinFilterFeaturesActivity.kt create mode 100644 MapboxAndroidDemo/src/main/res/layout/activity_dds_filter_feature.xml diff --git a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java index bb7949152..641c11681 100644 --- a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java +++ b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java @@ -39,6 +39,7 @@ import com.mapbox.mapboxandroiddemo.examples.dds.CircleLayerClusteringActivity; import com.mapbox.mapboxandroiddemo.examples.dds.CircleRadiusActivity; import com.mapbox.mapboxandroiddemo.examples.dds.CircleToIconTransitionActivity; +import com.mapbox.mapboxandroiddemo.examples.dds.KotlinFilterFeaturesActivity; import com.mapbox.mapboxandroiddemo.examples.dds.PropertyIconDeterminationActivity; import com.mapbox.mapboxandroiddemo.examples.camera.ZoomToShowClusterLeavesActivity; import com.mapbox.mapboxandroiddemo.examples.dds.WithinExpressionActivity; @@ -1678,6 +1679,14 @@ private void initializeModels() { new Intent(MainActivity.this, WithinExpressionActivity.class), R.string.activity_dds_within_expression_url, true, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( + R.id.nav_dds, + R.string.activity_dds_filter_features_title, + R.string.activity_dds_filter_features_description, + null, + new Intent(MainActivity.this, KotlinFilterFeaturesActivity.class), + R.string.activity_dds_filter_features_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( R.id.nav_basics, R.string.activity_basic_simple_mapview_title, diff --git a/MapboxAndroidDemo/src/main/AndroidManifest.xml b/MapboxAndroidDemo/src/main/AndroidManifest.xml index 106c53dce..af1159d36 100644 --- a/MapboxAndroidDemo/src/main/AndroidManifest.xml +++ b/MapboxAndroidDemo/src/main/AndroidManifest.xml @@ -97,6 +97,13 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.mapbox.mapboxandroiddemo.MainActivity" /> + + + diff --git a/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/KotlinFilterFeaturesActivity.kt b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/KotlinFilterFeaturesActivity.kt new file mode 100644 index 000000000..ce342a5c2 --- /dev/null +++ b/MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/KotlinFilterFeaturesActivity.kt @@ -0,0 +1,103 @@ +package com.mapbox.mapboxandroiddemo.examples.dds + +import android.os.Bundle +import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity +import com.mapbox.mapboxandroiddemo.R +import com.mapbox.mapboxsdk.Mapbox +import com.mapbox.mapboxsdk.maps.Style +import com.mapbox.mapboxsdk.style.expressions.Expression.* +import com.mapbox.mapboxsdk.style.layers.SymbolLayer +import kotlinx.android.synthetic.main.activity_dds_filter_feature.* + +/** + * Use filters to toggle the visibility of specific features. In this example, specific + * country labels are filtered out from a single layer. + */ +class KotlinFilterFeaturesActivity : AppCompatActivity() { + + private var specificCountryLabelsHidden = false + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + // Mapbox access token is configured here. This needs to be called either in your application + // object or in the same activity which contains the mapview. + Mapbox.getInstance(this, getString(R.string.access_token)) + + // This contains the MapView in XML and needs to be called after the access token is configured. + setContentView(R.layout.activity_dds_filter_feature) + mapView?.onCreate(savedInstanceState) + mapView?.getMapAsync { mapboxMap -> + + mapboxMap.uiSettings.setAllGesturesEnabled(false) + + mapboxMap.setStyle(Style.MAPBOX_STREETS) { + + toggle_feature_filter_visibility_fab.setOnClickListener { + + mapboxMap.getStyle { + if (it.getLayer("country-label") != null) { + it.getLayerAs("country-label")?.setFilter( + if (specificCountryLabelsHidden) all() else + match( + get("name_en"), + all( + literal("Libya"), + literal("Austria"), + literal("Spain"), + literal("Mali"), + literal("Syria"), + literal("Tunisia"), + literal("Denmark") + ), + literal(false), literal(true) + ) + ) + specificCountryLabelsHidden = !specificCountryLabelsHidden + Toast.makeText(this, if (specificCountryLabelsHidden) + R.string.filtering_countries else + R.string.showing_all_country_labels, Toast.LENGTH_SHORT).show() + } + } + } + } + } + } + + // Add the mapView lifecycle to the activity's lifecycle methods + public override fun onResume() { + super.onResume() + mapView?.onResume() + } + + override fun onStart() { + super.onStart() + mapView?.onStart() + } + + override fun onStop() { + super.onStop() + mapView?.onStop() + } + + public override fun onPause() { + super.onPause() + mapView?.onPause() + } + + override fun onLowMemory() { + super.onLowMemory() + mapView?.onLowMemory() + } + + override fun onDestroy() { + super.onDestroy() + mapView?.onDestroy() + } + + override fun onSaveInstanceState(outState: Bundle) { + super.onSaveInstanceState(outState) + mapView?.onSaveInstanceState(outState) + } +} \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_dds_filter_feature.xml b/MapboxAndroidDemo/src/main/res/layout/activity_dds_filter_feature.xml new file mode 100644 index 000000000..8edd4f613 --- /dev/null +++ b/MapboxAndroidDemo/src/main/res/layout/activity_dds_filter_feature.xml @@ -0,0 +1,26 @@ + + + + + + + + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_places_plugin.xml b/MapboxAndroidDemo/src/main/res/layout/activity_places_plugin.xml index 693b85dd1..08aa47c16 100644 --- a/MapboxAndroidDemo/src/main/res/layout/activity_places_plugin.xml +++ b/MapboxAndroidDemo/src/main/res/layout/activity_places_plugin.xml @@ -9,7 +9,6 @@ android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="match_parent" - mapbox:mapbox_cameraTargetLng="-43.334931" mapbox:mapbox_cameraZoom="0.346515" /> diff --git a/MapboxAndroidDemo/src/main/res/values/activity_strings.xml b/MapboxAndroidDemo/src/main/res/values/activity_strings.xml index cf3f37a23..9baef7a69 100644 --- a/MapboxAndroidDemo/src/main/res/values/activity_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/activity_strings.xml @@ -491,4 +491,8 @@ Duration: Color: Select + + + Showing all country labels + Match filter using "name_en", applied to layer to hide some countries\' labels diff --git a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml index 262020ffe..f80303c68 100644 --- a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml @@ -70,6 +70,7 @@ Create a SymbolLayer and set the iconId to be dependent on each Feature\'s property key/value pair. Use layer filters and feature properties to create the visual effect of toggling between circles and icons when they\'re tapped on. Check whether or not a feature is fully within the bounds of a GeoJSON polygon. + Use filters to toggle the visibility of specific features. In this example, specific country labels are filtered out from a single layer. Adjust the color, size, and fonts of SymbolLayer text fields. "Animate the map's camera position, tilt, bearing, and zoom." Position the camera so that all the given markers are in view. diff --git a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml index 33eb96af9..00854dd1c 100644 --- a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml @@ -71,6 +71,7 @@ Icon setting based on Feature property Circle icon toggle Within filter + Filter specific features Animate the map camera Fit camera in bounding box Restrict map panning diff --git a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml index 365aa9a06..30bd06b48 100644 --- a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml @@ -68,6 +68,7 @@ https://i.imgur.com/GE1DZMp.png https://i.imgur.com/5ewVbqM.png https://i.imgur.com/9kChmHL.png + https://i.imgur.com/fnn6RMI.png https://i.imgur.com/MSoIYmU.png http://i.imgur.com/PN3vyNJ.jpg http://i.imgur.com/A0JL21Q.png