如何在SQlite数据库中保存markerId()和特定标记的图像引用?

时间:2014-12-22 08:34:44

标签: android sqlite google-maps

我需要保存markerId()以及SQLite数据库中的图像,以便在重新加载应用时显示该标记的图像。

以下是我放置标记以及获取该标记图像的代码:

@Override
public void onMapLongClick(LatLng point) {
    thePoint=point;

    drawMarker(point);      
    ContentValues contentValues = new ContentValues();
    contentValues.put(LocationsDB.FIELD_LAT, point.latitude );
    contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
    contentValues.put(LocationsDB.FIELD_IMAGE, bitmap); //error under "put" The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Bitmap)
    contentValues.put(LocationsDB.FIELD_MARKER_ID, markerId);
    contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);
    LocationInsertTask insertTask = new LocationInsertTask();
    insertTask.execute(contentValues);
    Toast.makeText(getBaseContext(), "Please Take Photo", Toast.LENGTH_SHORT).show();

     Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
     File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My Folder");
     imagesFolder.mkdirs();
     image = new File(imagesFolder.getPath(), "Img_" + timeStamp + ".jpg");
     fileUri = Uri.fromFile(image);

     imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
     startActivityForResult(imageIntent, TAKE_PICTURE);
}

  private void drawMarker(LatLng point) {
    Marker marker = googleMap.addMarker(new MarkerOptions()
    .position(thePoint));
    markerId = marker.getId();
    marker.getPosition();

}

然后在我的数据库中,如果有:

public static final String FIELD_ROW_ID = "_id";
public static final String FIELD_LAT = "lat";
public static final String FIELD_LNG = "lng";
public static final String FIELD_ZOOM = "zom";
public static final String FIELD_IMAGE = "img";
public static final String FIELD_MARKER_ID = "id";
private static final String DATABASE_TABLE = "locations";

private SQLiteDatabase mDB;

public LocationsDB(Context context) {
    super(context, DBNAME, null, VERSION);
    this.mDB = getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    String sql =     "create table " + DATABASE_TABLE + " ( " +
                     FIELD_ROW_ID + " integer primary key autoincrement , " +
                     FIELD_LNG + " double , " +
                     FIELD_LAT + " double , " +
                     FIELD_ZOOM + " text , " +
                     FIELD_IMAGE + " text , " +
                     FIELD_MARKER_ID + "text " +
                     " ) ";

    db.execSQL(sql);
}

public long insert(ContentValues contentValues){
    long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
    return rowID;
}

public int del(){
    int cnt = mDB.delete(DATABASE_TABLE, null , null);
    return cnt;
}

public Cursor getAllLocations(){
    return mDB.query(DATABASE_TABLE, new String[] { FIELD_ROW_ID,  FIELD_LAT , FIELD_LNG, FIELD_ZOOM, FIELD_IMAGE, FIELD_MARKER_ID } , null, null, null, null, null, null);
}

然后在我的onLoadFinished(这是我不知道该放什么的地方)

public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    int locationCount = 0;
    double lat=0;
    double lng=0;
    float zoom=0;
    float img=0;
    float id=0;
    locationCount = arg1.getCount();
    arg1.moveToFirst();

    for(int i=0;i<locationCount;i++){

        lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
        lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
        zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
        img = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
        id = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_MARKER_ID));
        thePoint = new LatLng(lat, lng);
        drawMarker(thePoint);
        arg1.moveToNext();
    }

    if(locationCount>0){
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
        float bitmap = (img);
        float markerId = (id);
    }
}

这是添加标记的HashMap:

private Map<String, Bitmap> myMarkersHash;
private String markerId;

....

myMarkersHash = new HashMap<String, Bitmap>();

....

final ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
Bitmap bitmap = myMarkersHash.get(marker.getId());
markerIcon.setImageBitmap(bitmap);


private void drawMarker(LatLng point) {
    Marker marker = googleMap.addMarker(new MarkerOptions()
    .position(thePoint));
    markerId = marker.getId();

请善待我,这是我第一次使用SQlite并仍在学习:-) 感谢

0 个答案:

没有答案