开发者

How to retrieve Location Objects from a database?

开发者 https://www.devze.com 2023-01-25 07:48 出处:网络
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?

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消