How to remove a specific marker in Google Google Map

THE CODE:

private Marker mCurrentMarker;
private ArrayList<Marker> mMarkerArrayList;

@Override
    public void onMapReady(final GoogleMap googleMap) {
        mGoogleMap = googleMap;

        mMarkerArrayList = new ArrayList<>();
        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng point) {

                MarkerOptions marker_onclick = new MarkerOptions().position(
                        new LatLng(point.latitude, point.longitude)).title(getString(R.string.now_your_location));
                if (mMarkerArrayList.size() > 0){
                    Marker marker_to_remove = mMarkerArrayList.get(0);
                    marker_to_remove.remove();
                }

                mCurrentMarker = mGoogleMap.addMarker(marker_onclick);

                mGoogleMap.addMarker(marker_onclick);
                mMarkerArrayList.add(mCurrentMarker);

            }
        });
    }

I want that by clicking on the map, a marker will appear associated with the display of the clicked location. And the marker that was before the deletion. Thus, there is only one marker associated with displaying a clicked location.

I already know mGoogleMap.clean();can clear the map, as well as the markers on the map.

But I want to remove a specific marker. (Because there are many kinds of markers in my application. For example, a home marker shows where the user’s home is, and a bus stop marker indicates where the bus stop is.)

So I made an ArrayList and tried to use it.

But that did not work.

I think when I click on the map it addmarker();works fine, but .remove();it doesn't seem to work.

Where is the mistake?

?

: Google v2?

+4
3

, HashMap :

 HashMap<YourUniqueKey,Marker> hashMapMarker = new HashMap<>();
 Marker marker = googleMap.addMarker(markerOptions);
 hashMapMarker.put(YourUniqueKey,marker);

, , Maker YourUniqueKey :

Marker marker = hashMapMarker.get(YourUniqueKey);
marker.remove();
hashMapMarker.remove(YourUniqueKey);
+10

setOnMarkerClickListener, !

:

mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
    @Override
    public boolean onMarkerClick(Marker marker) {

        marker.remove();
        return true;
    }
});

"", .

+2

. arrayList.

val markerArrayList = ArrayList<Marker>()

    // set map click listener
    googleMap?.setOnMapClickListener {

        if (markerArrayList.size > 0) {
            val markerToRemove = markerArrayList.get(0)

            // remove the maker from list
            markerArrayList.remove(markerToRemove)

            // remove the marker from the map
            markerToRemove.remove()
        }

        // Marker options
        val markerOptions = MarkerOptions().position(it).draggable(true)

        // adds marker to the clicked point
        val currentMarker = googleMap.addMarker(markerOptions)

        // Add current marker to array list
        markerArrayList.add(currentMarker)

        // Set default icon
        currentMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED))

        // get the location of the clicked point
        val clickedPointLatitude = it.latitude
        mClickedPointLongitude = it.longitude
    }
0
source

Source: https://habr.com/ru/post/1659908/


All Articles