-2

How can I know from my Marker object if it has been removed from the Google Map. I am not interested is show/hide. I am interested in "remove()". Even after marker.remove() the isVisible is true for that marker. So how do I know?

Some guy has asked for a sample code to reproduce. Here it is:

override fun onMapReady(googleMap: GoogleMap) {
    this.googleMap = googleMap

    val markerOptions = MarkerOptions()
    markerOptions.position(LatLng(31.520959,74.352154))
  markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.curr_loc_grey))
    val marker = googleMap.addMarker(markerOptions)
   /........../
    marker.remove()
}

I am using

api 'com.google.maps:google-maps-services:0.10.2'

When I am debugging, I see marker.isVisible is true when it is added, and it stays true after remove(). Is it a bug?

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69
  • This cannot be true. – MrUpsidown Mar 10 '20 at 15:20
  • @MrUpsidown what cannot be true? isVisible? well it is. Did u verify? – M. Usman Khan Mar 11 '20 at 13:38
  • Verify? With what? You must provide a [mcve] so that it could be verified. According to the docs, your statements cannot be true. You have 2 close votes for the same reason and haven't updated your question so far. – MrUpsidown Mar 11 '20 at 17:52
  • @MrUpsidown Where is it in the documentation? it is simple marker.remove(). i have added and tested the code for u as well. Take a look – M. Usman Khan Mar 11 '20 at 19:00
  • 2
    *`public void remove ()` Removes this marker from the map. After a marker has been removed, the behavior of all its methods is undefined.* – MrUpsidown Mar 11 '20 at 19:04
  • @MrUpsidown yea so the behavior is "undefined" and it does not say that isVisible will be false, which u were saying "this cannot be true" – M. Usman Khan Mar 11 '20 at 19:09

1 Answers1

0

Anyway, you can put markers to HashSet at the same time that you add it to map:

...
HashSet<Makrer> markersOnMap = new HashSet<>();
...

...
// when add marker to map
Marker marker = googleMap.addMarker(markerOptions);
markersOnMap.add(marker);
...

...
// when remove
markersOnMap.remove(marker);
marker.remove();
...

...
// when check is marker on map
if (markersOnMap.contains(marker)) {
    // marker is on map   
} else {
    // marker removed   
}

Of course, this is just approach description, not final code.

Update:

You can create custom MapView/MapFragment-based component and inherit HashSet interactions. Something like that:

public class MarkesrMapView extends MapView implements OnMapReadyCallback {

    private OnMapReadyCallback mMapReadyCallback;
    private GoogleMap mGoogleMap;
    private HashSet<Makrer> mMarkersOnMap;

    public MarkersMapView(@NonNull Context context) {
        super(context);
        init();
    }

    public MarkersMapView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MarkersMapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public MarkersMapView(@NonNull Context context, @Nullable GoogleMapOptions options) {
        super(context, options);
        init();
    }

    private void init() {
        mMarkersOnMap = new HashSet<>();
    }

    @Override
    public void getMapAsync(OnMapReadyCallback callback) {
        mMapReadyCallback = callback;
        super.getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;
        if (mMapReadyCallback != null) {
            mMapReadyCallback.onMapReady(googleMap);
        }
    }

    public Marker addMarker(MarkerOptions markerOptions) {
        Marker marker = mGoogleMap.addMarker(markerOptions);
        mMarkersOnMap.add(marker);
        return marker;
    }

    public void removeMarker(Marker marker) {
        if (marker == null) return;
        mMarkersOnMap.remove(marker);
        marker.remove();
    }

    public boolean isOnMap(Marker marker) {
        if (marker == null) return false;
        return mMarkersOnMap.contains(marker);
    }
}

And use it like:

...
private markersMapView mMapView;
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Bundle mapViewBundle = null;
    if (savedInstanceState != null) {
        mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
    }

    mMapView = (MarkersMapView) findViewById(R.id.mapview);
    mMapView.onCreate(mapViewBundle);
    mMapView.getMapAsync(new OnMapReadyCallback() {
        @Override
        public void onMapReady(GoogleMap googleMap) {
            mGoogleMap = googleMap;
            mMapView.addMarker(new MarkerOptions().position(...).title("Marker"));
        }
    });

   ...
   if (mMapView.isOnMap(myMarker)) {
      ...
   }

It can be very nice simple call of .isOnMap() (especially if there is no other way).

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79