I have implemented a custom map that allows users to add markers to shared preferences using long click, finally I got it to work. On my map I have a button that calls a method "Clear Marker" when the button is pressed I am required to exit the map and re-open it for the changes to be made. I would like the map to instantly update and also is there a way to remove the last item added to shared preferences? IE clear the previous marker, not every single marker added. The code used to store a marker is
@Override
public void onMapLongClick(LatLng latLng) {
addressEditText = (EditText) findViewById(R.id.editTextAddMarker);
title12 = addressEditText.getText().toString();
if (title12.length() > 2) {
MarkerOptions markerOpt1 = new MarkerOptions()
.title(title12)
.anchor(0.5f, 0.5f);
markerOpt1.position(latLng);
mMap.addMarker(markerOpt1);
Toast.makeText(this, "Marker Added", Toast.LENGTH_LONG).show();
locationCount++;
/** Opening the editor object to write data to sharedPreferences */
SharedPreferences.Editor editor = sharedPreferences.edit();
// Storing the latitude for the i-th location
editor.putString("lat" + Integer.toString((locationCount - 1)), Double.toString(latLng.latitude));
// Storing the longitude for the i-th location
editor.putString("lng" + Integer.toString((locationCount - 1)), Double.toString(latLng.longitude));
//editor.putString("title", addressEditText.getText().toString());
editor.putString("title" + (locationCount-1), addressEditText.getText().toString());
// Storing the count of locations or marker count
editor.putInt("locationCount", locationCount);
/** Saving the values stored in the shared preferences */
editor.commit();
} else if (title12.length() < 1) {
Toast.makeText(this, "Enter title at the top left.", Toast.LENGTH_LONG).show();
}
}
I then retrieve the data from shared preferences and draw it onto the map in my onMapReady()
// Opening the sharedPreferences object
sharedPreferences = getSharedPreferences("location", 0);
// Getting number of locations already stored
locationCount = sharedPreferences.getInt("locationCount", 0);
// If locations are already saved
if (locationCount != 0) {
String lat = "";
String lng = "";
String title13 = "";
// Iterating through all the locations stored
for (int i = 0; i < locationCount; i++) {
// Getting the latitude of the i-th location
lat = sharedPreferences.getString("lat" + i, "0");
// Getting the longitude of the i-th location
lng = sharedPreferences.getString("lng" + i, "0");
SharedPreferences myPrefs = PreferenceManager.getDefaultSharedPreferences(this);
title13 = sharedPreferences.getString("title" + i, "0");
//Toast.makeText(this, lat + "," + lng, Toast.LENGTH_LONG).show();
double lat3 = Double.valueOf(lat).doubleValue();
double lng3 = Double.valueOf(lng).doubleValue();
position1 = new LatLng(lat3, lng3);
drawMarker(position1,title13);
}
}
Finally the code used to remove the marker(The problem is here) The markers don't remove from the map after the button is pressed, only when the user presses the button leave and returns to the map. Also it clears shared preferences, does anyone know how i can just remove the last item added?
private void clearMarker() {
// Opening the editor object to delete data from sharedPreferences
SharedPreferences.Editor editor = sharedPreferences.edit();
// Clearing the editor
editor.clear();
// Committing the changes
editor.commit();
}
}
The above is called with "onClick" via a button. Does anyone know how to accomplish this? All help will be greatly appreciated.