【问题标题】:Deleting database rows based on current location根据当前位置删除数据库行
【发布时间】:2011-10-26 01:48:07
【问题描述】:

我在我的 Android 应用程序的普通 SQLite 数据库中存储具有位置(纬度/经度)的对象。应用程序接收带有存储在此 DB 中的新对象的消息(UDP 数据包)。

为了避免数据库在某一点爆炸并使其保持轻便,我想永久删除所有比特定半径更远的对象。 DB 通过实现 DB Helper 的 DB-Adapter 类进行控制。

由于我不想使用距离近似值,我想使用半正弦公式。现在我有以下问题:

  1. 在轻量级智能解决方案中实现此任务的最佳方式是什么?
  2. 我考虑过使用 AsyncTask 来完成这项工作。这是推荐的吗?

【问题讨论】:

标签: android sqlite


【解决方案1】:

这是我一直用来为具有存储在数据库中的 GPS 数据的对象获取排序子句的方法:

public String getOrderString(double latitude, double longitude) {
    double fudge = Math.pow(Math.cos(Math.toRadians(latitude)), 2);

    return "((" + latitude + " - " + KEY_LATITUDE + ") * (" + latitude
            + " - " + KEY_LATITUDE + ") + (" + longitude + " - "
            + KEY_LONGITUDE + ") * (" + longitude + " - " + KEY_LONGITUDE
            + ") * " + fudge + ") ASC";
}

here'借来的'。我对软糖因素的理解是它考虑了不在赤道附近的坐标。到目前为止,这个方程对我来说效果很好,而且相当快。

现在,我认为您可以通过执行以下操作来利用相同的方程式:

public String getWhereString(double latitude, double longitude, double threshold) {
    double fudge = Math.pow(Math.cos(Math.toRadians(latitude)), 2);

    return "((" + latitude + " - " + KEY_LATITUDE + ") * (" + latitude
            + " - " + KEY_LATITUDE + ") + (" + longitude + " - "
            + KEY_LONGITUDE + ") * (" + longitude + " - " + KEY_LONGITUDE
            + ") * " + fudge + ") < " + threshold;
}

那么你的删除方法是这样的:

public int removeBuildingEntry(double latitude, double longitude, double threshold) {
    String where = getWhereString(double latitude, double longitude, double threshold)

    return db.delete(TABLE, where, null);
}

唯一的问题是我不知道方程结果的单位,因此你应该传入什么作为阈值。在我的情况下,我不关心那些,因为我只想要订单,但在你的情况下,这可能会有所作为。如果需要更准确的数字,您可以尝试使用不同的值或尝试计算它。

【讨论】:

  • 非常感谢您的代码和解释!这真的很有帮助!我会尝试并报告!
  • 好的,我是这样实现的,效果很好。对于单位,我将两个产品乘以 111.2 公里,大约是 111.2 公里。赤道处一度的宽度。有了软糖因素,它也可以工作。近似是好的。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
相关资源
最近更新 更多