setBounds move map to incorrect area
Environment
- Android OS version: Android 11
- Devices affected: Samsung Galaxy Tab A8
- Maps SDK Version: 10.8.1
Observed behavior and steps to reproduce
when I set bounds for small region, the map is moved to the incorrect area. Example:
mapView = findViewById(R.id.mapView)
mapView?.getMapboxMap()?.loadStyleUri(Style.MAPBOX_STREETS)
mapView?.getMapboxMap()?.setBounds(
CameraBoundsOptions.Builder()
.bounds(CoordinateBounds(
Point.fromLngLat(30.000000010,70.455706592),
Point.fromLngLat(30.000000000,70.455706582)
))
.build()
)
mapView?.getMapboxMap()?.addOnMapClickListener {
println(mapView!!.getMapboxMap().cameraState.center) // here I get 210.000000005, 70.45567451371102
true
}
Expected behavior
setBounds should move map to bounds center point
Notes / preliminary analysis
Additional links and references
@bartek977 setBounds will only restrict the camera, not move it. You first need to set the camera and then returning the cameraState.center should behave as expected.
@ZiZasaurus so why after calling setBounds the map is moved? I need to center map for given coordinates (southwest, northeast) and set bounds so user can't move the map outside boundaries, and setBounds usually works fine (center map to correct position and block moving map), but for small areas the map is moved somewhere else.
I changed code to:
mapView?.getMapboxMap()?.loadStyleUri(Style.MAPBOX_STREETS)
val bounds = CoordinateBounds(Point.fromLngLat(
30.000000010,70.455706592),
Point.fromLngLat(30.000000000,70.455706582
)
)
val cameraBounds = mapView!!.getMapboxMap().cameraForCoordinateBounds(bounds)
mapView?.getMapboxMap()?.setCamera(cameraBounds)
mapView?.getMapboxMap()?.setBounds(
CameraBoundsOptions.Builder()
.bounds(mapView!!.getMapboxMap().coordinateBoundsForCamera(mapView!!.getMapboxMap().cameraState.toCameraOptions()))
.build()
)
mapView?.getMapboxMap()?.addOnMapClickListener {
println(mapView!!.getMapboxMap().cameraState.center)
true
}
and it also doesn't work. After calling mapView?.getMapboxMap()?.setCamera(cameraBounds) the map is in correct position, but after setting bounds the map is moved to the wrong one.
@bartek977 thank you for this additional info! I have brought this to the team for further investigation, however, as a workaround, if you can avoid using such small bounds with setBounds you should get the result you expect.
Same issue with those bounds
public static final double OFFLINE_REGION_NORTHEAST_LATITUDE = 43.693044;
public static final double OFFLINE_REGION_NORTHEAST_LONGITUDE = 4.656988;
public static final double OFFLINE_REGION_SOUTHWEST_LATITUDE = 43.653161;
public static final double OFFLINE_REGION_SOUTHWEST_LONGITUDE = 4.594671;
For anyone with the same problem, I have some workaround:
- Move the camera to the given coordinates
- Check zoom, if it's equal to 22.0 (mapbox max zoom level), change it (for me 21.5 works fine, but for coordinates I used in the example I have to use 17.0)
- Set bounds using the updated camera state
if (mapView!!.getMapboxMap().cameraState.zoom >= 22.0) {
val cameraWithUpdatedZoom = mapView!!.getMapboxMap().cameraState
.toCameraOptions()
.toBuilder()
.zoom(17.0)
.build()
mapView!!.getMapboxMap().setCamera(cameraWithUpdatedZoom)
}
mapView?.getMapboxMap()?.setBounds(
CameraBoundsOptions.Builder()
.bounds(mapView!!.getMapboxMap().coordinateBoundsForCamera(mapView!!.getMapboxMap().cameraState.toCameraOptions()))
.build()
)
Nevertheless @ZiZasaurus do you have any update about this problem and possible fix on it?