Automotive

This commit is contained in:
Dimitris
2025-12-05 12:46:26 +01:00
parent 9f53db8e76
commit d7f5de0d3c
95 changed files with 909 additions and 685 deletions

View File

@@ -2,7 +2,7 @@ package com.kouros.navigation.data
import androidx.compose.ui.graphics.Color
val NavigationColor = Color(0xFF052086)
val NavigationColor = Color(0xFF368605)
val RouteColor = Color(0xFF5582D0)

View File

@@ -146,6 +146,8 @@ object Constants {
const val RECENT: String = "Recent"
const val FAVORITES: String = "Favorites"
/** The initial location to use as an anchor for searches. */
val homeLocation: Location = Location(LocationManager.GPS_PROVIDER)
val home2Location: Location = Location(LocationManager.GPS_PROVIDER)

View File

@@ -18,6 +18,7 @@ package com.kouros.navigation.data
import android.location.Location
import com.kouros.navigation.model.RouteModel
import com.kouros.navigation.utils.NavigationUtils.getBoundingBox
import org.json.JSONArray
import java.net.Authenticator
import java.net.HttpURLConnection
@@ -62,8 +63,14 @@ class NavigationRepository {
return routeModel.route.distance
}
fun searchPlaces(search : String) : String {
return fetchUrl("${nominatimUrl}search?q=$search&format=jsonv2&addressdetails=true&countrycodes=de", false)
fun searchPlaces(search: String, location: Location) : String {
// val bbox = getBoundingBox(location.longitude, location.latitude, 10.0)
// val neLon = bbox["ne"]?.get("lon")
// val neLat = bbox["ne"]?.get("lat")
// val swLon = bbox["sw"]?.get("lon")
// val swLat = bbox["sw"]?.get("lat")
// val viewbox = "&viewbox=$swLon,$swLat,$neLon,$neLat"
return fetchUrl("${nominatimUrl}search?q=$search&format=jsonv2&addressdetails=true,&countrycodes=de", false)
}
fun reverseAddress(location: Location) : String {

View File

@@ -97,7 +97,8 @@ data class Route (
} else {
currentManeuver().endShapeIndex + 1
}
return pointLocations.subList(beginShapeIndex, endShapeIndex)
//return pointLocations.subList(beginShapeIndex, endShapeIndex)
return pointLocations
}
fun clear() {

View File

@@ -10,8 +10,6 @@ import com.kouros.navigation.utils.location
import org.maplibre.geojson.FeatureCollection
import org.maplibre.geojson.Point
import org.maplibre.turf.TurfMeasurement
import org.maplibre.turf.TurfMisc
import kotlin.math.absoluteValue
import kotlin.math.roundToInt
open class RouteModel() {
@@ -28,7 +26,7 @@ open class RouteModel() {
/*
current shapeIndex
*/
private var currentShapeIndex = 0
var currentShapeIndex = 0
var distanceToStepEnd = 0F
@@ -53,7 +51,7 @@ open class RouteModel() {
val future = TurfMeasurement.center(FeatureCollection.fromJson(route.routeGeoJson))
val point = future.geometry() as Point
return location(point.latitude(), point.longitude())
return location(point.longitude(), point.latitude())
}
val currentDistance: Double
@@ -85,16 +83,16 @@ open class RouteModel() {
text = maneuver.streetNames[0]
}
val curLocation = location(
route.pointLocations[currentShapeIndex].latitude(),
route.pointLocations[currentShapeIndex].longitude()
route.pointLocations[currentShapeIndex].longitude(),
route.pointLocations[currentShapeIndex].latitude()
)
if (currentShapeIndex < route.pointLocations.size) {
val nextLocation = location(
route.pointLocations[currentShapeIndex + 1].latitude(),
route.pointLocations[currentShapeIndex + 1].longitude()
)
bearing = curLocation.bearingTo(nextLocation).absoluteValue
}
// if (currentShapeIndex < route.pointLocations.size) {
// val nextLocation = location(
// route.pointLocations[currentShapeIndex + 1].latitude(),
// route.pointLocations[currentShapeIndex + 1].longitude()
// )
// bearing = curLocation.bearingTo(nextLocation)
// }
val distanceStepLeft = leftStepDistance() * 1000
when (distanceStepLeft) {
in 0.0..NEXT_STEP_THRESHOLD -> {

View File

@@ -40,6 +40,10 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
MutableLiveData<List<Place>>()
}
val favorites: MutableLiveData<List<Place>> by lazy {
MutableLiveData<List<Place>>()
}
val searchPlaces: MutableLiveData<List<SearchResult>> by lazy {
MutableLiveData<List<SearchResult>>()
}
@@ -60,7 +64,7 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
val results = query.find()
query.close()
for (place in results) {
val plLocation = location(place.latitude, place.longitude)
val plLocation = location(place.longitude,place.latitude)
// val distance = repository.getRouteDistance(location, plLocation)
//place.distance = distance.toFloat()
if (place.distance == 0F) {
@@ -78,13 +82,13 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
try {
val placeBox = boxStore.boxFor(Place::class)
val query = placeBox
.query(Place_.name.notEqual(""))
.query(Place_.name.notEqual("").and(Place_.category.equal(Constants.RECENT)))
.orderDesc(Place_.lastDate)
.build()
val results = query.find()
query.close()
for (place in results) {
val plLocation = location(place.latitude, place.longitude)
val plLocation = location(place.longitude, place.latitude)
val distance = repository.getRouteDistance(location, plLocation, getSearchFilter(context))
place.distance = distance.toFloat()
}
@@ -95,6 +99,28 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
}
}
fun loadFavorites(context: Context, location: Location) {
viewModelScope.launch(Dispatchers.IO) {
try {
val placeBox = boxStore.boxFor(Place::class)
val query = placeBox
.query(Place_.name.notEqual("").and(Place_.category.equal(Constants.FAVORITES)))
.orderDesc(Place_.lastDate)
.build()
val results = query.find()
query.close()
for (place in results) {
val plLocation = location(place.longitude, place.latitude )
val distance = repository.getRouteDistance(location, plLocation, getSearchFilter(context))
place.distance = distance.toFloat()
}
favorites.postValue(results)
} catch (e: Exception) {
e.printStackTrace()
}
}
}
fun loadRoute(context: Context, currentLocation: Location, location: Location) {
viewModelScope.launch(Dispatchers.IO) {
try {
@@ -129,7 +155,7 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
) {
for (adr in it) {
if (addressLines.size > 1) {
val plLocation = location(adr.latitude, adr.longitude)
val plLocation = location( adr.longitude, adr.latitude)
val distance =
repository.getRouteDistance(currentLocation, plLocation, getSearchFilter(context))
contactList.add(
@@ -159,13 +185,13 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
fun searchPlaces(search: String, location: Location) {
viewModelScope.launch(Dispatchers.IO) {
val placesJson = repository.searchPlaces(search)
val placesJson = repository.searchPlaces(search, location)
val gson = GsonBuilder().serializeNulls().create()
val places = gson.fromJson(placesJson, Search::class.java)
val distPlaces = mutableListOf<SearchResult>()
places.forEach {
val plLocation =
location(latitude = it.lat.toDouble(), longitude = it.lon.toDouble())
location(longitude = it.lon.toDouble(), latitude = it.lat.toDouble())
val distance = plLocation.distanceTo(location)
it.distance = distance
distPlaces.add(it)
@@ -183,13 +209,20 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
return place.address.road
}
fun saveFavorite(place: Place) {
place.category = Constants.FAVORITES
savePlace(place)
}
fun saveRecent(place: Place) {
place.category = Constants.RECENT
savePlace(place)
}
fun savePlace(place: Place) {
viewModelScope.launch(Dispatchers.IO) {
place.category = Constants.RECENT
try {
val placeBox = boxStore.boxFor(Place::class)
val query = placeBox
.query(Place_.name.equal(place.name!!))
.query(Place_.name.equal(place.name!!).and(Place_.category.equal(place.category!!)))
.build()
val results = query.find()
query.close()
@@ -205,13 +238,21 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
}
}
fun deleteFavorite(place: Place) {
place.category = Constants.FAVORITES
deletePlace(place)
}
fun deleteRecent(place: Place) {
place.category = Constants.RECENT
deletePlace(place)
}
fun deletePlace(place: Place) {
viewModelScope.launch(Dispatchers.IO) {
place.category = Constants.RECENT
try {
val placeBox = boxStore.boxFor(Place::class)
val query = placeBox
.query(Place_.name.equal(place.name!!))
.query(Place_.name.equal(place.name!!).and(Place_.category.equal(place.category!!)))
.build()
val results = query.find()
query.close()

View File

@@ -3,27 +3,15 @@ package com.kouros.navigation.utils
import android.content.Context
import android.location.Location
import android.location.LocationManager
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.content.edit
import com.kouros.navigation.data.Constants.MAXIMAL_ROUTE_DEVIATION
import com.kouros.navigation.data.Constants.SHARED_PREF_KEY
import com.kouros.navigation.data.Constants.SHOW_THREED_BUILDING
import com.kouros.navigation.data.GeoJsonFeature
import com.kouros.navigation.data.GeoJsonFeatureCollection
import com.kouros.navigation.data.GeoJsonLineString
import kotlinx.serialization.json.Json
import org.maplibre.geojson.Feature
import org.maplibre.geojson.FeatureCollection
import org.maplibre.geojson.LineString
import org.maplibre.geojson.Point
import org.maplibre.turf.TurfClassification
import org.maplibre.turf.TurfConversion
import org.maplibre.turf.TurfJoins
import org.maplibre.turf.TurfMeasurement
import org.maplibre.turf.TurfMeta
import org.maplibre.turf.TurfMisc
import org.maplibre.turf.TurfTransformation
import org.maplibre.turf.TurfMeasurement
import java.lang.Math.toDegrees
import java.lang.Math.toRadians
import kotlin.math.asin
@@ -45,7 +33,6 @@ object NavigationUtils {
.getBoolean(key, false)
}
@RequiresApi(Build.VERSION_CODES.GINGERBREAD)
fun setBooleanKeyValue(context: Context, `val`: Boolean, key: String) {
context
.getSharedPreferences(
@@ -60,18 +47,13 @@ object NavigationUtils {
}
}
fun snapLocation(location: Location, stepCoordinates: List<Point>) : Location {
val newLocation = location(latitude = location.latitude, longitude = location.longitude)
val newLocation = Location(location)
val oldPoint = Point.fromLngLat(location.longitude, location.latitude)
if (stepCoordinates.size > 1) {
val pointFeature = TurfMisc.nearestPointOnLine(oldPoint, stepCoordinates)
val point = pointFeature.geometry() as Point
newLocation.latitude = point.latitude()
newLocation.longitude = point.longitude()
newLocation.speed = location.speed
newLocation.bearing = location.bearing
newLocation.time = location.time
newLocation.accuracy = location.accuracy
newLocation.altitude = location.altitude
}
return newLocation
}
@@ -129,10 +111,10 @@ object NavigationUtils {
radius: Double
): Map<String, Map<String, Double>> {
val earthRadius = 6371.0
val maxLat = lat + Math.toDegrees(radius / earthRadius)
val minLat = lat - Math.toDegrees(radius / earthRadius)
val maxLon = lon + Math.toDegrees(radius / earthRadius / cos(Math.toRadians(lat)))
val minLon = lon - Math.toDegrees(radius / earthRadius / cos(Math.toRadians(lat)))
val maxLat = lat + toDegrees(radius / earthRadius)
val minLat = lat - toDegrees(radius / earthRadius)
val maxLon = lon + toDegrees(radius / earthRadius / cos(toRadians(lat)))
val minLon = lon - toDegrees(radius / earthRadius / cos(toRadians(lat)))
return mapOf(
"nw" to mapOf("lat" to maxLat, "lon" to minLon),
@@ -182,8 +164,12 @@ fun calculateZoom(speed: Double?): Double {
}
return zoom.toDouble()
}
fun bearing(fromLocation: Location, toLocation: Location ) : Double {
val bearing = fromLocation.bearingTo(toLocation).toInt().toDouble()
return bearing
}
fun location(latitude: Double, longitude: Double): Location {
fun location(longitude : Double, latitude: Double): Location {
val location = Location(LocationManager.GPS_PROVIDER)
location.longitude = longitude
location.latitude = latitude