On this page
Download map tiles within a polygonal area - Kotlin SDK
The MapTiler SDK for Kotlin provides a powerful Offline Pack API that allows you to download tiles within a polygonal area defined by a GeoJSON Polygon for use without an active internet connection.
Before you begin, ensure you have followed the Android getting started with offline pack API to set up your project and obtain your API key.
Offline Pack for a Polygon (GeoJSON Polygon)
import com.maptiler.maptilersdk.offline.MTOfflineManager
import com.maptiler.maptilersdk.offline.MTOfflineRegionDefinition
import com.maptiler.maptilersdk.offline.MTOfflineRegionGeometry
import com.maptiler.maptilersdk.map.style.MTMapReferenceStyle
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
// Your GeoJSON Polygon
val polygonGeoJson = """
{
"type": "Polygon",
"coordinates": [[
[8.5417, 47.3769],
[8.5457, 47.3769],
[8.5457, 47.3799],
[8.5417, 47.3799],
[8.5417, 47.3769]
]]
}
"""
MainScope().launch {
try {
// Parse the polygon geometry from GeoJSON
val geometry = MTOfflineRegionGeometry.Polygon.fromGeoJson(polygonGeoJson)
// Create the polygon definition
val polyDefinition = MTOfflineRegionDefinition(
geometry = geometry,
minZoom = 0,
maxZoom = 14,
referenceStyle = MTMapReferenceStyle.STREETS
)
// Create and download the pack
val pack = MTOfflineManager.createPack(context, polyDefinition)
pack.download()
println("Polygon download started")
} catch (e: Exception) {
e.printStackTrace()
}
}
Learn more
For more information browse our API Reference
Check out our SDK Kotlin Examples.
Was this helpful?