Overview
The Camera2dInterface controls the map’s viewport, including position, zoom level, and boundaries. Access it through the MapView’s getCamera() method.
Getting the Camera
val camera = mapView.getCamera()
Position and Zoom
moveToCenterPositionZoom()
Moves the camera to a specific position and zoom level.
fun moveToCenterPositionZoom(
center: Coord,
zoom: Double,
animated: Boolean
)
The coordinate to center the camera on
The zoom level (larger values = more zoomed in)
Whether to animate the camera movement
Example:
mapView.getCamera().moveToCenterPositionZoom(
Coord(
CoordinateSystemIdentifiers.EPSG4326(),
8.378232525377973,
46.962592372639634,
0.0
),
10000000.0,
false
)
Zoom Limits
setMinZoom()
Sets the minimum allowed zoom level.
fun setMinZoom(zoom: Double)
Example:
mapView.getCamera().setMinZoom(5000000.0)
setMaxZoom()
Sets the maximum allowed zoom level.
fun setMaxZoom(zoom: Double)
Example:
mapView.getCamera().setMaxZoom(300.0)
Important Notes
The MapView must be ready and running to properly compute requested view bounds. Check the mapViewState StateFlow before performing camera operations.
Complete Example
class MapActivity : AppCompatActivity() {
private lateinit var mapView: MapView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Setup MapView
mapView.setupMap(MapConfig(CoordinateSystemFactory.getEpsg3857System()))
mapView.registerLifecycle(lifecycle)
// Configure camera
val camera = mapView.getCamera()
// Set zoom limits
camera.setMinZoom(5000000.0)
camera.setMaxZoom(300.0)
// Move to initial position
camera.moveToCenterPositionZoom(
Coord(
CoordinateSystemIdentifiers.EPSG4326(),
8.378232,
46.962592,
0.0
),
10000000.0,
false
)
}
}
Coordinate Systems
The camera operates in the coordinate system specified in the MapConfig during setup. Common coordinate systems:
- EPSG 3857 (Web Mercator): Used by most web mapping services
- EPSG 4326 (WGS84): Standard latitude/longitude coordinates
Converting coordinates:
// Create a coordinate in EPSG 4326 (lat/lon)
val latLonCoord = Coord(
CoordinateSystemIdentifiers.EPSG4326(),
longitude,
latitude,
0.0
)
// The camera will automatically handle conversion if needed
camera.moveToCenterPositionZoom(latLonCoord, zoom, animated)