Improve place search speed by ~10x (#1236)

* improve it

* fix distortion with lattitude + remove far away results
This commit is contained in:
leekleak 2025-02-08 16:56:18 +02:00 committed by GitHub
parent a371213d4a
commit ce5495418e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 4 deletions

View File

@ -141,7 +141,7 @@ internal class OsmLocationProvider(
return result
.asSequence()
.filter {
!hideUncategorized || (it.category != null)
(!hideUncategorized || (it.category != null)) && it.distanceTo(userLocation) < searchRadiusMeters
}
.groupBy {
it.label.lowercase()

View File

@ -7,6 +7,7 @@ import retrofit2.Retrofit
import retrofit2.http.Body
import retrofit2.http.POST
import java.lang.reflect.Type
import kotlin.math.cos
data class OverpassFuzzyRadiusQuery(
val tag: String = "name",
@ -62,10 +63,13 @@ class OverpassFuzzyRadiusQueryConverter : Converter<OverpassFuzzyRadiusQuery, Re
) { Regex.escapeReplacement(it) }
val overpassQlBuilder = StringBuilder()
overpassQlBuilder.append("[out:json];")
val latDegreeChange = value.radius * 0.00001 / 1.11
val lonDegreeChange = latDegreeChange / cos(Math.toRadians(value.latitude))
val boundingBox = arrayOf(value.latitude - latDegreeChange, value.longitude - lonDegreeChange,
value.latitude + latDegreeChange, value.longitude + lonDegreeChange)
overpassQlBuilder.append("[out:json][timeout:10][bbox:" + boundingBox.joinToString(",") + "];")
// nw: node or way
overpassQlBuilder.append("nw(around:", value.radius, ',', value.latitude, ',', value.longitude, ')')
overpassQlBuilder.append('[', value.tag, '~', escapedQueryName, if (value.caseInvariant) ",i];" else "];")
overpassQlBuilder.append("nw[", value.tag, "~", escapedQueryName, if (value.caseInvariant) ",i];" else "];")
// center to add the center coordinate of a way to the result, if applicable
overpassQlBuilder.append("out center;")