Last active
October 17, 2021 10:17
-
-
Save kuwapa/3a5df3828704743345fd7824a2c71ad6 to your computer and use it in GitHub Desktop.
Mapbox, Maplibre limit map to bounds code
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
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