Skip to main content

Local Style MapSnapshotter

Use an local style with static map image generation.
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?