MapView, Navigation to RecentPlace
This commit is contained in:
@@ -15,6 +15,7 @@ android {
|
||||
compileSdk = 36
|
||||
|
||||
defaultConfig {
|
||||
minSdk = 33
|
||||
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
|
||||
consumerProguardFiles("consumer-rules.pro")
|
||||
}
|
||||
|
||||
@@ -5,3 +5,5 @@ import androidx.compose.ui.graphics.Color
|
||||
val NavigationColor = Color(0xFF052086)
|
||||
|
||||
val RouteColor = Color(0xFF5582D0)
|
||||
|
||||
val SpeedColor = Color(0xFF262525)
|
||||
@@ -32,7 +32,8 @@ class NavigationRepository {
|
||||
|
||||
private val routeUrl = "https://kouros-online.de/valhalla/route?json="
|
||||
|
||||
private val nominatimUrl = "https://nominatim.openstreetmap.org/search?q="
|
||||
private val nominatimUrl = "https://nominatim.openstreetmap.org/"
|
||||
|
||||
fun getRoute(currentLocation : Location, location: Location): String {
|
||||
val vLocation = listOf(
|
||||
Locations(lat = currentLocation.latitude, lon = currentLocation.longitude),
|
||||
@@ -57,7 +58,11 @@ class NavigationRepository {
|
||||
}
|
||||
|
||||
fun searchPlaces(search : String) : String {
|
||||
return fetchUrl("$nominatimUrl$search&format=jsonv2&addressdetails=true&countrycodes=de", false)
|
||||
return fetchUrl("${nominatimUrl}search?q=$search&format=jsonv2&addressdetails=true&countrycodes=de", false)
|
||||
}
|
||||
|
||||
fun reverseAddress(location: Location) : String {
|
||||
return fetchUrl("${nominatimUrl}reverse?lat=${location.latitude}&lon=${location.longitude}&format=jsonv2&addressdetails=true&countrycodes=de", false)
|
||||
}
|
||||
|
||||
fun getPlaces(): List<Place> {
|
||||
@@ -103,6 +108,7 @@ class NavigationRepository {
|
||||
httpURLConnection.setRequestProperty("User-Agent", "email=nominatim@kouros-online.de");
|
||||
httpURLConnection.requestMethod = "GET"
|
||||
val responseCode = httpURLConnection.responseCode
|
||||
println(responseCode)
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
val response = httpURLConnection.inputStream.bufferedReader()
|
||||
.use { it.readText() } // defaults to UTF-8
|
||||
|
||||
@@ -16,8 +16,8 @@ data class SearchResult(
|
||||
@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("lon") var lon: String = "",
|
||||
@SerializedName("type") var type: String = "",
|
||||
@SerializedName("place_rank") var placeRank: Int = 0,
|
||||
@SerializedName("importance") var importance: Double = 0.0,
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.kouros.navigation.model
|
||||
import android.content.Context
|
||||
import android.location.Geocoder
|
||||
import android.location.Location
|
||||
import android.os.Build
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
@@ -32,6 +34,9 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
MutableLiveData<String>()
|
||||
}
|
||||
|
||||
val recentPlace: MutableLiveData<Place> by lazy {
|
||||
MutableLiveData<Place>()
|
||||
}
|
||||
val places: MutableLiveData<List<Place>> by lazy {
|
||||
MutableLiveData<List<Place>>()
|
||||
}
|
||||
@@ -45,6 +50,31 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
}
|
||||
|
||||
|
||||
fun loadRecentPlace(location: Location) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
val placeBox = boxStore.boxFor(Place::class)
|
||||
val query = placeBox
|
||||
.query(Place_.name.notEqual(""))
|
||||
.orderDesc(Place_.lastDate)
|
||||
.build()
|
||||
val results = query.find()
|
||||
query.close()
|
||||
for (place in results) {
|
||||
val plLocation = location(place.latitude, place.longitude)
|
||||
// val distance = repository.getRouteDistance(location, plLocation)
|
||||
//place.distance = distance.toFloat()
|
||||
if (place.distance == 0F) {
|
||||
recentPlace.postValue(place)
|
||||
println("RecentPlace $recentPlace")
|
||||
return@launch
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
fun loadPlaces(location: Location) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
try {
|
||||
@@ -59,7 +89,6 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
val plLocation = location(place.latitude, place.longitude)
|
||||
val distance = repository.getRouteDistance(location, plLocation)
|
||||
place.distance = distance.toFloat()
|
||||
println(place.lastDate)
|
||||
}
|
||||
places.postValue(results)
|
||||
} catch (e: Exception) {
|
||||
@@ -148,6 +177,15 @@ class ViewModel(private val repository: NavigationRepository) : ViewModel() {
|
||||
}
|
||||
}
|
||||
|
||||
fun reverseAddress(location: Location ): String {
|
||||
val address = repository.reverseAddress(location)
|
||||
println(address)
|
||||
val gson = GsonBuilder().serializeNulls().create()
|
||||
val place = gson.fromJson(address, SearchResult::class.java)
|
||||
println(place.address.road)
|
||||
return place.address.road
|
||||
}
|
||||
|
||||
fun saveRecent(place: Place) {
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
place.category = Constants.RECENT
|
||||
|
||||
Reference in New Issue
Block a user