Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Adding example of using layer filter to remove specific Features #1308

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions MapboxAndroidDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.dds.KotlinFilterFeaturesActivity"
android:label="@string/activity_dds_filter_features_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.javaservices.MultipleGeometriesDirectionsRouteActivity"
android:label="@string/activity_java_services_multiple_geometries_from_directions_route_title">
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SymbolLayer>("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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraTargetLat="17.184917"
mapbox:mapbox_cameraTargetLng="9.386903"
mapbox:mapbox_cameraZoom="2" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/toggle_feature_filter_visibility_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:tint="@android:color/white"
app:backgroundTint="@color/colorPrimary"
app:srcCompat="@drawable/ic_cycle_through" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>

Expand Down
4 changes: 4 additions & 0 deletions MapboxAndroidDemo/src/main/res/values/activity_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -491,4 +491,8 @@
<string name="duration">Duration:</string>
<string name="color">Color:</string>
<string name="select">Select</string>

<!-- Feature filter -->
<string name="showing_all_country_labels">Showing all country labels</string>
<string name="filtering_countries">Match filter using "name_en", applied to layer to hide some countries\' labels</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<string name="activity_dds_property_icon_switch_description">Create a SymbolLayer and set the iconId to be dependent on each Feature\'s property key/value pair. </string>
<string name="activity_dds_circle_icon_toggle_on_click_description">Use layer filters and feature properties to create the visual effect of toggling between circles and icons when they\'re tapped on.</string>
<string name="activity_dds_within_expression_description">Check whether or not a feature is fully within the bounds of a GeoJSON polygon.</string>
<string name="activity_dds_filter_features_description">Use filters to toggle the visibility of specific features. In this example, specific country labels are filtered out from a single layer.</string>
<string name="activity_styles_text_field_formatting_description">Adjust the color, size, and fonts of SymbolLayer text fields.</string>
<string name="activity_camera_animate_description">"Animate the map's camera position, tilt, bearing, and zoom."</string>
<string name="activity_camera_bounding_box_description">Position the camera so that all the given markers are in view.</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/titles_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<string name="activity_dds_property_icon_switch_title">Icon setting based on Feature property</string>
<string name="activity_dds_circle_icon_toggle_on_click_title">Circle icon toggle</string>
<string name="activity_dds_within_expression_title">Within filter</string>
<string name="activity_dds_filter_features_title">Filter specific features</string>
<string name="activity_camera_animate_title">Animate the map camera</string>
<string name="activity_camera_bounding_box_title">Fit camera in bounding box</string>
<string name="activity_camera_restrict_title">Restrict map panning</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/urls_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<string name="activity_dds_property_icon_switch_url" translatable="false">https://i.imgur.com/GE1DZMp.png</string>
<string name="activity_dds_circle_icon_toggle_on_click_url" translatable="false">https://i.imgur.com/5ewVbqM.png</string>
<string name="activity_dds_within_expression_url" translatable="false">https://i.imgur.com/9kChmHL.png</string>
<string name="activity_dds_filter_features_url" translatable="false">https://i.imgur.com/fnn6RMI.png</string>
<string name="activity_styles_text_field_formatting_url" translatable="false">https://i.imgur.com/MSoIYmU.png</string>
<string name="activity_camera_animate_url" translatable="false">http://i.imgur.com/PN3vyNJ.jpg</string>
<string name="activity_camera_bounding_box_url" translatable="false">http://i.imgur.com/A0JL21Q.png</string>
Expand Down