RouteCalculator

This commit is contained in:
Dimitris
2026-05-06 19:49:43 +02:00
parent 25ba7d7bf6
commit 8b612d5b80
9 changed files with 451 additions and 41 deletions
@@ -21,7 +21,7 @@ const val tomtomTrafficUrl = "https://api.tomtom.com/traffic/services/5/incident
private const val tomtomFields =
"{incidents{type,geometry{type,coordinates},properties{iconCategory,events{description}}}}"
val useLocal = BuildConfig.DEBUG
val useLocal = BuildConfig.DEBUG
val useLocalTraffic = BuildConfig.DEBUG
@@ -64,10 +64,10 @@ class TomTomRoute {
// calculate left step distance for each point
var leftStepDistance = stepDistance
leftDistance.add(leftStepDistance.toFloat())
points.subList(
val subPoints = points.subList(
lastPointIndex,
instruction.pointIndex + 1,
).forEach {
instruction.pointIndex + 1)
subPoints.forEach {
if (lastLocation.latitude != 0.0) {
val curLocation = location(it[0], it[1])
val dist = curLocation.distanceTo(lastLocation).absoluteValue
@@ -80,10 +80,7 @@ class TomTomRoute {
bearingBefore = 0,
bearingAfter = 0,
type = convertType(instruction.maneuver),
waypoints = points.subList(
lastPointIndex,
instruction.pointIndex + 1,
).map { location(it[0], it[1]) },
waypoints = subPoints.map { location(it[0], it[1]) },
exit = exitNumber(instruction),
location = location(
instruction.point.longitude, instruction.point.latitude
@@ -8,6 +8,7 @@ import com.kouros.navigation.data.Constants.MAXIMUM_LOCATION_DISTANCE
import com.kouros.navigation.data.Constants.NEAREST_LOCATION_DISTANCE
import com.kouros.navigation.data.Constants.SPEED_UPDATE_DISTANCE
import com.kouros.navigation.data.Constants.TAG
import com.kouros.navigation.utils.bearingPositive
import com.kouros.navigation.utils.location
import java.util.concurrent.TimeUnit
import kotlin.math.absoluteValue
@@ -20,28 +21,42 @@ class RouteCalculator(var routeModel: RouteModel) {
fun findStep(location: Location) {
var nearestDistance = MAXIMUM_LOCATION_DISTANCE
var count = 0
var lastDistance = 0F
var increaseDistance = 0
for ((index, step) in routeModel.curLeg.steps.withIndex()) {
count++
var distance = 0F
if (index >= routeModel.navState.route.currentStepIndex) {
for ((wayIndex, waypoint) in step.maneuver.waypoints.withIndex()) {
count++
if (wayIndex >= step.waypointIndex) {
distance = location.distanceTo(waypoint)
if (distance < nearestDistance) {
if (distance < nearestDistance ) {
nearestDistance = distance
routeModel.navState.route.currentStepIndex = step.index
step.waypointIndex = wayIndex
step.wayPointLocation = waypoint
}
}
if (nearestDistance < NEAREST_LOCATION_DISTANCE && distance > NEAREST_LOCATION_DISTANCE * 5) {
if (stopSearch(nearestDistance, distance) || increaseDistance > 20) {
break
}
if (distance > lastDistance) {
increaseDistance++
}
lastDistance = distance
}
}
if (nearestDistance < NEAREST_LOCATION_DISTANCE && distance > NEAREST_LOCATION_DISTANCE * 5) {
if (stopSearch(nearestDistance, distance) || increaseDistance > 20) {
break
}
}
Log.d(TAG, "FindStep $nearestDistance $count $increaseDistance")
}
private fun stopSearch(nearestDistance: Float, distance: Float): Boolean {
return nearestDistance < NEAREST_LOCATION_DISTANCE && distance > NEAREST_LOCATION_DISTANCE * 10
}
fun travelLeftTime(): Double {