Refactoring Route, Speed
This commit is contained in:
@@ -137,7 +137,7 @@ class RouteCarModel() : RouteModel() {
|
||||
|
||||
val dismissAction: Action = createToastAction(
|
||||
carContext,
|
||||
R.string.speed_camera, R.string.exit_action_title,
|
||||
R.string.exit_action_title, R.string.exit_action_title,
|
||||
FLAG_DEFAULT
|
||||
)
|
||||
|
||||
|
||||
@@ -34,7 +34,9 @@ import com.kouros.navigation.data.Place
|
||||
import com.kouros.navigation.data.nominatim.SearchResult
|
||||
import com.kouros.navigation.data.overpass.Elements
|
||||
import com.kouros.navigation.model.ViewModel
|
||||
import com.kouros.navigation.utils.bearing
|
||||
import com.kouros.navigation.utils.location
|
||||
import kotlin.math.absoluteValue
|
||||
|
||||
class NavigationScreen(
|
||||
carContext: CarContext,
|
||||
@@ -488,10 +490,15 @@ class NavigationScreen(
|
||||
it.distance = distance.toDouble()
|
||||
updatedCameras.add(it)
|
||||
}
|
||||
val sortedList = updatedCameras.sortedWith(compareBy { it.distance })
|
||||
val sortedList = updatedCameras.sortedWith(compareBy { it.distance })
|
||||
val camera = sortedList.first()
|
||||
if (camera.distance < 80) {
|
||||
routeModel.showSpeedCamera(carContext, camera.distance, camera.tags.maxspeed)
|
||||
val bearingSpeedCamera = location.bearingTo(location(camera.lon!!, camera.lat!!))
|
||||
val bearingRoute = surfaceRenderer.lastLocation.bearingTo(location)
|
||||
|
||||
if (camera.distance < 80
|
||||
&& (bearingSpeedCamera.absoluteValue - bearingRoute.absoluteValue).absoluteValue < 15.0
|
||||
) {
|
||||
routeModel.showSpeedCamera(carContext, camera.distance, camera.tags.maxspeed)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,3 +179,6 @@ object Constants {
|
||||
}
|
||||
|
||||
|
||||
enum class RouteEngine {
|
||||
VALHALLA, OSRM, GRAPHHOPPER
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package com.kouros.navigation.data
|
||||
|
||||
import android.location.Location
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.kouros.navigation.data.osrm.OsrmResponse
|
||||
import com.kouros.navigation.data.osrm.OsrmRoute
|
||||
import com.kouros.navigation.data.route.Leg
|
||||
import com.kouros.navigation.data.route.Step
|
||||
import com.kouros.navigation.data.route.Summary
|
||||
@@ -28,7 +30,7 @@ data class Route(
|
||||
|
||||
data class Builder (
|
||||
|
||||
var routeEngine : Int = 0,
|
||||
var routeEngine : Int = RouteEngine.VALHALLA.ordinal,
|
||||
var summary: Summary? = null,
|
||||
var legs: List<Leg>? = null,
|
||||
var routeGeoJson: String = "",
|
||||
@@ -46,10 +48,14 @@ data class Route(
|
||||
fun route(route: String) = apply {
|
||||
if (route.isNotEmpty() && route != "[]") {
|
||||
val gson = GsonBuilder().serializeNulls().create()
|
||||
val jsonObject: Map<String, JsonElement> = Json.parseToJsonElement(route).jsonObject
|
||||
val routeJson =
|
||||
gson.fromJson(jsonObject["trip"].toString(), ValhallaResponse::class.java)
|
||||
ValhallaRoute().mapJsonToValhalla(routeJson, this)
|
||||
if (routeEngine == RouteEngine.VALHALLA.ordinal) {
|
||||
val jsonObject: Map<String, JsonElement> = Json.parseToJsonElement(route).jsonObject
|
||||
val routeJson = gson.fromJson(jsonObject["trip"].toString(), ValhallaResponse::class.java)
|
||||
ValhallaRoute().mapJsonToValhalla(routeJson, this)
|
||||
} else {
|
||||
val osrmJson = gson.fromJson(route, OsrmResponse::class.java)
|
||||
OsrmRoute().mapToOsrm(osrmJson, this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package com.kouros.navigation.data.osrm
|
||||
import com.google.gson.annotations.SerializedName
|
||||
|
||||
|
||||
data class OsrmJson (
|
||||
data class OsrmResponse (
|
||||
|
||||
@SerializedName("code" ) var code : String? = null,
|
||||
@SerializedName("routes" ) var routes : ArrayList<Routes> = arrayListOf(),
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.kouros.navigation.data.osrm
|
||||
|
||||
import com.kouros.navigation.data.Route
|
||||
import com.kouros.navigation.data.valhalla.ValhallaResponse
|
||||
|
||||
class OsrmRoute {
|
||||
|
||||
fun mapToOsrm(routeJson: OsrmResponse, builder: Route.Builder) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@ class Overpass {
|
||||
|);
|
||||
|out body;
|
||||
""".trimMargin()
|
||||
println("way[highway](around:$radius,$linestring)")
|
||||
return overpassApi(httpURLConnection, searchQuery)
|
||||
}
|
||||
|
||||
@@ -60,7 +61,6 @@ class Overpass {
|
||||
}
|
||||
|
||||
fun overpassApi(httpURLConnection: HttpURLConnection, searchQuery: String) : List<Elements> {
|
||||
// Send the JSON we created
|
||||
val outputStreamWriter = OutputStreamWriter(httpURLConnection.outputStream)
|
||||
outputStreamWriter.write(searchQuery)
|
||||
outputStreamWriter.flush()
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.kouros.navigation.data.valhalla
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
|
||||
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
@JsonIgnoreUnknownKeys
|
||||
data class Trip (
|
||||
|
||||
@SerializedName("locations" ) var locations : ArrayList<Locations> = arrayListOf(),
|
||||
@SerializedName("legs" ) var legs : ArrayList<Legs> = arrayListOf(),
|
||||
@SerializedName("summary" ) var summaryValhalla : SummaryValhalla = SummaryValhalla(),
|
||||
@SerializedName("status_message" ) var statusMessage : String = "",
|
||||
@SerializedName("status" ) var status : Int = 0,
|
||||
@SerializedName("units" ) var units : String = "",
|
||||
@SerializedName("language" ) var language : String = "",
|
||||
|
||||
)
|
||||
@@ -1,16 +0,0 @@
|
||||
package com.kouros.navigation.data.valhalla
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
|
||||
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
@JsonIgnoreUnknownKeys
|
||||
data class ValhallaJson (
|
||||
|
||||
@SerializedName("trip" ) var trip : Trip = Trip(),
|
||||
@SerializedName("id" ) var id : String = ""
|
||||
|
||||
)
|
||||
@@ -26,7 +26,7 @@ open class RouteModel() {
|
||||
val arrived: Boolean = false,
|
||||
val maneuverType: Int = 0,
|
||||
val travelMessage: String = "",
|
||||
// max speed for street (maneuver)
|
||||
val lastSpeedLocation: Location = location(0.0, 0.0),
|
||||
val lastSpeedIndex: Int = 0,
|
||||
val maxSpeed: Int = 0,
|
||||
)
|
||||
@@ -89,9 +89,11 @@ open class RouteModel() {
|
||||
}
|
||||
|
||||
fun updateSpeedLimit(location: Location, viewModel: ViewModel) {
|
||||
// speed limit for each maneuver index
|
||||
if (routeState.lastSpeedIndex < route.currentStep) {
|
||||
// speed limit
|
||||
val distance = routeState.lastSpeedLocation.distanceTo(location)
|
||||
if (distance > 500 || routeState.lastSpeedIndex < route.currentStep) {
|
||||
routeState = routeState.copy(lastSpeedIndex = route.currentStep)
|
||||
routeState = routeState.copy(lastSpeedLocation = location)
|
||||
val elements = viewModel.getMaxSpeed(location)
|
||||
elements.forEach {
|
||||
if (it.tags.name != null && it.tags.maxspeed != null) {
|
||||
|
||||
Reference in New Issue
Block a user