GoogleMaps v2 for Android: unable to remove marker while map is showing

I have a snippet with:

  • snippet googlemap v2
  • Places button to add markers (user places)
  • Remove Places button (remove custom markers)
  • Additional markers (for example, general point of interest)
  • Custom tile sketch

When the user clicks the Places button, the application saves the hashmap with the token references and places the object in the WeakHashMap file. When the user clicks Remove Places, the application iterates over the map keys that call marker.remove ().

When the map is fully displayed, the markers are removed as expected, but if the button is pressed while the map is displayed, the markers are not removed.

Has anyone had the same problem? How to solve it?

I cannot use map.clear () as it removes all markers, polylines, overlays, etc. I just want to delete the previously saved list of markers (custom places) not all.

+3


source to share


3 answers


The problem was that the object to store the relationship between the Marker object and the Place should not be a WeakHashMap, but a HashMap. He solved the problem.



+1


source


You can use multiple gates to check if the load map has run out. If you don't delay removing markers until it is. Here's an example:



private boolean mLoadFinished, mDelayRemove;

public void removeMarkers() {
    for (Marker marker : markers) {
        marker.remove();
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    Button button;
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!mLoadFinished) {
                mDelayRemove = true;
            } else {
                removeMarkers();
            }
        }
    });

    mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
        @Override
        public void onMapLoaded() {
            mLoadFinished = true;
            if (mDelayRemove) {
                mDelayRemove = false;
                removeMarkers();
            }
        }
    });

    ...
}

      

+3


source


GoogleMAp.clear () will remove all the marker you have drawn on the map.

+1


source







All Articles