@@ -16,9 +16,13 @@ import com.mapbox.maps.CameraBoundsOptions
1616import com.mapbox.maps.CoordinateBounds
1717import com.mapbox.maps.MapboxMap
1818import com.mapbox.maps.Style
19+ import com.mapbox.maps.extension.style.layers.generated.FillLayer
20+ import com.mapbox.maps.extension.style.layers.generated.fillLayer
21+ import com.mapbox.maps.extension.style.layers.getLayerAs
22+ import com.mapbox.maps.extension.style.layers.properties.generated.Visibility
1923import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
2024import com.mapbox.maps.extension.style.sources.generated.geoJsonSource
21- import com.mapbox.maps.extension.style.sources.getSource
25+ import com.mapbox.maps.extension.style.sources.getSourceAs
2226import com.mapbox.maps.extension.style.style
2327import com.mapbox.maps.testapp.R
2428import com.mapbox.maps.testapp.databinding.ActivityRestrictBoundsBinding
@@ -41,6 +45,12 @@ class RestrictBoundsActivity : AppCompatActivity() {
4145 + geoJsonSource(BOUNDS_ID ) {
4246 featureCollection(FeatureCollection .fromFeatures(listOf ()))
4347 }
48+ + fillLayer(BOUNDS_ID , BOUNDS_ID ) {
49+ fillColor(Color .RED )
50+ fillOpacity(0.15 )
51+ visibility(Visibility .NONE )
52+ slot(" bottom" )
53+ }
4454 }
4555 ) { setupBounds(SAN_FRANCISCO_BOUND ) }
4656 showCrosshair()
@@ -69,39 +79,47 @@ class RestrictBoundsActivity : AppCompatActivity() {
6979 setupBounds(INFINITE_BOUNDS )
7080 true
7181 }
82+ R .id.menu_action_toggle_bounds -> {
83+ toggleShowBounds()
84+ true
85+ }
7286 else -> {
7387 super .onOptionsItemSelected(item)
7488 }
7589 }
7690 }
7791
92+ private fun toggleShowBounds () {
93+ mapboxMap.getStyle {
94+ val boundsFillLayer = it.getLayerAs<FillLayer >(BOUNDS_ID )!!
95+ val visibility: Visibility = boundsFillLayer.visibility!!
96+ when (visibility) {
97+ Visibility .NONE -> boundsFillLayer.visibility(Visibility .VISIBLE )
98+ Visibility .VISIBLE -> boundsFillLayer.visibility(Visibility .NONE )
99+ }
100+ }
101+ }
102+
78103 private fun setupBounds (bounds : CameraBoundsOptions ) {
79104 mapboxMap.getStyle { style ->
80- mapboxMap.setBounds(bounds)
81- showBoundsArea(bounds, style)
105+ setupBoundsArea(bounds, style)
82106 }
83107 }
84108
85- private fun showBoundsArea (boundsOptions : CameraBoundsOptions , style : Style ) {
86- val source = style.getSource(BOUNDS_ID ) as GeoJsonSource
87- val bounds = boundsOptions.bounds
88- val list = mutableListOf<List <Point >>()
89- bounds?.let {
90- if (! it.infiniteBounds) {
91- val northEast = it.northeast
92- val southWest = it.southwest
93- val northWest = Point .fromLngLat(southWest.longitude(), northEast.latitude())
94- val southEast = Point .fromLngLat(northEast.longitude(), southWest.latitude())
95- list.add(
96- mutableListOf (northEast, southEast, southWest, northWest, northEast)
97- )
98- }
99- }
100- if (list.isNotEmpty()) {
101- source.geometry(
102- Polygon .fromLngLats(
103- list
104- )
109+ private fun setupBoundsArea (boundsOptions : CameraBoundsOptions , style : Style ) {
110+ mapboxMap.setBounds(boundsOptions)
111+ // In this example we always have bounds
112+ val bounds = boundsOptions.bounds!!
113+ if (! bounds.infiniteBounds) {
114+ val northEast = bounds.northeast
115+ val southWest = bounds.southwest
116+ val northWest = Point .fromLngLat(southWest.longitude(), northEast.latitude())
117+ val southEast = Point .fromLngLat(northEast.longitude(), southWest.latitude())
118+ val boundsBox = listOf (northEast, southEast, southWest, northWest, northEast)
119+ // Update the source with the new bounds
120+ style.getSourceAs<GeoJsonSource >(BOUNDS_ID )!! .geometry(
121+ // We only want one polygon: the box around the bounds
122+ Polygon .fromLngLats(listOf (boundsBox))
105123 )
106124 }
107125 }
0 commit comments