Find N closest points with geohash

70 views Asked by At

I have issues with finding N closest points using geohash. Currently I have done it with the location, need to change it with geohash usage (suppose, it should be faster if I already have 10-length geohashes in my POI).

fun closestItems(items: List<Item>, centralPoint: LatLng, maxCount: Int): List<Item> {
    // Calculate the squared distance for each item to the central point
    val itemsWithDistance = items.map { item ->
        val latDiff = item.latLng.latitude - centralPoint.latitude
        val lngDiff = item.latLng.longitude - centralPoint.longitude
        val distanceSquared = latDiff.pow(2) + lngDiff.pow(2)
        Pair(item, distanceSquared)
    }

    // Sort the items by distance
    val sortedItems = itemsWithDistance.sortedBy { it.second }

    // Take the closest 5000 items or less if there are fewer items
    val closestItems = sortedItems.take(maxCount.coerceAtMost(items.size))

    return closestItems.map { it.first }
}

Any common programming language is fine.

0

There are 0 answers