I'm building a running tracker app and i want to save a screenshot of the map at the end of the run I'm using utsmannn's implementation of osmdroid in my app and was wondering if there's a way to do this using utsmannn library since i didn't find anything in their wiki, if not is there a way to do it using osmdroid directly ?
this is my base map composable
@Composable
fun TrackerMap(
modifier: Modifier = Modifier,
isRunFinished: Boolean,
currentLocation: Location?,
locations: List<List<LocationTimestamp>>,
onSnapshot: (Bitmap) -> Unit
) {
val context = LocalContext.current
val cameraState = rememberCameraState {
zoom = 17.0
}
LaunchedEffect(currentLocation, isRunFinished) {
if (currentLocation != null && !isRunFinished) {
cameraState.geoPoint = GeoPoint(currentLocation.lat, currentLocation.long)
}
}
val overlayManagerState = rememberOverlayManagerState()
var mapProperties by remember {
mutableStateOf(DefaultMapProperties)
}
SideEffect {
mapProperties = mapProperties
.copy(isTilesScaledToDpi = true)
.copy(tileSources = TileSourceFactory.MAPNIK)
.copy(isEnableRotationGesture = false)
.copy(isFlingEnable = false)
.copy(zoomButtonVisibility = ZoomButtonVisibility.NEVER)
}
OpenStreetMap(
modifier = modifier.fillMaxSize(),
cameraState = cameraState,
properties = mapProperties,
overlayManagerState = overlayManagerState,
onFirstLoadListener = {
mapInstance = overlayManagerState.getMap()
val copyright = CopyrightOverlay(context)
overlayManagerState.overlayManager.add(copyright)
}
) {
RunikPolylines(locations = locations)
}
}
I'm really new to android and may need your answer basically in baby terms !!
I'm building a running tracker app and i want to save a screenshot of the map at the end of the run I'm using utsmannn's implementation of osmdroid in my app and was wondering if there's a way to do this using utsmannn library since i didn't find anything in their wiki, if not is there a way to do it using osmdroid directly ?
this is my base map composable
@Composable
fun TrackerMap(
modifier: Modifier = Modifier,
isRunFinished: Boolean,
currentLocation: Location?,
locations: List<List<LocationTimestamp>>,
onSnapshot: (Bitmap) -> Unit
) {
val context = LocalContext.current
val cameraState = rememberCameraState {
zoom = 17.0
}
LaunchedEffect(currentLocation, isRunFinished) {
if (currentLocation != null && !isRunFinished) {
cameraState.geoPoint = GeoPoint(currentLocation.lat, currentLocation.long)
}
}
val overlayManagerState = rememberOverlayManagerState()
var mapProperties by remember {
mutableStateOf(DefaultMapProperties)
}
SideEffect {
mapProperties = mapProperties
.copy(isTilesScaledToDpi = true)
.copy(tileSources = TileSourceFactory.MAPNIK)
.copy(isEnableRotationGesture = false)
.copy(isFlingEnable = false)
.copy(zoomButtonVisibility = ZoomButtonVisibility.NEVER)
}
OpenStreetMap(
modifier = modifier.fillMaxSize(),
cameraState = cameraState,
properties = mapProperties,
overlayManagerState = overlayManagerState,
onFirstLoadListener = {
mapInstance = overlayManagerState.getMap()
val copyright = CopyrightOverlay(context)
overlayManagerState.overlayManager.add(copyright)
}
) {
RunikPolylines(locations = locations)
}
}
I'm really new to android and may need your answer basically in baby terms !!
Share Improve this question asked Mar 16 at 22:51 esmail.unlineaesmail.unlinea 1711 gold badge1 silver badge7 bronze badges1 Answer
Reset to default 0In OSMDroid, you can take a screenshot of the map view using the getDrawingCache() method of the MapView. Here's how you can do it in Kotlin:
Steps to Capture Screenshot in OSMDroid
Enable the drawing cache for the
MapView
.Capture the bitmap from the
MapView
.Save the bitmap to storage.
Kotlin Code to Capture OSMDroid Map Screenshotimport android.graphics.Bitmap import android.graphics.Canvas import android.os.Bundle import android.os.Environment import android.view.View import android.widget.Button import androidx.appcompat.app.AppCompatActivity import .osmdroid.config.Configuration import .osmdroid.tileprovider.tilesource.TileSourceFactory import .osmdroid.util.GeoPoint import .osmdroid.views.MapView import java.io.File import java.io.FileOutputStream class MainActivity : AppCompatActivity() { private lateinit var mapView: MapView private lateinit var btnCapture: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) Configuration.getInstance().load(applicationContext, getSharedPreferences("osmdroid", MODE_PRIVATE)) setContentView(R.layout.activity_main) // Initialize MapView mapView = findViewById(R.id.mapView) btnCapture = findViewById(R.id.btnCapture) mapView.setTileSource(TileSourceFactory.MAPNIK) mapView.setMultiTouchControls(true) mapView.controller.setZoom(15.0) mapView.controller.setCenter(GeoPoint(37.7749, -122.4194)) // Example: San Francisco // Capture Screenshot on Button Click btnCapture.setOnClickListener { captureMapScreenshot() } } private fun captureMapScreenshot() { try { // Create bitmap val bitmap = Bitmap.createBitmap(mapView.width, mapView.height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) mapView.draw(canvas) // Draw the map on canvas // Save the bitmap to file val file = File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "map_screenshot.png") val outputStream = FileOutputStream(file) bitmappress(Bitmap.CompressFormat.PNG, 100, outputStream) outputStream.flush() outputStream.close() // Screenshot saved successfully println("Screenshot saved at: ${file.absolutePath}") } catch (e: Exception) { e.printStackTrace() } } }
XML Layout (
activity_main.xml
)<LinearLayout xmlns:android="http://schemas.android/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <.osmdroid.views.MapView android:id="@+id/mapView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <Button android:id="@+id/btnCapture" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Capture Screenshot"/> </LinearLayout>
How It Works
The
captureMapScreenshot()
method:Creates a
Bitmap
with the size ofMapView
.Draws the map onto the
Canvas
.Saves the image to the device storage.
The
onClickListener
for the button triggers the screenshot function.