Skip to content

Commit 7a00581

Browse files
committed
Add clampToTargetBounds
1 parent e2e9b45 commit 7a00581

1 file changed

Lines changed: 29 additions & 28 deletions

File tree

openmapview/src/main/kotlin/de/afarber/openmapview/MapController.kt

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,36 @@ import kotlin.math.sin
3535
class MapController(
3636
private val context: Context,
3737
) {
38+
companion object {
39+
private const val DEFAULT_MIN_ZOOM = 2.0
40+
private const val DEFAULT_MAX_ZOOM = 19.0
41+
private const val TILE_SIZE = 256f
42+
}
43+
44+
private var minZoomPreference = DEFAULT_MIN_ZOOM
45+
private var maxZoomPreference = DEFAULT_MAX_ZOOM
46+
private var cameraTargetBounds: LatLngBounds? = null
47+
3848
private var zoom = 10.0
3949
private var center = LatLng(0.0, 0.0)
50+
51+
/**
52+
* Clamps a LatLng coordinate to remain within the camera target bounds.
53+
*
54+
* If no bounds are set, returns the input unchanged.
55+
*
56+
* @param latLng The coordinate to clamp
57+
* @return The clamped coordinate
58+
*/
59+
private fun clampToTargetBounds(latLng: LatLng): LatLng {
60+
val bounds = cameraTargetBounds ?: return latLng
61+
62+
val clampedLat = latLng.latitude.coerceIn(bounds.southwest.latitude, bounds.northeast.latitude)
63+
val clampedLng = latLng.longitude.coerceIn(bounds.southwest.longitude, bounds.northeast.longitude)
64+
65+
return LatLng(clampedLat, clampedLng)
66+
}
67+
4068
private var viewWidth = 0
4169
private var viewHeight = 0
4270
private var panOffsetX = 0f
@@ -48,16 +76,6 @@ class MapController(
4876

4977
private var lastDrawnTiles = mutableSetOf<TileCoordinate>()
5078

51-
companion object {
52-
private const val DEFAULT_MIN_ZOOM = 2.0
53-
private const val DEFAULT_MAX_ZOOM = 19.0
54-
private const val TILE_SIZE = 256f
55-
}
56-
57-
private var minZoomPreference = DEFAULT_MIN_ZOOM
58-
private var maxZoomPreference = DEFAULT_MAX_ZOOM
59-
private var cameraTargetBounds: LatLngBounds? = null
60-
6179
private val markers = mutableListOf<Marker>()
6280
private val defaultMarkerIcon by lazy { MarkerIconFactory.getDefaultIcon() }
6381
var onMarkerClickListener: ((Marker) -> Boolean)? = null
@@ -217,7 +235,7 @@ class MapController(
217235
fun setLatLngBoundsForCameraTarget(bounds: LatLngBounds?) {
218236
cameraTargetBounds = bounds
219237
// Apply constraint immediately if camera is currently outside bounds
220-
bounds?.let { center = clampToTargetBounds(center) }
238+
bounds?.let { setCenter(center) }
221239
}
222240

223241
/**
@@ -227,23 +245,6 @@ class MapController(
227245
*/
228246
fun getLatLngBoundsForCameraTarget(): LatLngBounds? = cameraTargetBounds
229247

230-
/**
231-
* Clamps a LatLng coordinate to remain within the camera target bounds.
232-
*
233-
* If no bounds are set, returns the input unchanged.
234-
*
235-
* @param latLng The coordinate to clamp
236-
* @return The clamped coordinate
237-
*/
238-
private fun clampToTargetBounds(latLng: LatLng): LatLng {
239-
val bounds = cameraTargetBounds ?: return latLng
240-
241-
val clampedLat = latLng.latitude.coerceIn(bounds.southwest.latitude, bounds.northeast.latitude)
242-
val clampedLng = latLng.longitude.coerceIn(bounds.southwest.longitude, bounds.northeast.longitude)
243-
244-
return LatLng(clampedLat, clampedLng)
245-
}
246-
247248
/**
248249
* Sets the tile source for rendering the base map.
249250
*

0 commit comments

Comments
 (0)