dimanche 21 décembre 2014

How to save extra marker data to SQlite database

In my app i want to save the extra data from my map (google map v2) and that is an image taken from camera intent and added to that marker.


So i am going to try and give as much information as possible, so here goes:


first i had to create a Hashmap for for the image:



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


Then in onCreate :



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


then when adding my marker (with the camera intent):



@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_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(), "Image_" + 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();

}


And then when displaying the image when tapped on the marker:



@Override
public View getInfoContents(Marker marker)
{
View v = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
final ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
Bitmap bitmap = myMarkersHash.get(marker.getId());
markerIcon.setImageBitmap(bitmap);

return v;


So all of this works fine, So i now needed to save all these markers to the map so when the user returns to the map all their markers are still there (hence why i am using SQlite)


So now the markers are saved (sorry for displaying the full code of the classes, but am not sure what to post and what not to post.


This is my LocationsContentProvider class:



public class LocationsContentProvider extends ContentProvider{

public static final String PROVIDER_NAME = "com.example.mainapp.locations";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/locations" );
private static final int LOCATIONS = 1;
private static final UriMatcher uriMatcher ;

static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "locations", LOCATIONS);
}

LocationsDB mLocationsDB;

@Override
public boolean onCreate() {
mLocationsDB = new LocationsDB(getContext());
return true;
}

@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = mLocationsDB.insert(values);
Uri _uri=null;
if(rowID>0){
_uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
}else {
try {
throw new SQLException("Failed to insert : " + uri);
} catch (SQLException e) {
e.printStackTrace();
}
}
return _uri;
}

@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 0;
cnt = mLocationsDB.del();
return cnt;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {

if(uriMatcher.match(uri)==LOCATIONS){
return mLocationsDB.getAllLocations();
}
return null;
}

@Override
public String getType(Uri uri) {
return null;
}
}


Then in my LocationsDB Class:



public class LocationsDB extends SQLiteOpenHelper{

/** Database name */
private static String DBNAME = "locationmarkersqlite";
private static int VERSION = 1;
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";
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 " +
" ) ";

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

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}


Then in my MainActivity:



@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
Uri uri = LocationsContentProvider.CONTENT_URI;
return new CursorLoader(this, uri, null, null, null, null);
}


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

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

// Get the latitude
lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
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));
}
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {

}
class LocationInsertTask extends AsyncTask<ContentValues, Void, Void>{
@Override
protected Void doInBackground(ContentValues... contentValues) {
getContentResolver().insert(LocationsContentProvider.CONTENT_URI, contentValues[0]);
return null;
}
}

private class LocationDeleteTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
getContentResolver().delete(LocationsContentProvider.CONTENT_URI, null, null);
return null;
}
}
}

}


So as you can see I need to save the image taken from the onMapLongClick to the database. How would i go about that?


Aucun commentaire:

Enregistrer un commentaire