Skip to main content

Local Style MapSnapshotter

This example demonstrates the process of creating a snapshot from a configuration that does not use a style Uniform Resource Identifier (URI) or JSON format with the Maps SDK for Android. The LocalStyleMapSnapshotterActivity class generates a snapshot of a map with a specific custom configuration.

In this example, a Snapshotter instance is created with specified options such as size and pixel ratio using MapSnapshotOptions. The app sets the map's camera to a specific zoom level and center coordinates, and defines a custom style directly in the code using JSON format. After starting the snapshot process, the resulting bitmap is displayed on an ImageView which is then set as the content view of the activity.

Android Examples App Available

This example code is part of the Maps SDK for Android Examples App, a working Android project available on GitHub. Android developers are encouraged to run the examples app locally to interact with this example in an emulator and explore other features of the Maps SDK.

See our Run the Maps SDK for Android Examples App tutorial for step-by-step instructions.

LocalStyleMapSnapshotterActivity.kt
package com.mapbox.maps.testapp.examples.snapshotter

import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.maps.*

/**
* Activity to validate creating a snapshot from a configuration not using style URI or JSON
*/
class LocalStyleMapSnapshotterActivity : AppCompatActivity() {
private lateinit var mapSnapshotter: Snapshotter

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val snapshotterOptions = MapSnapshotOptions.Builder()
.size(Size(512.0f, 512.0f))
.pixelRatio(1.0f)
.build()

mapSnapshotter = Snapshotter(this, snapshotterOptions)
mapSnapshotter.setCamera(
CameraOptions.Builder().zoom(14.0).center(
Point.fromLngLat(
4.895033, 52.374724
)
).build()
)
mapSnapshotter.setStyleJson(
"""
{
"version": 8,
"metadata": {
"test": {
"width": 64,
"height": 64
}
},
"sources": {},
"layers": [
{
"id": "background",
"type": "background",
"paint": {
"background-color": "red"
}
}
]
}
""".trimIndent()
)
// ignore error in this example
mapSnapshotter.start { bitmap, _ ->
val imageView = ImageView(this)
imageView.setImageBitmap(bitmap)
setContentView(imageView)
}
}

override fun onDestroy() {
super.onDestroy()
mapSnapshotter.destroy()
}

companion object {
const val TAG: String = "LocalStyleMapSnapshotterActivity"
}
}
Was this example helpful?