简体   繁体   中英

How to store ArrayList<LatLng> on Sqlite database?

I'm developing an app that tracks user movement. On each onLocationChanged() I store the latitude and longitude in ArrayList<latLng> and than draw a polyline on the route. Now, when activity stopped I want to save all users activity information like:

  • distance
  • average pace
  • duration
  • all ArrayList latlng points of activity

on Sqlite database to restore it later and draw a polyline with this points. The problem that I have no idea how to store the ArrayList of latitude and longitude to the database. Anyone can help?

My solution edit, maybe will help someone!

public class SQLUtils extends SQLiteOpenHelper {

private static final String DB_NAME = "data.db";
private static final int DB_VERSION = 1;
private static final String run_table = "Run_table";
private static final String locationPoints = "Location_points";

public SQLUtils(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    Log.d("DB", "onCreate");

    String runTable = "create table " + run_table + "(ID VARCHAR(10), PACE VARCHAR(10), DURATION VARCHAR(10), DISTANCE VARCHAR(10), DATE VARCHAR(10), TIME VARCHAR(10));";
    String LocationTable = "create table " + locationPoints + "(ID INTEGER, POSITION INTEGER, Latitude REAL, Longitude REAL);";

    db.execSQL(runTable);
    db.execSQL(LocationTable);
}

public void insertRunData(String id, String pace,String duration, String distance, String date, String time) {
    Log.d("DB", "onInsertRunData");

    SQLiteDatabase db = this.getWritableDatabase();

    String sql = "INSERT into " + run_table +
        " (ID, PACE, DURATION, DISTANCE, DATE, TIME) VALUES ('" + id + "','" + pace + "','" + duration + "','" + distance +
           "','" + date + "','" + time + "')";

    db.execSQL(sql);

    db.close();
}

    public void insertLatLng(String id, int position, double latitude, double longitude){
    SQLiteDatabase db = this.getWritableDatabase();

    String sql = "INSERT into " + locationPoints +
            " (ID, POSITION, LATITUDE, LONGITUDE) VALUES ('" + UUID.fromString(id) + "','" + Integer.toString(position) + "','"
             + String.valueOf(latitude) + "','" + String.valueOf(longitude) + "')";

    db.execSQL(sql);
    db.close();
}

Run fragment class:

    private void saveSqlRun() {
    SQLUtils sqlUtils = new SQLUtils(getActivity());
    sqlUtils.insertRunData(currentRun.getId(), currentRun.getAveragePace(), currentRun.getRunDuration(),
            currentRun.getRunDistance(), currentRun.getRunDate(),currentRun.getRunTime());

    /* Save user Location points */
    for (int index = 0; index < currentRun.getLocationPoints().size(); ++index) {
        sqlUtils.insertLatLng(currentRun.getId(), index, currentRun.getLocationPoints().get(index).latitude, currentRun.getLocationPoints().get(index).longitude);
    }
}

Thats how run_rable looks: 在此处输入图片说明

Thats how Location_points looks: 在此处输入图片说明

First read about Sqlite

Then you should read about Content Providers

After you understood how to use sqlite with android, design your tables. I'm guessing that you will have a table with columns:

 id, userId, lat, lng, timeStamp.

Now you can easily get for each user where he was and when and calculate all the rest.

To give you a small hint of how to store ArrayList then use something like:

ArrayList<ContentProviderOperation> ops
for (LatLng latLng : list) {
    ops.add(ContentProviderOperation.insert(Uri).withValue("userId", userId).withValue("lat", 32.3232)...build()
} 

getActivity().getContentResolver().applyBatch("AUTHORITY", ops)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM