Serialize Json
This commit is contained in:
@@ -1,19 +1,3 @@
|
||||
/*
|
||||
* Copyright 2023 Google LLC
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.kouros.navigation
|
||||
|
||||
import android.Manifest
|
||||
@@ -21,7 +5,6 @@ import android.annotation.SuppressLint
|
||||
import android.location.Location
|
||||
import android.location.LocationManager
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
@@ -47,7 +30,7 @@ import androidx.compose.material3.SnackbarHost
|
||||
import androidx.compose.material3.SnackbarHostState
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.livedata.observeAsState
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
@@ -68,14 +51,16 @@ import androidx.lifecycle.Observer
|
||||
import com.example.places.ui.theme.PlacesTheme
|
||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||
import com.google.accompanist.permissions.rememberMultiplePermissionsState
|
||||
import com.kouros.android.cars.carappservice.R
|
||||
|
||||
import com.kouros.navigation.data.Category
|
||||
import com.kouros.navigation.data.Constants
|
||||
import com.kouros.navigation.data.Constants.TAG
|
||||
import com.kouros.navigation.data.NavigationRepository
|
||||
import com.kouros.navigation.data.StepData
|
||||
import com.kouros.navigation.model.RouteModel
|
||||
import com.kouros.navigation.model.ViewModel
|
||||
import com.kouros.navigation.utils.NavigationUtils
|
||||
import com.kouros.navigation.utils.NavigationUtils.snapLocation
|
||||
import com.kouros.navigation.utils.calculateZoom
|
||||
import kotlinx.coroutines.launch
|
||||
import org.koin.androidx.compose.koinViewModel
|
||||
import org.maplibre.compose.camera.CameraPosition
|
||||
@@ -83,14 +68,14 @@ import org.maplibre.compose.camera.rememberCameraState
|
||||
import org.maplibre.compose.expressions.dsl.const
|
||||
import org.maplibre.compose.layers.FillLayer
|
||||
import org.maplibre.compose.layers.LineLayer
|
||||
import org.maplibre.compose.location.DesiredAccuracy
|
||||
import org.maplibre.compose.location.LocationPuck
|
||||
import org.maplibre.compose.location.LocationPuckColors
|
||||
import org.maplibre.compose.location.LocationPuckSizes
|
||||
import org.maplibre.compose.location.LocationTrackingEffect
|
||||
import org.maplibre.compose.location.rememberDefaultLocationProvider
|
||||
import org.maplibre.compose.location.rememberUserLocationState
|
||||
import org.maplibre.compose.map.GestureOptions
|
||||
import org.maplibre.compose.map.MapOptions
|
||||
import org.maplibre.compose.map.MaplibreMap
|
||||
import org.maplibre.compose.map.OrnamentOptions
|
||||
import org.maplibre.compose.sources.GeoJsonData
|
||||
import org.maplibre.compose.sources.getBaseSource
|
||||
import org.maplibre.compose.sources.rememberGeoJsonSource
|
||||
@@ -99,24 +84,24 @@ import org.maplibre.spatialk.geojson.Position
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
|
||||
val routeData = MutableLiveData("")
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
val routeData = MutableLiveData("")
|
||||
|
||||
val vieModel = ViewModel(NavigationRepository())
|
||||
val routeModel = RouteModel()
|
||||
|
||||
var tilt = 0.0
|
||||
|
||||
val curLocation = Location(LocationManager.GPS_PROVIDER)
|
||||
var tilt = 50.0
|
||||
|
||||
val instruction: MutableLiveData<StepData> by lazy {
|
||||
MutableLiveData<StepData>()
|
||||
}
|
||||
|
||||
var lastLocation = Location(LocationManager.GPS_PROVIDER)
|
||||
|
||||
val observer = Observer<String> { newRoute ->
|
||||
routeModel.startNavigation(newRoute)
|
||||
routeData.value = routeModel.route
|
||||
routeData.value = routeModel.route.routeGeoJson
|
||||
}
|
||||
|
||||
val cameraPosition = MutableLiveData(
|
||||
@@ -126,40 +111,14 @@ class MainActivity : ComponentActivity() {
|
||||
)
|
||||
)
|
||||
|
||||
var locationIndex = 0
|
||||
|
||||
var test = false
|
||||
|
||||
init {
|
||||
vieModel.route.observe(this, observer)
|
||||
}
|
||||
|
||||
fun updateLocation(location: org.maplibre.compose.location.Location?) {
|
||||
if (location != null) {
|
||||
if (routeModel.isNavigating()) {
|
||||
instruction.value = routeModel.currentStep()
|
||||
}
|
||||
val zoom = NavigationUtils().calculateZoom(location.speed)
|
||||
cameraPosition.postValue(
|
||||
cameraPosition.value!!.copy(
|
||||
zoom = zoom,
|
||||
target = location.position
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun test() {
|
||||
for (i in 0..<routeModel.polylineLocations.size) {
|
||||
val loc = routeModel.polylineLocations[i]
|
||||
val curLocation = Location(LocationManager.GPS_PROVIDER)
|
||||
curLocation.longitude = loc[0]
|
||||
curLocation.latitude = loc[1]
|
||||
routeModel.updateLocation(curLocation)
|
||||
val leftTime = routeModel.travelLeftTime()
|
||||
val leftDistance = routeModel.travelLeftDistance()
|
||||
Log.i(TAG, " leftTime: ${leftTime / 60}")
|
||||
Log.i(TAG, " leftDistance: $leftDistance")
|
||||
Log.i(TAG, "Cue: ${routeModel.maneuvers[routeModel.maneuverIndex]}")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
@@ -198,10 +157,10 @@ class MainActivity : ComponentActivity() {
|
||||
scope.launch {
|
||||
snackbarHostState.showSnackbar("Starte Navigation")
|
||||
}
|
||||
if (!routeModel.isNavigating()) {
|
||||
if (!routeModel.isNavigating() && lastLocation.latitude != 0.0) {
|
||||
tilt = 60.0
|
||||
vieModel.loadRoute(
|
||||
curLocation,
|
||||
lastLocation,
|
||||
Constants.home2Location
|
||||
)
|
||||
} else {
|
||||
@@ -271,8 +230,8 @@ class MainActivity : ComponentActivity() {
|
||||
Card {
|
||||
Column {
|
||||
Icon(
|
||||
painter = painterResource(com.kouros.android.cars.carappservice.R.drawable.ic_turn_normal_right),
|
||||
contentDescription = stringResource(id = com.kouros.android.cars.carappservice.R.string.accept_action_title)
|
||||
painter = painterResource(R.drawable.ic_turn_normal_right),
|
||||
contentDescription = stringResource(id = R.string.accept_action_title)
|
||||
)
|
||||
if (step != null) {
|
||||
Text(text = step.bearing.toString(), fontSize = 25.sp)
|
||||
@@ -295,10 +254,23 @@ class MainActivity : ComponentActivity() {
|
||||
|
||||
@Composable
|
||||
fun MapView() {
|
||||
val locationProvider = rememberDefaultLocationProvider()
|
||||
val locationState = rememberUserLocationState(locationProvider)
|
||||
updateLocation(locationState.location)
|
||||
val locationProvider = rememberDefaultLocationProvider(
|
||||
updateInterval = 0.1.seconds,
|
||||
desiredAccuracy = DesiredAccuracy.Highest
|
||||
)
|
||||
val userLocationState = rememberUserLocationState(locationProvider)
|
||||
val locationState = locationProvider.location.collectAsState()
|
||||
if (!test) {
|
||||
updateLocation(locationState.value)
|
||||
} else {
|
||||
test()
|
||||
}
|
||||
if (locationState.value != null && lastLocation.latitude == 0.0) {
|
||||
lastLocation.latitude = locationState.value?.position!!.latitude
|
||||
lastLocation.longitude = locationState.value?.position!!.longitude
|
||||
}
|
||||
val position: CameraPosition? by cameraPosition.observeAsState()
|
||||
|
||||
val route: String? by routeData.observeAsState()
|
||||
val cameraState =
|
||||
rememberCameraState(
|
||||
@@ -311,42 +283,32 @@ class MainActivity : ComponentActivity() {
|
||||
zoom = 15.0,
|
||||
)
|
||||
)
|
||||
|
||||
if (locationState.location != null) {
|
||||
curLocation.latitude = locationState.location?.position!!.latitude
|
||||
curLocation.longitude = locationState.location?.position!!.longitude
|
||||
}
|
||||
MaplibreMap(
|
||||
cameraState = cameraState,
|
||||
//baseStyle = BaseStyle.Uri("https://tiles.openfreemap.org/styles/liberty"),
|
||||
baseStyle = BaseStyle.Uri("https://kouros-online.de/liberty"),
|
||||
options =
|
||||
MapOptions(
|
||||
gestureOptions = GestureOptions(
|
||||
isTiltEnabled = true,
|
||||
isZoomEnabled = true,
|
||||
isRotateEnabled = false,
|
||||
isScrollEnabled = true,
|
||||
),
|
||||
ornamentOptions = OrnamentOptions(
|
||||
isScaleBarEnabled = false
|
||||
)
|
||||
)
|
||||
baseStyle = BaseStyle.Uri(Constants.STYLE),
|
||||
) {
|
||||
LocationPuck(
|
||||
idPrefix = "user-location",
|
||||
locationState = locationState,
|
||||
cameraState = cameraState,
|
||||
accuracyThreshold = 10f,
|
||||
colors = LocationPuckColors(accuracyStrokeColor = Color.Green)
|
||||
)
|
||||
getBaseSource(id = "openmaptiles")?.let { tiles ->
|
||||
FillLayer(id = "example", visible = false, source = tiles, sourceLayer = "building")
|
||||
RouteLayer(route)
|
||||
}
|
||||
LocationPuck(
|
||||
idPrefix = "user-location1",
|
||||
locationState = userLocationState,
|
||||
cameraState = cameraState,
|
||||
accuracyThreshold = 10f,
|
||||
showBearing = false,
|
||||
sizes = LocationPuckSizes(dotRadius = 10.dp),
|
||||
colors = LocationPuckColors(
|
||||
dotFillColorCurrentLocation = Color.Cyan,
|
||||
accuracyStrokeColor = Color.Green
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
LaunchedEffect(position) {
|
||||
LocationTrackingEffect(
|
||||
locationState = userLocationState,
|
||||
) {
|
||||
//cameraState.updateFromLocation()
|
||||
cameraState.animateTo(
|
||||
finalPosition = CameraPosition(
|
||||
bearing = position!!.bearing,
|
||||
@@ -354,9 +316,22 @@ class MainActivity : ComponentActivity() {
|
||||
target = position!!.target,
|
||||
tilt = tilt
|
||||
),
|
||||
duration = 3.seconds
|
||||
duration = 1.seconds
|
||||
)
|
||||
}
|
||||
|
||||
// LaunchedEffect(position) {
|
||||
// println("CameraPosition ${position!!.target.latitude}")
|
||||
// cameraState.animateTo(
|
||||
// finalPosition = CameraPosition(
|
||||
// bearing = position!!.bearing,
|
||||
// zoom = position!!.zoom,
|
||||
// target = position!!.target,
|
||||
// tilt = tilt
|
||||
// ),
|
||||
// duration = 3.seconds
|
||||
// )
|
||||
// }
|
||||
}
|
||||
|
||||
@Composable
|
||||
@@ -368,17 +343,66 @@ class MainActivity : ComponentActivity() {
|
||||
id = "routes-casing",
|
||||
source = routes,
|
||||
color = const(Color.White),
|
||||
width = const(6.dp),
|
||||
width = const(10.dp),
|
||||
)
|
||||
LineLayer(
|
||||
id = "routes",
|
||||
source = routes,
|
||||
color = const(Color.Blue),
|
||||
width = const(4.dp),
|
||||
width = const(8.dp),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
fun updateLocation(location: org.maplibre.compose.location.Location?) {
|
||||
if (location != null) {
|
||||
if (routeModel.isNavigating()) {
|
||||
routeModel.updateLocation(lastLocation)
|
||||
instruction.value = routeModel.currentStep()
|
||||
}
|
||||
val zoom = calculateZoom(location.speed)
|
||||
cameraPosition.postValue(
|
||||
cameraPosition.value!!.copy(
|
||||
zoom = zoom,
|
||||
target = location.position
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun updateTestLocation(location: Location) {
|
||||
var snapedLocation = location
|
||||
var bearing: Double
|
||||
if (routeModel.isNavigating()) {
|
||||
snapedLocation = snapLocation(location, routeModel.maneuverLocations())
|
||||
bearing = routeModel.currentStep().bearing
|
||||
routeModel.updateLocation(snapedLocation)
|
||||
instruction.value = routeModel.currentStep()
|
||||
} else {
|
||||
bearing = cameraPosition.value!!.bearing
|
||||
}
|
||||
val zoom = calculateZoom(snapedLocation.speed.toDouble())
|
||||
cameraPosition.postValue(
|
||||
cameraPosition.value!!.copy(
|
||||
bearing = bearing,
|
||||
zoom = zoom,
|
||||
target = Position(snapedLocation.longitude, snapedLocation.latitude)
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
fun test() {
|
||||
if (routeModel.isNavigating() && locationIndex < routeModel.route.waypoints.size) {
|
||||
val loc = routeModel.route.waypoints[locationIndex]
|
||||
lastLocation.longitude = loc[0]
|
||||
lastLocation.latitude = loc[1]
|
||||
updateTestLocation(lastLocation)
|
||||
Thread.sleep(1_000)
|
||||
locationIndex++
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun PlaceList(viewModel: ViewModel = koinViewModel()) {
|
||||
var categories: List<Category>
|
||||
|
||||
Reference in New Issue
Block a user