This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding example of removing feature visibility with layer filter (#1308)
- Loading branch information
Langston Smith
authored
May 19, 2020
1 parent
753bf74
commit 6f71c46
Showing
9 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...o/src/main/java/com/mapbox/mapboxandroiddemo/examples/dds/KotlinFilterFeaturesActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
MapboxAndroidDemo/src/main/res/layout/activity_dds_filter_feature.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters