How do I save locations of a map in an SQLite database?

5

I want to save locations displayed on a map to a SQLite database, so that the user can refer to them in the future and can add a description. Here's my class Description ...

public class Descricao {
  private long id;
  private String comentario;
  private double mLat;
  private double mLng;

  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  public double getLat() {
    return mLat;
  }

  public void setLat(double lat) {
    this.mLat = lat;
  }

  public double getLng() {
    return mLng;
  }

  public void setLng(double lng) {
    this.mLng = lng;
  }

  public String getComentario(){
    return comentario;
  }

  public void setComentario( String comentario) {
     this.comentario = comentario;
  }

  // Will be used by the ArrayAdapter in the ListView
  @Override
  public String toString() {
     return comentario;
  }
} 

Here I have the code part of my MainActivity.java where I call latitude and longitude to display the locations on the map:

  mPlaces = (Place[]) savedInstanceState.getParcelableArray("places");              
                for(int i=0;i<mPlaces.length;i++){


                    LatLng point = new LatLng(Double.parseDouble(mPlaces[i].mLat),
                                              Double.parseDouble(mPlaces[i].mLng));


                    Marker m = drawMarker(point,UNDEFINED_COLOR);


                    mHMReference.put(m.getId(), mPlaces[i]);
                }

This is the MySQLiteOpenHelper.java class

public class MySQLiteHelper extends SQLiteOpenHelper {

      public static final String TABELA_DESCRICAO = "descricao";
      public static final String COLUMN_ID = "_id";
      public static final String COLUMN_COMENTARIO = "comentario";
      public static final String COLUMN_LAT = "lat";
      public static final String COLUMN_LNG = "lng";
      private static final String DATABASE_NAME = "descricao.db";
      private static final int DATABASE_VERSION = 1;

      // Database creation sql statement
      private static final String DATABASE_CREATE = "create table "
          + TABELA_DESCRICAO + "(" + COLUMN_ID
          + " integer primary key autoincrement, " + COLUMN_COMENTARIO
          + " text not null)" +
          COLUMN_LAT + "double" + COLUMN_LNG + "double"
          + COLUMN_COMENTARIO + "text no null";


      public MySQLiteHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }

      @Override
      public void onCreate(SQLiteDatabase basedados) {
        basedados.execSQL(DATABASE_CREATE);
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(MySQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS " + TABELA_DESCRICAO);
        onCreate(db);
      }       
}

This is the class DescriptionAdoO.java that interfaces between the class Descripcia.java and MySQLiteOpenHelper.java

public class DescricaoDAO {
public class DescricaoDAO {

   // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_LAT, MySQLiteHelper.COLUMN_LNG,
      MySQLiteHelper.COLUMN_COMENTARIO };

  public DescricaoDAO(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }


public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Descricao createComment(String comentario) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMENTARIO, comentario);
    long insertId = database.insert(MySQLiteHelper.TABELA_DESCRICAO, null,
        values);
    Cursor cursor = database.query(MySQLiteHelper.TABELA_DESCRICAO,
        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Descricao newComment = cursorToComment(cursor);
    cursor.close();
    return newComment;
  }

  public void deleteComment(Descricao comentario) {
    long id = comentario.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABELA_DESCRICAO, MySQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }

  public List<Descricao> getAllComments() {
    List<Descricao> descricao = new ArrayList<Descricao>();

    Cursor cursor = database.query(MySQLiteHelper.TABELA_DESCRICAO,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
      Descricao comentario = cursorToComment(cursor);
      descricao.add(comentario);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return descricao;
  }

  private Descricao cursorToComment(Cursor cursor) {
    Descricao comentario = new Descricao();
    comentario.setId(cursor.getLong(0));
    comentario.setComentario(cursor.getString(1));
    return comentario;
  }
} 
    
asked by anonymous 02.09.2015 / 01:01

1 answer

2

Save the coordinates, latitude and longitude as a String in the database, then check the two information as a parameter.

If this is still not useful, check out this material, I think it will help you.

link

    
03.09.2015 / 14:59