My app allows a user to tape on a cross hair in the center of the map and then add a marker. The marker lat, lng as well as name are saved to a database and thats working fine. I dont add an id as my database adds the id.
public long addEntry(String theLabel, String theArea, String theLat, String theLng) {
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_NAME, theLabel);
contentValues.put(KEY_AREA, theArea);
contentValues.put(KEY_LAT, theLat);
contentValues.put(KEY_LNG, theLng);
return mySpotsDatabase.insert(DATABASE_TABLE, null, contentValues);
}
All good so far and on subsequent loads of the map I get the data from the SQLite database and populate the map with the markers. When I do the retrieval I also include the id of the SQlite entry.
public Cursor getAllData() {
String [] columns = new String [] {KEY_ROWID, KEY_NAME, KEY_AREA, KEY_LAT, KEY_LNG};
Cursor c = mySpotsDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
c.moveToFirst();
return c;
}
And in my maps page
MySpotsDatabase spotDatabase = new MySpotsDatabase(MyMap.this);
spotDatabase.open();
if (spotDatabase.getNumRows() == 0)
spotDatabase.addEntry("MarkerTitle", "One", myLatitude.toString(), myLongitude.toString());
spotDatabase.close();
spotDatabase.open();
myDataC = spotDatabase.getAllData();
do {
dataprovider = new MarkersDataProvider(myDataC.getString(0), myDataC.getString(1), myDataC.getString(2), myDataC.getString(3), myDataC.getString(4));
arrayList.add(dataprovider);
} while (myDataC.moveToNext());
spotDatabase.close();
Again all good so far, BUT now I want to update the marker if the user drags it to another location or if they want to update the title label.
HOW do I set, get and restore a individual marker and write its updated lat, lng, and name back to the SQLite database. THE WRITING BACK to the SQLite database is trivial and I have that sorted, but I need to know which entry in the SQLite database I need to update.
SO I am trying to find a way to grab the _id that I read from the database for each marker and keep that reference with its marker so I can update the database when needed.
ATTEMPT I tried was setting the marker title to the _id but that gives me a null reference for some reason
LatLng newMarker = new LatLng(myLat, myLng);
MarkerOptions newOptions = new MarkerOptions();
newOptions.position(newMarker);
int lastEntry = arrayList.size()-1;
String rowID = arrayList.get(lastEntry).getRowID();
newOptions.title(rowID);
newOptions.draggable(true);
String theSnippet = "Marker id: " + arrayList.get(lastEntry).getRowID() + "\nLat: " + String.valueOf(myLat) + "\nLng: " + String.valueOf(myLng);
newOptions.snippet(theSnippet);
myMap.addMarker(newOptions);
Snackbar.make(findViewById(R.id.myMapCoordinatorLayout), rowID + " " + beforeCount + " " + afterCount , Snackbar.LENGTH_INDEFINITE).show();
Looking at hashmaps but unsure if thats the right solution. I will keep digging as maybe storing the _id in the title might still work if I can sort out why its giving me a null reference.