1

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.

MichaelCS
  • 51
  • 4

1 Answers1

0

The method signature for addMarker is:

public final Marker addMarker (MarkerOptions options)

It returns the Marker object that is created on the map.

In your case, replace

mMap.addMarker(markerOpt1);

with

Marker marker = mMap.addMarker(markerOpt1);

You can then store this Marker object in your SharedPreferences instead of storing the latitude and longitude. Check this answer for instructions.

This enables you to call Marker.remove() on the Marker object you retrieve from the SharedPreferences inside your clearMarker() method to remove that particular marker from the map.

Community
  • 1
  • 1
Sajal Narang
  • 397
  • 1
  • 14
  • Can you explain what exactly i need to change or add? Or the steps I need to follow to achieve this? I am still developing skills in programming/android and have a long way to go. Any hints in the correct direction would be greatly appreciated. – MichaelCS May 07 '17 at 12:24
  • I've edited the answer to include more details. Hope it helps :) – Sajal Narang May 07 '17 at 19:29