代码之家  ›  专栏  ›  技术社区  ›  nbarraille

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

  •  1
  • nbarraille  · 技术社区  · 14 年前

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

    • 我应该创建一个空白位置然后设置属性吗?(如果是,怎么做?)
    • 我应该将我的数据库用作位置提供程序吗?(如果是,怎么做?)

    谢谢!

    编辑:到目前为止,我找到的唯一解决方案是检索当前位置,并覆盖所有属性。。。一定有更好的办法!

    我的代码:

    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;
    }
    
    1 回复  |  直到 14 年前
        1
  •  3
  •   spanky    14 年前

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

    l = new Location(null);
    

    这样你就不用知道你现在的位置了。这仍应创建一个新位置,但提供程序字符串为空,使用其他位置构造函数:

    Location(String provider)
    

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