简体   繁体   中英

Remove Multiple markers on Map with updated markers

I have this trivial problem. I have Listener method, that listens for markers. When someone changes location it fires listener method which in turn fires onLocationLoaded() method and passes the MarkerOption object, that has all the location and other details. Now my problem is to save this MarkerOption markersReceived object in Marker object with special ID, so that later on I can find them by this id to remove them indvidually from the map when location for the same MarkerOption gets updated. for unique id I have saved it in title of MarkerOption.

So here is my code

final Marker[] markerTrack = new Marker[3];
final String[] idTrack = new String[3];
public void onLocationLoaded(MarkerOptions markersReceived) {
    if(markersReceived != null) {
    /// Check if marker is implemented if yes then remove and overwrite exixting else make new array

        for(int i =0 ; i < markerTrack.length; i++){
            if(idTrack[i] == markersReceived.getTitle()){
                markerTrack[i].remove();
                markerTrack[i] = mMap.addMarker(markersReceived);

            }else{
                //make new marker
                markerTrack[i] = mMap.addMarker(markersReceived);
                idTrack[i] = markersReceived.getTitle();
            }
        }

So here in onLocationLoaded(MarkerOptions markersReceived) method it receives multiple MarkerOptions object, problem is to identify similar objects and update them in map and remove previous marker from the map. this remove thing is bugging me the map gets filled with new and previous markers

PS mMap.clear() doesn't help. Anyways Thanx for the help in advance.

firstly i want to thank u all for your effort. I finaly found out the way to solve my multiple marker, removing them indvidually and updating them.

Here Is the logic that helped me in achieving this

     final Marker[] markerTrack = new Marker[4];
            //final String[] idTrack = new String[3];
            final ArrayList<String> id = new ArrayList<String>();
            id.add("HELLO");
            final int[] i = {0};
 . . . .. . .
....
....
    @Override
 public void onLocationLoaded(MarkerOptions markersReceived) {
   if (markersReceived != null ) {
      if (id.contains(markersReceived.getTitle())) {
          int j = id.indexOf(markersReceived.getTitle());
           markerTrack[j].remove();
           markerTrack[j] = mMap.addMarker(markersReceived);
     } else {
           markerTrack[i[0]+1] = mMap.addMarker(markersReceived);
          id.add(markersReceived.getTitle());
           i[0]++;
      }
   }

But guys I am now encountering one problem...that is my async task contains another async method which takes too much time in loading results from the web...Is there any way to fast the process... Thanx

Your this line of code should be like:

if(idTrack[i].equalsIgnoreCase(markersReceived.getTitle()){

Update and check, Is it working or not?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM