LocationPuck

This commit is contained in:
Dimitris
2025-11-15 12:38:40 +01:00
parent d63747e811
commit 1773ec2244
15 changed files with 568 additions and 213 deletions

View File

@@ -21,7 +21,6 @@ import android.location.LocationManager
import android.net.Uri
import io.objectbox.annotation.Entity
import io.objectbox.annotation.Id
import io.objectbox.annotation.Index
import kotlinx.serialization.Serializable
data class Category(
@@ -52,6 +51,12 @@ data class ContactData(
val avatar: Uri?
)
data class StepData (
var instruction: String,
var leftDistance: Double,
var bearing: Double
)
//val places = mutableListOf<Place>()
/* Place(

View File

@@ -33,7 +33,9 @@ class Contacts(private var context: Context) {
while (moveToNext()) {
val contactId = getLong(getColumnIndex(ContactsContract.Data.CONTACT_ID))
val name = getString(getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
if (name.contains("Jola") || name.contains("Dominic")
if (name.contains("Jola")
|| name.contains("Dominic")
|| name.contains("Martha")
|| name.contains("Μεντή")
|| name.contains("David")) {
val mimeType: String = getString(getColumnIndex(ContactsContract.Data.MIMETYPE))

View File

@@ -3,6 +3,7 @@ package com.kouros.navigation.model
import android.location.Location
import android.location.LocationManager
import com.kouros.navigation.data.Place
import com.kouros.navigation.data.StepData
import com.kouros.navigation.utils.NavigationUtils.Utils.createGeoJson
import com.kouros.navigation.utils.NavigationUtils.Utils.decodePolyline
import org.json.JSONArray
@@ -10,23 +11,19 @@ import org.json.JSONObject
import kotlin.math.roundToInt
open class RouteModel {
// Source - https://stackoverflow.com/a
// Posted by Dmitrii Bychkov
// Retrieved 2025-11-14, License - CC BY-SA 4.0
open class RouteModel () {
var polylineLocations: List<List<Double>> = emptyList()
lateinit var maneuvers: JSONArray
lateinit var locations: JSONArray
lateinit var summary: JSONObject
lateinit var destination: Place
var navigating = false
var arrived = false
var maneuverIndex = 0
var maneuverType = 0
var currentIndex = 0
var distanceToStepEnd = 0F
@@ -38,13 +35,23 @@ open class RouteModel {
var distanceToRoute = 0F
var geoJson = ""
var route = ""
private fun decodeValhallaRoute(route: String) {
if (route.isEmpty() || route == "[]") {
data class Builder(
var route: String? = null,
var fromLocation: Location? = null,
var toLocation: Location? = null) {
fun route(route: String) = apply { this.route = route }
fun fromLocation(fromLocation: Location) = apply { this.fromLocation = fromLocation }
fun toLocation(toLocation: Location) = apply { this.toLocation = toLocation }
//fun build() = RouteModel(route!!, fromLocation!!, toLocation!!)
}
private fun decodeValhallaRoute(valhallaRoute: String) {
if (valhallaRoute.isEmpty() || valhallaRoute == "[]") {
return;
}
val jObject = JSONObject(route)
val jObject = JSONObject(valhallaRoute)
val trip = jObject.getJSONObject("trip")
locations = trip.getJSONArray("locations")
val legs = trip.getJSONArray("legs")
@@ -54,13 +61,13 @@ open class RouteModel {
polylineLocations = decodePolyline(shape)
}
fun createNavigationRoute(route: String) {
decodeValhallaRoute(route)
fun startNavigation(valhallaRoute: String) {
decodeValhallaRoute(valhallaRoute)
for (i in 0..<maneuvers.length()) {
val maneuver = (maneuvers[i] as JSONObject)
routingManeuvers.add(maneuver)
}
geoJson = createGeoJson(polylineLocations)
route = createGeoJson(polylineLocations)
navigating = true
}
@@ -88,6 +95,31 @@ open class RouteModel {
distanceToRoute = nearestDistance
}
fun currentStep(): StepData {
var bearing = 0
val maneuver = (maneuvers[maneuverIndex] as JSONObject)
var text = ""
if (maneuver.optJSONArray("street_names") != null) {
text = maneuver.getJSONArray("street_names").get(0) as String
}
if (maneuver.has("bearing_after")) {
bearing = maneuver.getInt("bearing_after")
}
val distanceStepLeft = leftStepDistance() * 1000
when (distanceStepLeft) {
in 0.0..100.0 -> {
if (maneuverIndex < maneuvers.length()) {
val maneuver = (maneuvers[maneuverIndex + 1] as JSONObject)
if (maneuver.optJSONArray("street_names") != null) {
text = maneuver.getJSONArray("street_names").get(0) as String
}
}
}
}
return StepData(text, distanceStepLeft, bearing.toDouble())
}
/** Calculates the index in a maneuver. */
private fun calculateCurrentIndex(
@@ -159,8 +191,6 @@ open class RouteModel {
val maneuver = routingManeuvers[maneuverIndex]
var leftDistance = maneuver.getDouble("length")
if (endIndex > 0) {
val percent = 100 * (endIndex - currentIndex) / (endIndex - beginIndex)
//leftDistance = leftDistance * percent / 100
leftDistance = (distanceToStepEnd / 1000).toDouble()
}
return leftDistance
@@ -190,11 +220,11 @@ open class RouteModel {
return arrived
}
fun stopNavigating() {
fun stopNavigation() {
navigating = false
polylineLocations = mutableListOf()
routingManeuvers = mutableListOf()
geoJson = ""
route = ""
maneuverIndex = 0
currentIndex = 0
distanceToStepEnd = 0F

View File

@@ -105,4 +105,20 @@ class NavigationUtils() {
//return LatLng(toDegrees(asin(sinLat)), toDegrees(fromLng + dLng))
}
}
fun calculateZoom(speed: Double?): Double {
if (speed == null) {
return 18.0
}
val zoom = when (speed.toInt()) {
in 0..10 -> 17.0
in 11..20 -> 17.0
in 21..30 -> 17.0
in 31..40 -> 16.0
in 41..50 -> 15.0
in 51..60 -> 14.0
else -> 11
}
return zoom.toDouble()
}
}