如何存储ArrayList< LatLng>在Sqlite数据库上? [英] How to store ArrayList<LatLng> on Sqlite database?

查看:148
本文介绍了如何存储ArrayList< LatLng>在Sqlite数据库上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个跟踪用户运动的应用程序.在每个onLocationChanged()上,我将纬度和经度存储在ArrayList<latLng>中,然后在路线上绘制一条折线.现在,当activity停止时,我想保存所有用户的活动信息,例如:

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:

  • 距离
  • 平均速度
  • 持续时间
  • 所有ArrayList的活动点

以便稍后还原它,并以此点画一条折线. 我不知道如何将纬度和经度的ArrayList存储到数据库中的问题. 有人可以帮忙吗?

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

运行片段类:

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

那是run_rable的样子:

Thats how run_rable looks:

这就是Location_points的外观:

Thats how Location_points looks:

推荐答案

首先阅读有关然后,您应该阅读有关内容提供商

Then you should read about Content Providers

了解如何在Android上使用sqlite后,设计表格.我猜您将有一个带有列的表:

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.

为您提供有关如何存储ArrayList的小提示,然后使用类似以下内容的

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)

这篇关于如何存储ArrayList&lt; LatLng&gt;在Sqlite数据库上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆