-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathHttpTilesVM.kt
More file actions
47 lines (43 loc) · 1.48 KB
/
Copy pathHttpTilesVM.kt
File metadata and controls
47 lines (43 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package ovh.plrapps.mapcompose.demo.viewmodels
import androidx.lifecycle.ViewModel
import ovh.plrapps.mapcompose.api.addLayer
import ovh.plrapps.mapcompose.api.scale
import ovh.plrapps.mapcompose.api.shouldLoopScale
import ovh.plrapps.mapcompose.core.TileStreamProvider
import ovh.plrapps.mapcompose.ui.state.MapState
import java.io.BufferedInputStream
import java.net.HttpURLConnection
import java.net.URL
/**
* Shows how MapCompose behaves with remote HTTP tiles.
*/
class HttpTilesVM : ViewModel() {
private val tileStreamProvider = makeTileStreamProvider()
val state = MapState(
levelCount = 4,
fullWidth = 8192,
fullHeight = 8192,
workerCount = 16 // Notice how we increase the worker count when performing HTTP requests
).apply {
addLayer(tileStreamProvider)
scale = 0.0
shouldLoopScale = true
}
}
/**
* A [TileStreamProvider] which performs HTTP requests.
*/
private fun makeTileStreamProvider() =
TileStreamProvider { row, col, zoomLvl ->
try {
val url =
URL("https://raw.githubusercontent.com/p-lr/MapCompose/master/demo/src/main/assets/tiles/mont_blanc_layered/$zoomLvl/$row/$col.jpg")
val connection = url.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
BufferedInputStream(connection.inputStream)
} catch (e: Exception) {
e.printStackTrace()
null
}
}