Nominatim
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package com.kouros.navigation.data
|
||||
|
||||
import androidx.compose.ui.graphics.Color
|
||||
|
||||
val NavigationColor = Color(0xFF1965D9)
|
||||
|
||||
val RouteColor = Color(0xFF2E75E1)
|
||||
@@ -119,9 +119,10 @@ data class ValhallaLocation (
|
||||
object Constants {
|
||||
|
||||
const val STYLE: String = "https://kouros-online.de/liberty.json"
|
||||
const val STYLE_DARK: String = "https://kouros-online.de/liberty_night.json"
|
||||
//const val STYLE: String = "https://tiles.openfreemap.org/styles/liberty"
|
||||
|
||||
//const val STYLE: String = "https://tiles.openfreemap.org/styles/dark"
|
||||
|
||||
const val TAG: String = "Navigation"
|
||||
|
||||
const val CONTACTS: String = "Contacts"
|
||||
@@ -145,6 +146,10 @@ object Constants {
|
||||
|
||||
const val SHOW_THREED_BUILDING = "Show3D"
|
||||
|
||||
const val NEXT_STEP_THRESHOLD = 100.0
|
||||
|
||||
const val MAXIMAL_ROUTE_DEVIATION = 70.0
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ class NavigationRepository {
|
||||
|
||||
private val routeUrl = "https://kouros-online.de/valhalla/route?json="
|
||||
|
||||
private val nominatimUrl = "https://nominatim.openstreetmap.org/search?q="
|
||||
fun getRoute(currentLocation : Location, location: Location): String {
|
||||
val vLocation = listOf(
|
||||
Locations(lat = currentLocation.latitude, lon = currentLocation.longitude),
|
||||
@@ -45,7 +46,7 @@ class NavigationRepository {
|
||||
language = "de-DE"
|
||||
)
|
||||
val routeLocation = Json.encodeToString(valhallaLocation)
|
||||
return fetchUrl(routeUrl + routeLocation)
|
||||
return fetchUrl(routeUrl + routeLocation, true)
|
||||
}
|
||||
|
||||
fun getRouteDistance(currentLocation : Location, location: Location): Double {
|
||||
@@ -55,9 +56,13 @@ class NavigationRepository {
|
||||
return routeModel.route.distance
|
||||
}
|
||||
|
||||
fun searchPlaces(search : String) : String {
|
||||
return fetchUrl("$nominatimUrl$search&format=jsonv2&addressdetails=true&countrycodes=de", false)
|
||||
}
|
||||
|
||||
fun getPlaces(): List<Place> {
|
||||
val places: MutableList<Place> = ArrayList()
|
||||
val placesStr = fetchUrl(placesUrl)
|
||||
val placesStr = fetchUrl(placesUrl, true)
|
||||
val jArray = JSONArray(placesStr)
|
||||
for (i in 0..<jArray.length()) {
|
||||
val json = jArray.getJSONObject(i)
|
||||
@@ -77,22 +82,25 @@ class NavigationRepository {
|
||||
}
|
||||
|
||||
|
||||
private fun fetchUrl(url: String): String {
|
||||
private fun fetchUrl(url: String, authenticator : Boolean): String {
|
||||
try {
|
||||
Authenticator.setDefault(object : Authenticator() {
|
||||
override fun getPasswordAuthentication(): PasswordAuthentication {
|
||||
return PasswordAuthentication(
|
||||
"kouros",
|
||||
"eo7sbjyWpmjSVFyELgbfrryqJ6ddNeq9".toCharArray()
|
||||
)
|
||||
}
|
||||
})
|
||||
if (authenticator) {
|
||||
Authenticator.setDefault(object : Authenticator() {
|
||||
override fun getPasswordAuthentication(): PasswordAuthentication {
|
||||
return PasswordAuthentication(
|
||||
"kouros",
|
||||
"eo7sbjyWpmjSVFyELgbfrryqJ6ddNeq9".toCharArray()
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
println(url)
|
||||
val httpURLConnection = URL(url).openConnection() as HttpURLConnection
|
||||
httpURLConnection.setRequestProperty(
|
||||
"Accept",
|
||||
"application/json"
|
||||
) // The format of response we want to get from the server
|
||||
httpURLConnection.setRequestProperty("User-Agent", "email=nominatim@kouros-online.de");
|
||||
httpURLConnection.requestMethod = "GET"
|
||||
val responseCode = httpURLConnection.responseCode
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
@@ -101,7 +109,7 @@ class NavigationRepository {
|
||||
return response
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
println(e.message)
|
||||
println("Exception ${e.message}")
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ data class Route (
|
||||
|
||||
var routeGeoJson : String,
|
||||
|
||||
var currentIndex: Int
|
||||
var currentManeuverIndex: Int
|
||||
|
||||
) {
|
||||
|
||||
@@ -103,10 +103,10 @@ data class Route (
|
||||
}
|
||||
|
||||
fun currentManeuver() : Maneuvers {
|
||||
return maneuvers[currentIndex]
|
||||
return maneuvers[currentManeuverIndex]
|
||||
}
|
||||
|
||||
fun nextManeuver() : Maneuvers {
|
||||
return maneuvers[currentIndex+1]
|
||||
return maneuvers[currentManeuverIndex+1]
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.kouros.navigation.data.nominatim
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
@JsonIgnoreUnknownKeys
|
||||
data class Address(
|
||||
@SerializedName("city") var city: String = "",
|
||||
@SerializedName("country") var country: String = "",
|
||||
@SerializedName("country_code") var countryCode: String = "",
|
||||
@SerializedName("neighbourhood") var neighbourhood: String = "",
|
||||
@SerializedName("postcode") var postcode: String = "",
|
||||
@SerializedName("road") var road: String = "",
|
||||
@SerializedName("state") var state: String = "",
|
||||
@SerializedName("suburb") var suburb: String = "",
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.kouros.navigation.data.nominatim
|
||||
|
||||
import com.google.gson.annotations.SerializedName
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
|
||||
|
||||
|
||||
class Search : ArrayList<SearchResult>()
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
@JsonIgnoreUnknownKeys
|
||||
data class SearchResult(
|
||||
|
||||
@SerializedName("place_id") var placeId: Int = 0,
|
||||
@SerializedName("licence") var licence: String = "",
|
||||
@SerializedName("osm_type") var osmType: String = "",
|
||||
@SerializedName("osm_id") var osmId: Long = 0,
|
||||
@SerializedName("lat") var lat: String = "",
|
||||
@SerializedName("lon") var lon: String = "",
|
||||
@SerializedName("category") var category: String = "",
|
||||
@SerializedName("type") var type: String = "",
|
||||
@SerializedName("place_rank") var placeRank: Int = 0,
|
||||
@SerializedName("importance") var importance: Double = 0.0,
|
||||
@SerializedName("addresstype") var addresstype: String = "",
|
||||
@SerializedName("address") var address: Address,
|
||||
@SerializedName("name") var name: String = "",
|
||||
@SerializedName("display_name") var displayName: String = "",
|
||||
@SerializedName("boundingbox") var boundingbox: ArrayList<String> = arrayListOf()
|
||||
|
||||
)
|
||||
@@ -2,11 +2,10 @@ package com.kouros.navigation.model
|
||||
|
||||
import android.location.Location
|
||||
import android.location.LocationManager
|
||||
import com.kouros.navigation.data.Constants.homeLocation
|
||||
import com.kouros.navigation.data.Constants.NEXT_STEP_THRESHOLD
|
||||
import com.kouros.navigation.data.Place
|
||||
import com.kouros.navigation.data.Route
|
||||
import com.kouros.navigation.data.StepData
|
||||
import com.kouros.navigation.utils.NavigationUtils
|
||||
import com.kouros.navigation.utils.location
|
||||
import org.maplibre.geojson.FeatureCollection
|
||||
import org.maplibre.geojson.Point
|
||||
@@ -26,9 +25,9 @@ open class RouteModel() {
|
||||
var maneuverType = 0
|
||||
|
||||
/*
|
||||
Index in a maneuver
|
||||
current shapeIndex
|
||||
*/
|
||||
var currentIndex = 0
|
||||
private var currentShapeIndex = 0
|
||||
|
||||
var distanceToStepEnd = 0F
|
||||
|
||||
@@ -64,7 +63,7 @@ open class RouteModel() {
|
||||
|
||||
fun updateLocation(location: Location) {
|
||||
var nearestDistance = 100000.0f
|
||||
route.currentIndex = -1
|
||||
route.currentManeuverIndex = -1
|
||||
// find maneuver
|
||||
for ((i, maneuver) in route.maneuvers.withIndex()) {
|
||||
val beginShapeIndex = maneuver.beginShapeIndex
|
||||
@@ -72,7 +71,7 @@ open class RouteModel() {
|
||||
val distance = calculateDistance(beginShapeIndex, endShapeIndex, location)
|
||||
if (distance < nearestDistance) {
|
||||
nearestDistance = distance
|
||||
route.currentIndex = i
|
||||
route.currentManeuverIndex = i
|
||||
calculateCurrentIndex(beginShapeIndex, endShapeIndex, location)
|
||||
}
|
||||
}
|
||||
@@ -84,13 +83,13 @@ open class RouteModel() {
|
||||
if (maneuver.streetNames != null && maneuver.streetNames.isNotEmpty()) {
|
||||
text = maneuver.streetNames[0]
|
||||
}
|
||||
if (bearing == 0F) {
|
||||
bearing = maneuver.bearingAfter.toFloat()
|
||||
}
|
||||
val curLocation = location(route.pointLocations[currentShapeIndex].latitude(), route.pointLocations[currentShapeIndex].longitude())
|
||||
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..100.0 -> {
|
||||
if (route.currentIndex < route.maneuvers.size) {
|
||||
in 0.0..NEXT_STEP_THRESHOLD -> {
|
||||
if (route.currentManeuverIndex < route.maneuvers.size) {
|
||||
val maneuver = route.nextManeuver()
|
||||
if (maneuver.streetNames != null && maneuver.streetNames.isNotEmpty()) {
|
||||
text = maneuver.streetNames[0]
|
||||
@@ -109,13 +108,13 @@ open class RouteModel() {
|
||||
) {
|
||||
var nearestLocation = 100000.0f
|
||||
for (i in beginShapeIndex..endShapeIndex) {
|
||||
val polylineLocation = Location(LocationManager.GPS_PROVIDER)
|
||||
polylineLocation.longitude = route.waypoints[i][0]
|
||||
polylineLocation.latitude = route.waypoints[i][1]
|
||||
val distance: Float = location.distanceTo(polylineLocation)
|
||||
val waypoint = Location(LocationManager.GPS_PROVIDER)
|
||||
waypoint.longitude = route.waypoints[i][0]
|
||||
waypoint.latitude = route.waypoints[i][1]
|
||||
val distance: Float = location.distanceTo(waypoint)
|
||||
if (distance < nearestLocation) {
|
||||
nearestLocation = distance
|
||||
currentIndex = i
|
||||
currentShapeIndex = i
|
||||
beginIndex = beginShapeIndex
|
||||
endIndex = endShapeIndex
|
||||
distanceToStepEnd = 0F
|
||||
@@ -157,14 +156,14 @@ open class RouteModel() {
|
||||
|
||||
fun travelLeftTime(): Double {
|
||||
var timeLeft = 0.0
|
||||
for (i in route.currentIndex + 1..<route.maneuvers.size) {
|
||||
for (i in route.currentManeuverIndex + 1..<route.maneuvers.size) {
|
||||
val maneuver = route.maneuvers[i]
|
||||
timeLeft += maneuver.time
|
||||
}
|
||||
if (endIndex > 0) {
|
||||
val maneuver = route.currentManeuver()
|
||||
val curTime = maneuver.time
|
||||
val percent = 100 * (endIndex - currentIndex) / (endIndex - beginIndex)
|
||||
val percent = 100 * (endIndex - currentShapeIndex) / (endIndex - beginIndex)
|
||||
val time = curTime * percent / 100
|
||||
timeLeft += time
|
||||
}
|
||||
@@ -183,14 +182,14 @@ open class RouteModel() {
|
||||
|
||||
fun travelLeftDistance(): Double {
|
||||
var leftDistance = 0.0
|
||||
for (i in route.currentIndex + 1..<route.maneuvers.size) {
|
||||
for (i in route.currentManeuverIndex + 1..<route.maneuvers.size) {
|
||||
val maneuver = route.maneuvers[i]
|
||||
leftDistance += maneuver.length
|
||||
}
|
||||
if (endIndex > 0) {
|
||||
val maneuver = route.currentManeuver()
|
||||
val curDistance = maneuver.length
|
||||
val percent = 100 * (endIndex - currentIndex) / (endIndex - beginIndex)
|
||||
val percent = 100 * (endIndex - currentShapeIndex) / (endIndex - beginIndex)
|
||||
val time = curDistance * percent / 100
|
||||
leftDistance += time
|
||||
}
|
||||
@@ -208,10 +207,9 @@ open class RouteModel() {
|
||||
fun stopNavigation() {
|
||||
route.clear()
|
||||
navigating = false
|
||||
currentIndex = 0
|
||||
currentShapeIndex = 0
|
||||
distanceToStepEnd = 0F
|
||||
beginIndex = 0
|
||||
endIndex = 0
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,16 @@ package com.kouros.navigation.model
|
||||
import android.content.Context
|
||||
import android.location.Geocoder
|
||||
import android.location.Location
|
||||
import android.location.LocationManager
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.google.gson.GsonBuilder
|
||||
import com.kouros.navigation.data.Constants
|
||||
import com.kouros.navigation.data.NavigationRepository
|
||||
import com.kouros.navigation.data.ObjectBox.boxStore
|
||||
import com.kouros.navigation.data.Place
|
||||
import com.kouros.navigation.data.Place_
|
||||
import com.kouros.navigation.utils.NavigationUtils
|
||||
import com.kouros.navigation.data.nominatim.Search
|
||||
import com.kouros.navigation.utils.location
|
||||
import io.objectbox.kotlin.boxFor
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -32,6 +32,10 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
MutableLiveData<List<Place>>()
|
||||
}
|
||||
|
||||
val searchPlaces: MutableLiveData<Search> by lazy {
|
||||
MutableLiveData<Search>()
|
||||
}
|
||||
|
||||
val contactAddress: MutableLiveData<List<Place>> by lazy {
|
||||
MutableLiveData<List<Place>>()
|
||||
}
|
||||
@@ -85,11 +89,13 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
for (address in addresses) {
|
||||
val addressLines = address.address.split("\n")
|
||||
geocoder.getFromLocationName(
|
||||
address.address, 5) {
|
||||
address.address, 5
|
||||
) {
|
||||
for (adr in it) {
|
||||
if (addressLines.size > 1) {
|
||||
val plLocation = location(adr.latitude, adr.longitude)
|
||||
val distance = repository.getRouteDistance(currentLocation, plLocation)
|
||||
val distance =
|
||||
repository.getRouteDistance(currentLocation, plLocation)
|
||||
contactList.add(
|
||||
Place(
|
||||
id = address.contactId,
|
||||
@@ -115,6 +121,15 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
fun searchPlaces(search: String) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val placesJson = repository.searchPlaces(search)
|
||||
val gson = GsonBuilder().serializeNulls().create()
|
||||
val places = gson.fromJson(placesJson, Search::class.java)
|
||||
searchPlaces.postValue(places)
|
||||
}
|
||||
}
|
||||
|
||||
fun saveRecent(place: Place) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
place.category = Constants.RECENT
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.location.Location
|
||||
import android.location.LocationManager
|
||||
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
|
||||
@@ -57,11 +58,17 @@ object NavigationUtils {
|
||||
}
|
||||
fun snapLocation(location: Location, stepCoordinates: List<Point>): Location {
|
||||
val oldPoint = Point.fromLngLat(location.longitude, location.latitude)
|
||||
val oldLocation = location(location.latitude, location.longitude)
|
||||
if (stepCoordinates.size > 1) {
|
||||
val pointFeature = TurfMisc.nearestPointOnLine(oldPoint, stepCoordinates)
|
||||
val point = pointFeature.geometry() as Point
|
||||
location.latitude = point.latitude()
|
||||
location.longitude = point.longitude()
|
||||
val distance = oldLocation.distanceTo(location)
|
||||
if (distance > MAXIMAL_ROUTE_DEVIATION) {
|
||||
println("Distance to big")
|
||||
return location(0.0, 0.0)
|
||||
}
|
||||
}
|
||||
return location
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user