How to remove specific markers from a Googlemap

Asked

Viewed 1,315 times

3

So, this app works like this: the user inserts a journey that goes from point C (collection) to point E (delivery).

These points are shown on the map, in the form of markers, as in the figure below.

Exemplo de mapa com marcadores

Now I need to remove the markers by clicking on X.

The general concept is like this: I will make a list containing (String ID, Marker labelDe, Marker labelPara) and each new marker adds a line to this list in the addMarker method().

Dai, the removeMarker(String ID) method will retrieve the markers relative to the ID and remove them from the map when the user deletes the journey.

My research indicated several possible approaches. Could create a class, a list, a hashmap, etc.

I would like opinions on how best to do this and if possible, code examples.

Note that the addition of markers already works and the removal of the record also. And just at this time I will call the removeMarker method().

I think the question can be summarized in: What is the best way to create a list with types String, Marker, Marker?

  • Why not use the registration id in the database table for the id of the Mac?

  • the ID is already the table id. I don’t want to rebuild the map with each deletion. Then, when mounting the map, the app stores a list of each pair of bookmarks and the registration ID to use later, in deletion. My question is precisely, what would be the best way to store these values: creating array, object, hashmap, etc? What do you think?

  • Usually what is used is a Hashmap. Use id as key and, as value, an object of a class that stores the two Marker.

  • 1

    Fair. It worked out.

2 answers

4


The solution was thus.

In the delete button (X) Listener of the recyclerView adapter, we delete the record and then call the method in Mainactivity:

holder.ivDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ...
            ...  
            runDbHelper.deleteRow(runId);
            ((MainActivity) v.getContext()).removeMarkersFromMap(runId);
            ...
            ... 
        }
    });

The removal method in Mainactivity, receives the ID and passes to the fragment:

public  void removeMarkersFromMap(String ID) {
    if (ID != null) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        MarkersFragment mFragment = null;
        if (getResources().getConfiguration().orientation == ORIENTATION_LANDSCAPE) {
            mFragment = (MarkersFragment) fragmentManager.findFragmentById(R.id.markersMapPanel);
        } else if (getResources().getConfiguration().orientation == ORIENTATION_PORTRAIT) {
            mFragment = (MarkersFragment) fragmentManager.findFragmentById(R.id.viewpager);
        }
        if (mFragment != null) {
            mFragment.removeMarkers(ID);
        }
    }

}

This in turn executes the method on the map fragment that excludes the marker(s)).

public void removeMarkers(String id){

    MarkersPair markersPair = markersPairHashMap.get(id);
    if(markersPair != null){
        if(markersPair.collect != null) {
            markersPair.collect.remove();
        }
        if(markersPair.delivery != null) {
            markersPair.delivery.remove();
        }
    }
}

and, finita la comedia!

0

I have a screen that displays some points on a map; according to the user’s action it is necessary to update this map by hiding/displaying some markers. What we did was keep the dots already loaded and control these actions in the javascript.

var marcadores = {}; //seria array de [ID] -> marcadores

...
function LimparMarcadores () {
    for (var i = 0; i < marcadores[id].length; i++) {
        if (marcadores[id][i] != "0")
            marcadores[id][i].setMap(null);
    }

    marcadores[id] = [];
}
  • The question refers to the tags Android and java.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.