Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to retrieve data based on the closest distance in room persistance [duplicate]

hay all .. i have a customers table in my android database .. i am currently using room persistance, i am looking to take all my customers data .. but sequential from start location to nearest me at the moment .. how to query for it my case example .. My current latitude is -7.3530829 , my longitude is currently 112.6901788

    data in my customers table
    customers.address = "Waterpark Boulevard Citraland Area
    customers.handphone1 = "0857887328"
    customers.latitude = -7.2864881
    customers.longitude = 112.6525076


    data in my customers table
    customers.address = "Nature Conservation Area
    customers.handphone1 = "0857887328"
    customers.latitude = -7.3571553
    customers.longitude = 112.6862058


    data in my customers table
    customers.address = "Kawasan Taman Pinang
    customers.handphone1 = "0857887328"
    customers.latitude =
            -7.3403807
    customers.longitude = 112.6914085

   //get Data by Name,,
   @Query("SELECT * FROM customers WHERE name = :name")
   abstract fun findByName(name:String): MutableList<Customers>

   //How to retrieve data based on the closest distance ?
   @Query("SELECT ???")
   abstract fun findByDistance(mylatitude:Double,mylongitude:Double): MutableList<Customers>

please help to findByDistance query ..

like image 317
abdul Avatar asked Oct 25 '25 23:10

abdul


1 Answers

I've found the right query by calculating the straight distance..

@Query("SELECT * FROM customers ORDER BY ABS(latitude - :latitude) + ABS(longitude - :longitude) ASC")
abstract fun findByDistance(latitude:Double,longitude:Double): MutableList<Customers>

Maybe this can help you who need the same thing as me.. thank you..

like image 178
abdul Avatar answered Oct 27 '25 11:10

abdul