Skip to content

Commit 7976c58

Browse files
authored
Bring back bounds layer and simplify code (#2912)
* Bring back bounds layer and simplify code * Fix rsync install package failure
1 parent 5bdca7f commit 7976c58

File tree

3 files changed

+46
-23
lines changed

3 files changed

+46
-23
lines changed

app/src/main/java/com/mapbox/maps/testapp/examples/camera/RestrictBoundsActivity.kt

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import com.mapbox.maps.CameraBoundsOptions
1616
import com.mapbox.maps.CoordinateBounds
1717
import com.mapbox.maps.MapboxMap
1818
import 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
1923
import com.mapbox.maps.extension.style.sources.generated.GeoJsonSource
2024
import 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
2226
import com.mapbox.maps.extension.style.style
2327
import com.mapbox.maps.testapp.R
2428
import 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
}

app/src/main/res/menu/menu_bounds.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@
1717
android:id="@+id/menu_action_reset"
1818
android:title="@string/restrict_reset"
1919
app:showAsAction="never"/>
20+
<item
21+
android:id="@+id/menu_action_toggle_bounds"
22+
android:title="@string/toggle_show_bounds"
23+
app:showAsAction="never"/>
2024
</menu>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
<string name="restrict_almost_worldview">Restrict almost worldview</string>
4848
<string name="restrict_across_idl">Restrict across IDL</string>
4949
<string name="restrict_reset">Reset bounds</string>
50+
<string name="toggle_show_bounds">Toggle show bounds</string>
5051

5152
<!--Space station toast-->
5253
<string name="space_station_toast">Zoom in closely on the space station\nto see it moving across the map</string>

0 commit comments

Comments
 (0)