Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove specific marker on Android GoogleMap

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 when I click on the map, there will be a marker related in showing clicked location. And marker which has been before being removed. So, there is only one marker related to showing clicked location.

I already know mGoogleMap.clean(); can clean map, also markers on the map.

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

So I made ArrayList and tried to use it.

But it didn't work.

I think when i click on map, addmarker(); is working well but .remove(); seems to be not working.

Where is the error?

How can I remove specific marker only?

I read: How to remove the marker in Google map v2?

like image 289
touchingtwist Avatar asked Nov 05 '16 08:11

touchingtwist


People also ask

How do you remove a specific marker?

Inside the Info Window of the marker, there's an HTML button (Delete) which when clicked triggers the DeleteMarker JavaScript function. This function accepts the ID of the marker as a parameter and using this ID the selected marker is removed from the Map. };

How do I hide other markers on Google Maps?

Step 1 Go to Add or Edit Map and Scroll down to the 'Infowindow Settings' section. Step 2 Enable the box of 'Hide Markers on Page Load' option. Step 3 Click on Save Map and open it in browser.

How do I change the marker on Google Maps Android?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.


1 Answers

When you add a marker on Map, you can store it into HashMap like this:

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

At the time you want to delete particular marker just get your Maker by YourUniqueKey for that marker like this:

Marker marker = hashMapMarker.get(YourUniqueKey);
marker.remove();
hashMapMarker.remove(YourUniqueKey);
like image 158
Manoj Singh Rawal Avatar answered Nov 19 '22 23:11

Manoj Singh Rawal