Legacy offline Map
This example demonstrates the process of downloading an offline region using the OfflineRegionManager
and displaying a map at the offline region location using the Maps SDK for Android. The app creates an offline region based on specific parameters such as geometry
, pixelRatio
, minZoom
, and maxZoom
, and sets the style URL to Style.SATELLITE
. The callback function OfflineRegionCreateCallback
is used to handle the creation of the offline region, and an OfflineRegionObserver
monitors the status of the download process, providing feedback on resource download progress and handling any errors that may occur.
Once the offline region download is complete, a button is displayed to load the map at the defined offline region. Clicking the button creates a MapView
with the specified style URI and adjusts the camera position based on the defined zoom level and center point. The map view is then set to be displayed on the screen by calling setContentView(mapView)
. Additionally, the app manages the lifecycle of the map view by calling onStart()
when the activity starts, onStop()
when the activity stops, and onDestroy()
when the activity is destroyed, ensuring proper resource handling.
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.
package com.mapbox.maps.testapp.examples
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.maps.*
import com.mapbox.maps.testapp.databinding.ActivityLegacyOfflineBinding
/**
* Example app that downloads an offline region and when succeeded
* shows a button to load a map at the offline region definition.
*/
@Suppress("DEPRECATION")
class LegacyOfflineActivity : AppCompatActivity() {
private lateinit var offlineManager: OfflineRegionManager
private lateinit var offlineRegion: OfflineRegion
private var mapView: MapView? = null
private lateinit var binding: ActivityLegacyOfflineBinding
private val regionObserver: OfflineRegionObserver = object : OfflineRegionObserver {
override fun errorOccurred(error: OfflineRegionError) {
if (error.isFatal) {
logE(TAG, "Fatal error: ${error.type}, ${error.message}")
} else {
logW(TAG, "Error downloading some resources: ${error.type}, ${error.message}")
}
offlineRegion.setOfflineRegionDownloadState(OfflineRegionDownloadState.INACTIVE)
}
override fun statusChanged(status: OfflineRegionStatus) {
logD(
TAG,
"${status.completedResourceCount}/${status.requiredResourceCount} resources; ${status.completedResourceSize} bytes downloaded."
)
if (status.downloadState == OfflineRegionDownloadState.INACTIVE) {
downloadComplete()
return
}
}
}
private val callback: OfflineRegionCreateCallback = OfflineRegionCreateCallback { expected ->
if (expected.isValue) {
expected.value?.let {
offlineRegion = it
it.setOfflineRegionObserver(regionObserver)
it.setOfflineRegionDownloadState(OfflineRegionDownloadState.ACTIVE)
}
} else {
logE(TAG, expected.error!!)
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLegacyOfflineBinding.inflate(layoutInflater)
setContentView(binding.root)
offlineManager = OfflineRegionManager()
offlineManager.createOfflineRegion(
OfflineRegionGeometryDefinition.Builder()
.geometry(point)
.pixelRatio(2f)
.minZoom(zoom - 2)
.maxZoom(zoom + 2)
.styleURL(styleUrl)
.glyphsRasterizationMode(GlyphsRasterizationMode.NO_GLYPHS_RASTERIZED_LOCALLY)
.build(),
callback
)
}
private fun downloadComplete() {
binding.downloadProgress.visibility = View.GONE
binding.showMapButton.visibility = View.VISIBLE
binding.showMapButton.setOnClickListener {
it.visibility = View.GONE
// create mapView
mapView = MapView(
this@LegacyOfflineActivity,
MapInitOptions(context = this@LegacyOfflineActivity, styleUri = styleUrl)
)
mapView?.mapboxMap?.setCamera(CameraOptions.Builder().zoom(zoom).center(point).build())
setContentView(mapView)
mapView?.onStart()
}
}
override fun onStart() {
super.onStart()
mapView?.onStart()
}
override fun onStop() {
super.onStop()
mapView?.onStop()
}
override fun onDestroy() {
super.onDestroy()
offlineRegion.invalidate { }
mapView?.onDestroy()
}
companion object {
private const val TAG = "Offline"
private const val zoom = 16.0
private val point: Point = Point.fromLngLat(57.818901, 20.071357)
private const val styleUrl = Style.SATELLITE
}
}