Custom tile source #92
-
Hello everyone I don't quite understand how to use third-party map sources. val tileStreamProvider = TileStreamProvider { row, col, zoomLvl ->
withContext(Dispatchers.IO) {
try {
val url = URL("https://tile.openstreetmap.org/$zoomLvl/$row/$col.png")
val connection = url.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
BufferedInputStream(connection.inputStream)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
} Could you show an example and use maps from open sources. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Guys need help please! |
Beta Was this translation helpful? Give feedback.
-
There is a demo app with a "RemoteHttp" demo. |
Beta Was this translation helpful? Give feedback.
-
Here is a complete example (note that OSM blocks some user-agents): /**
* Shows how MapCompose behaves with remote HTTP tiles.
*/
class HttpTilesVM : ViewModel() {
private val tileStreamProvider = makeTileStreamProvider()
private val lvlCnt = 8 // avoid using level higher than 16
private val mapSize = mapSizeAtLevel(lvlCnt - 1, tileSize = 256)
val state: MapState by mutableStateOf(
MapState(lvlCnt, mapSize, mapSize, workerCount = 16).apply {
addLayer(tileStreamProvider)
scale = 0f
}
)
}
/**
* A [TileStreamProvider] which performs HTTP requests.
*/
private fun makeTileStreamProvider() =
TileStreamProvider { row, col, zoomLvl ->
try {
val url = URL("https://tile.openstreetmap.org/$zoomLvl/$col/$row.png")
val connection = url.openConnection() as HttpURLConnection
connection.setRequestProperty("User-Agent", "Chrome/120.0.0.0 Safari/537.36")
connection.doInput = true
connection.connect()
BufferedInputStream(connection.inputStream)
} catch (e: Exception) {
e.printStackTrace()
null
}
}
/**
* wmts level are 0 based.
* At level 0, the map corresponds to just one tile.
*/
private fun mapSizeAtLevel(wmtsLevel: Int, tileSize: Int): Int {
return tileSize * 2.0.pow(wmtsLevel).toInt()
} |
Beta Was this translation helpful? Give feedback.
Here is a complete example (note that OSM blocks some user-agents):