根据当前位置删除数据库行

发布于 2024-12-12 04:00:24 字数 308 浏览 0 评论 0原文

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

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

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

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

I store objects with a position (lat/long) in a normal SQLite database in my Android applicaton. The application receives messages (UDP packets) with new objects which are stored in the this DB.

To avoid the explosion of the DB at one point and to keep it light, I would like to delete all objects which are further away than a specific radius permanently. The DB is controlled via a DB-Adapter class implementing a DB Helper.

As I do not want to use distance approximations, I would like to use the haversine formula. Now I have the following questions:

  1. What is the best way to implement this task in a lightweight intelligent solution?
  2. I thought about using an AsyncTask to do the job. Is this recommended?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撩人痒 2024-12-19 04:00:24

这是我一直用来获取数据库中存储的 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";
}

此处。我对模糊因子的理解是,它考虑了不在赤道附近的坐标。到目前为止,这个方程对我来说效果很好,而且速度相当快。

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

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);
}

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

This is the method that I have been using to get a sort clause for objects with GPS data stored in a database:

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";
}

'borrowed' from here. My understanding of the fudge factor is that it takes into account coordinates that are not near the equator. This equation has worked well for me so far and is fairly quick.

Now, I think you can utilize the same equation, by doing something like:

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;
}

Then your delete method would be like:

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

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

The only issue is that I do not know the units of the result of the equation, and therefore what you should pass in as a threshold. In my situation I don't care about those as I only want the order, but in your case that might make a difference. You could either play around with different values or attempt to calculate it if you need a more exact number.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文