Emilio Gaines
Emilio Gaines

Reputation: 95

Google Maps API Remove stored markers

I am saving Markers that I put in the map using an array however I would like a AlertDialog showing up asking me if I want to delete the marker in question when I tap a Marker but how would I do that? this is the code I am using for storing markers

            sharedPreferences = getSharedPreferences("location", 0);

        // Getting number of locations already stored
        locationCount = sharedPreferences.getInt("locationCount", 0);

        // Getting stored zoom level if exists else return 0
        String zoom = sharedPreferences.getString("zoom", "0");

        // If locations are already saved
        if(locationCount!=0){

            String lat = "";
            String lng = "";
            String title = "";
            String snippet = "";

            // 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");

                String textstored = sharedPreferences.getString("title"+i,"0");

                String snippetstored = sharedPreferences.getString("snippet"+i,"0");

                // Drawing marker on the map
                googleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)))
                        .title(textstored)
                        .snippet(snippetstored)
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

            }
    }

and

      googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {



        @Override
        public void onMapLongClick(final LatLng point) {
            AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(MainActivity.this);
            builder.setTitle("Titel:");

            // Set up the input
            final EditText input = new EditText(MainActivity.this);
            builder.setView(input);

            // Set up the buttons
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    AlertDialogWrapper.Builder builderinfo = new AlertDialogWrapper.Builder(MainActivity.this);
                    builderinfo.setTitle("Info:");

                    // Set up the input
                    final EditText inputinfo = new EditText(MainActivity.this);
                    builderinfo.setView(inputinfo);

                    // Set up the buttons
                    builderinfo.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            locationCount++;

                            // Drawing marker on the map
                            String text = input.getText().toString();
                            String textinfo = inputinfo.getText().toString();
                            googleMap.addMarker(new MarkerOptions()
                                    .position(point)
                                    .title(text)
                                    .snippet(textinfo)
                                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

                            /** 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(point.latitude));

                            // Storing the longitude for the i-th location
                            editor.putString("lng"+ Integer.toString((locationCount-1)), Double.toString(point.longitude));

                            editor.putString("title"+(locationCount-1), text);
                            editor.putString("snippet"+(locationCount-1),textinfo);

                            // Storing the count of locations or marker count
                            editor.putInt("locationCount", locationCount);


                            /** Saving the values stored in the shared preferences */
                            editor.commit();

                            Toast.makeText(getBaseContext(), "En Markör lades till framgångsrikt", Toast.LENGTH_SHORT).show();


                        }
                    });

Upvotes: 0

Views: 158

Answers (1)

nPn
nPn

Reputation: 16728

In the same class that you implement onMapLongClick implement OnMarkerClickListener

As such:

public class MyMapView implements OnMarkerClickListener { 

  ...
  @Override
  public boolean onMarkerClick(Marker marker) {
    // your code here
    return true; 
  }
...
}

Then in the onMarkerClick(Marker marker) you can remove the marker.

you remove the saved marker from the shared preferences using

editor.remove(String key);
editor.commit();

and from the map using:

marker.remove()

As far as the alert, I would suggest you take a look at the development guide on Dialogs

Upvotes: 1

Related Questions