be a bit smarter about bounding boxes for various types of shared locations

This commit is contained in:
johan12345
2021-06-10 21:05:48 +02:00
parent 232aecfe3b
commit 7946663299
2 changed files with 29 additions and 3 deletions

View File

@@ -77,6 +77,7 @@ import net.vonforst.evmap.ui.ChargerIconGenerator
import net.vonforst.evmap.ui.ClusterIconGenerator
import net.vonforst.evmap.ui.MarkerAnimator
import net.vonforst.evmap.ui.getMarkerTint
import net.vonforst.evmap.utils.boundingBox
import net.vonforst.evmap.utils.distanceBetween
import net.vonforst.evmap.viewmodel.*
@@ -784,7 +785,8 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
positionSet = true
} else if (lat != null && lon != null) {
// show given position
val cameraUpdate = map.cameraUpdateFactory.newLatLngZoom(LatLng(lat, lon), 16f)
val latLng = LatLng(lat, lon)
val cameraUpdate = map.cameraUpdateFactory.newLatLngZoom(latLng, 16f)
map.moveCamera(cameraUpdate)
if (chargerId != null) {
@@ -804,7 +806,7 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
})
} else {
// mark location as search result
vm.searchResult.value = PlaceWithBounds(LatLng(lat, lon), null)
vm.searchResult.value = PlaceWithBounds(latLng, boundingBox(latLng, 750.0))
}
positionSet = true
@@ -817,7 +819,16 @@ class MapFragment : Fragment(), OnMapReadyCallback, MapsActivity.FragmentCallbac
val latLng = LatLng(it.latitude, it.longitude)
val cameraUpdate = map.cameraUpdateFactory.newLatLngZoom(latLng, 16f)
map.moveCamera(cameraUpdate)
vm.searchResult.value = PlaceWithBounds(latLng, null)
val bboxSize = if (it.subAdminArea != null) {
750.0 // this is a place within a city
} else if (it.adminArea != null && it.adminArea != it.featureName) {
4000.0 // this is a city
} else if (it.adminArea != null) {
100000.0 // this is a top-level administrative area (i.e. state)
} else {
500000.0 // this is a country
}
vm.searchResult.value = PlaceWithBounds(latLng, boundingBox(latLng, bboxSize))
}
}
}

View File

@@ -2,6 +2,8 @@ package net.vonforst.evmap.utils
import android.content.Intent
import android.location.Location
import com.car2go.maps.model.LatLng
import com.car2go.maps.model.LatLngBounds
import kotlin.math.*
/**
@@ -13,6 +15,12 @@ fun Location.plusMeters(dx: Double, dy: Double): Pair<Double, Double> {
return Pair(lat, lon)
}
fun LatLng.plusMeters(dx: Double, dy: Double): LatLng {
val lat = this.latitude + (180 / Math.PI) * (dx / 6378137.0)
val lon = this.longitude + (180 / Math.PI) * (dy / 6378137.0) / cos(Math.toRadians(lat))
return LatLng(lat, lon)
}
const val earthRadiusM = 6378137.0
/**
@@ -60,3 +68,10 @@ internal fun stringToCoords(s: String?): List<Double>? {
null
}
}
fun boundingBox(pos: LatLng, sizeMeters: Double): LatLngBounds {
return LatLngBounds(
pos.plusMeters(-sizeMeters, -sizeMeters),
pos.plusMeters(sizeMeters, sizeMeters)
)
}