Skip to content

Instantly share code, notes, and snippets.

@kuwapa
Last active October 17, 2021 10:17
Show Gist options
  • Save kuwapa/3a5df3828704743345fd7824a2c71ad6 to your computer and use it in GitHub Desktop.
Save kuwapa/3a5df3828704743345fd7824a2c71ad6 to your computer and use it in GitHub Desktop.
Mapbox, Maplibre limit map to bounds code
fun MapboxMap.limitViewToBounds(bounds: LatLngBounds) {
val newBoundsHeight = bounds.latitudeSpan - projection.visibleRegion.latLngBounds.latitudeSpan
val newBoundsWidth = bounds.longitudeSpan - projection.visibleRegion.latLngBounds.longitudeSpan
val leftTopLatLng = LatLng(
bounds.latNorth - (bounds.latitudeSpan - newBoundsHeight) / 2,
bounds.lonEast - (bounds.longitudeSpan - newBoundsWidth) / 2 - newBoundsWidth,
)
val rightBottomLatLng = LatLng(
bounds.latNorth - (bounds.latitudeSpan - newBoundsHeight) / 2 - newBoundsHeight,
bounds.lonEast - (bounds.longitudeSpan - newBoundsWidth) / 2,
)
val newBounds = LatLngBounds.Builder()
.include(leftTopLatLng)
.include(rightBottomLatLng)
.build()
setLatLngBoundsForCameraTarget(newBounds)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment