Khudoyshukur Juraev [11:50 AM] Hi, all! I am usi...
# server
k
Khudoyshukur Juraev [11:50 AM] Hi, all! I am using ktor in the backend side. I have tables:
Drivers {
id: Long
......
}
DriverLocations {
id: Long
driverId: Long
latitude: Double
longitude: Double
datetime: Long
}
I need to query the drivers within 1 km radius from Some point. To calculate distance, i can use:
Copy code
fun haversine(lat1: Double, lon1: Double, lat2: Double, lon2: Double): Double {
    val earthRadius = 6371000 // Radius of the earth in meters

    val latDistance = Math.toRadians(lat2 - lat1)
    val lonDistance = Math.toRadians(lon2 - lon1)
    val a = sin(latDistance / 2) * sin(latDistance / 2) +
            cos(Math.toRadians(lat1)) * cos(Math.toRadians(lat2)) *
            sin(lonDistance / 2) * sin(lonDistance / 2)
    val c = 2 * atan2(sqrt(a), sqrt(1 - a))

    return earthRadius * c // Calculate the distance in meters
}
However, i do not now how to query this with exposed?
h
I would write plain sql code first, then transform this sql code to exposed dsl. Maybe you need to create some functions.
c
@Khudoyshukur Juraev My approach: 1. Set up a database and query with plain sql. 2. Create Test-Cases for the kotlin-query. 3. Convert the sql to exposed dsl. I am not sure as well how to convert, but as a developer it is your job to make the research needed. 4. Test you solution. Good luck 🙂
k
@hfhbd @Christopher Hübner Thank you guys. Figured out the solution with your suggestions.