如何从数据库中检索位置对象?

发布于 2024-10-03 03:43:11 字数 1043 浏览 4 评论 0原文

我想将 Location 对象存储到我的数据库中,然后能够加载它们。如何根据 Location 对象的属性构建 Location 对象?

  • 我应该创建一个空白位置然后设置属性吗? (如果是,如何?)
  • 我应该使用我的数据库作为 LocationProvider 吗? (如果是,怎么做?)

谢谢!

编辑:到目前为止,我发现的唯一可行的解​​决方案是检索当前位置,并覆盖所有属性......必须有更好的方法!

我的代码:

public Location selectLocationById(int id){
    String query = "SELECT * FROM " + LOCATIONS_TABLE + " WHERE id = " + id + ";";
    Cursor c = db.rawQuery(query, null);
    Location l = null;
    if(c.getCount() == 1) {
        c.moveToFirst();
        if(c.getColumnCount() == 8){
            l = new Location(MyProject.getCurrentLoc());
            l.setAccuracy(c.getFloat(1));
            l.setAltitude(c.getDouble(2));
            l.setBearing(c.getFloat(3));
            l.setLatitude(c.getDouble(4));
            l.setLongitude(c.getDouble(5));
            l.setSpeed(c.getFloat(6));
            l.setTime(c.getLong(7));
        }
    }

    if(c != null && !c.isClosed()) {
        c.close();
    }

    return l;
}

I want to store Location objects into my database, and then be able to load them. How can I build a Location object from its attributes?

  • Should I create a blank Location and then set the attributes? (If yes, how?)
  • Should I use my database as a LocationProvider? (If yes, how?)

Thanks!

EDIT: The only working solution I found so far is to retrieve the current Location, and override all the attributes... There has to be a better way!

my code:

public Location selectLocationById(int id){
    String query = "SELECT * FROM " + LOCATIONS_TABLE + " WHERE id = " + id + ";";
    Cursor c = db.rawQuery(query, null);
    Location l = null;
    if(c.getCount() == 1) {
        c.moveToFirst();
        if(c.getColumnCount() == 8){
            l = new Location(MyProject.getCurrentLoc());
            l.setAccuracy(c.getFloat(1));
            l.setAltitude(c.getDouble(2));
            l.setBearing(c.getFloat(3));
            l.setLatitude(c.getDouble(4));
            l.setLongitude(c.getDouble(5));
            l.setSpeed(c.getFloat(6));
            l.setTime(c.getLong(7));
        }
    }

    if(c != null && !c.isClosed()) {
        c.close();
    }

    return l;
}

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

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

发布评论

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

评论(1

当梦初醒 2024-10-10 03:43:11

这是有道理的。我相信您可以使用

l = new Location(null);

这样您就不必获取当前位置。这仍然应该创建一个新的位置,但提供者字符串为 null,使用其他位置构造函数:

Location(String provider)

请参阅 http://developer.android.com/reference/android/location/Location.html

This makes sense. I believe you could use

l = new Location(null);

So that you don't have to get your current location. This should still create a new Location, but with a provider string of null, using the other Location constructor:

Location(String provider)

See http://developer.android.com/reference/android/location/Location.html

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