Карта работает, ура! URL.openStream заменён на Ktor, поправлены размеры блока с картой

This commit is contained in:
DarkCat09 2023-06-05 20:11:42 +04:00
parent 266e2001f2
commit 03a758673b
3 changed files with 28 additions and 4 deletions

View file

@ -119,6 +119,11 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0") implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
// Ktor
val ktor_version = "2.3.1"
implementation("io.ktor:ktor-client-core:$ktor_version")
implementation("io.ktor:ktor-client-cio:$ktor_version")
// Map Compose library // Map Compose library
implementation("ovh.plrapps:mapcompose:2.7.1") implementation("ovh.plrapps:mapcompose:2.7.1")
} }

View file

@ -3,6 +3,7 @@ package ru.nm17.narodmon.ui.pages
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilterChip import androidx.compose.material3.FilterChip
@ -13,6 +14,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.navigation.NavController import androidx.navigation.NavController
@ -31,11 +33,16 @@ fun SensorsPage(navController: NavController) {
val mapVM by remember { mutableStateOf(MapViewModel()) } val mapVM by remember { mutableStateOf(MapViewModel()) }
var filter by remember { mutableStateOf(SensorsFilter.All) } var filter by remember { mutableStateOf(SensorsFilter.All) }
val scrConfig = LocalConfiguration.current
val scrHalfHeight = scrConfig.screenHeightDp / 2
GenericNavScaffold( GenericNavScaffold(
title = { Text(text = stringResource(R.string.sensors_page_title)) } title = { Text(text = stringResource(R.string.sensors_page_title)) }
) { ) {
Column(modifier = Modifier.padding(it)) { Column(modifier = Modifier.padding(it)) {
MapUI(state = mapVM.state)
MapUI(state = mapVM.state, modifier = Modifier.height(scrHalfHeight.dp))
Row( Row(
modifier = Modifier.padding(horizontal = 8.dp), modifier = Modifier.padding(horizontal = 8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp), horizontalArrangement = Arrangement.spacedBy(8.dp),

View file

@ -3,20 +3,32 @@ package ru.nm17.narodmon.ui.viewmodel
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsChannel
import io.ktor.utils.io.jvm.javaio.toInputStream
import ovh.plrapps.mapcompose.api.addLayer import ovh.plrapps.mapcompose.api.addLayer
import ovh.plrapps.mapcompose.core.TileStreamProvider import ovh.plrapps.mapcompose.core.TileStreamProvider
import ovh.plrapps.mapcompose.ui.state.MapState import ovh.plrapps.mapcompose.ui.state.MapState
import java.io.InputStream
import java.net.URL import java.net.URL
class MapViewModel : ViewModel() { class MapViewModel : ViewModel() {
private val client = HttpClient(CIO)
private val tileStreamProvider = TileStreamProvider { row, col, zoom -> private val tileStreamProvider = TileStreamProvider { row, col, zoom ->
URL("https://tile.openstreetmap.org/${zoom}/${row}/${col}.png").openStream() requestTile(row, col, zoom)
//URL("https://tile2.maps.2gis.com/poi?x=${row}&y=${col}&z=${zoom}&v=1&ts=online_sd").openStream()
} }
val state: MapState by mutableStateOf( val state: MapState by mutableStateOf(
MapState(4, 4, 4096).apply { MapState(4, 4096, 4096).apply {
addLayer(tileStreamProvider) addLayer(tileStreamProvider)
} }
) )
private suspend fun requestTile(row: Int, col: Int, zoom: Int): InputStream {
val response = client.get("https://tile.openstreetmap.org/${zoom}/${col}/${row}.png")
return response.bodyAsChannel().toInputStream()
}
} }