如何根据房间持久性中的最近距离检索数据

时间:2018-02-01 02:36:01

标签: android sqlite kotlin location android-room

干草全部...我在我的android数据库中有一个客户表..我目前正在使用房间持久性,我希望把我所有的客户数据..但是从开始位置顺序到最近的我...查询我的案例..我目前的纬度是-7.3530829,我的经度目前是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>

请帮助查找远程查询..

1 个答案:

答案 0 :(得分:2)

我通过计算直线距离找到了正确的查询..

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

也许这可以帮助那些需要和我一样的人..谢谢..