if location is not available, keep last map position across app restarts

addresses #191
This commit is contained in:
johan12345
2023-06-08 22:36:24 +02:00
parent 265b530936
commit 0d11e450ac
2 changed files with 45 additions and 17 deletions

View File

@@ -1038,9 +1038,12 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
positionSet = true
}
if (!positionSet) {
// center the camera on Europe
// use position saved in preferences, fall back to default (Europe)
val cameraUpdate =
map.cameraUpdateFactory.newLatLngZoom(LatLng(50.113388, 9.252536), 3.5f)
map.cameraUpdateFactory.newLatLngZoom(
prefs.currentMapLocation,
prefs.currentMapZoom
)
map.moveCamera(cameraUpdate)
}
@@ -1378,6 +1381,10 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
override fun onPause() {
super.onPause()
removeLocationUpdates()
vm.mapPosition.value?.let {
prefs.currentMapLocation = it.bounds.center
prefs.currentMapZoom = it.zoom
}
}
override fun onDestroy() {

View File

@@ -1,6 +1,8 @@
package net.vonforst.evmap.storage
import android.content.Context
import android.content.SharedPreferences
import android.content.SharedPreferences.Editor
import androidx.preference.PreferenceManager
import com.car2go.maps.AnyMap
import com.car2go.maps.model.LatLng
@@ -224,21 +226,9 @@ class PreferenceDataSource(val context: Context) {
}
var placeSearchResultAndroidAuto: LatLng?
get() = if (sp.contains("place_search_result_android_auto_lat")) {
LatLng(
Double.fromBits(sp.getLong("place_search_result_android_auto_lat", 0L)),
Double.fromBits(sp.getLong("place_search_result_android_auto_lng", 0L))
)
} else null
get() = sp.getLatLng("place_search_result_android_auto")
set(value) {
if (value == null) {
sp.edit().remove("place_search_result_android_auto_lat")
.remove("place_search_result_android_auto_lng").apply()
} else {
sp.edit().putLong("place_search_result_android_auto_lat", value.latitude.toBits())
.putLong("place_search_result_android_auto_lng", value.longitude.toBits())
.apply()
}
sp.edit().putLatLng("place_search_result_android_auto", value).apply()
}
var placeSearchResultAndroidAutoName: String?
@@ -264,4 +254,35 @@ class PreferenceDataSource(val context: Context) {
val mapScale: String
get() = sp.getString("map_scale", null) ?: "both"
}
var currentMapLocation: LatLng
get() = sp.getLatLng("current_map_location") ?: LatLng(50.113388, 9.252536)
set(value) {
sp.edit().putLatLng("current_map_location", value).apply()
}
var currentMapZoom: Float
get() = sp.getFloat("current_map_zoom", 3.5f)
set(value) {
sp.edit().putFloat("current_map_zoom", value).apply()
}
}
fun SharedPreferences.getLatLng(key: String): LatLng? =
if (contains("${key}_lat") && contains("${key}_lng")) {
LatLng(
Double.fromBits(getLong("${key}_lat", 0L)),
Double.fromBits(getLong("${key}_lng", 0L))
)
} else null
fun Editor.putLatLng(key: String, value: LatLng?): Editor {
if (value == null) {
remove("${key}_lat")
remove("${key}_lng")
} else {
putLong("${key}_lat", value.latitude.toBits())
putLong("${key}_lng", value.longitude.toBits())
}
return this
}