Remove previous Marker and add new Marker in Google Map v2

你离开我真会死。 提交于 2019-12-03 01:31:22

Just creat a new marker object and before adding a new marker, remove the previous one

Marker marker;

MAP.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {

                @Override
                public void onMapLongClick(LatLng arg0) {
                    if (marker != null) {
                        marker.remove();
                    }
                    marker = MAP.addMarker(new MarkerOptions()
                            .position(
                                    new LatLng(arg0.latitude,
                                            arg0.longitude))
                            .draggable(true).visible(true));
                }
            });

EDIT

Do the same for OnMapClick

MAP.setOnMapClickListener(new OnMapClickListener() {
        @Override
        public void onMapClick(LatLng point) {
            // TODO Auto-generated method stub

                if (marker != null) {
                    marker.remove();
                }
            marker = MAP.addMarker(new MarkerOptions()
                    .position(currentPosition)
                    .snippet(
                            "Lat:" + location.getLatitude() + "Lng:"
                                    + location.getLongitude())
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                    .title("ME"));
            Log.e("lat", "" + point);

        }
    });

Just clear the google map before adding marker. Like this:

@Override
public void onMapLongClick(LatLng latLng) {
    googleMap.clear();

    googleMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title(latLng.toString())
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}

Here a simple way You just have to change the position of the marker. Create Global Object as Marker marker;
After that add marker to map like

marker = map.addMarker(markerOptions).position(new Latlng(31.647316, 74.763791));  

And after it use marker.setPosition(newlaLng); where you need to add marker.

At first clear the Google map, then add a new marker. Check the code below

mMap.clear();

//google map marker adding code here
Sagar Devanga

Just reposting the answer of Anthony.

The method signature for addMarker is:

public final Marker addMarker (MarkerOptions options) So when you add a marker to a GoogleMap by specifying the options for the marker, you should save the Marker object that is returned (instead of the MarkerOptions object that you used to create it). This object allows you to change the marker state later on. When you are finished with the marker, you can call Marker.remove() to remove it from the map.

As an aside, if you only want to hide it temporarily, you can toggle the visibility of the marker by calling Marker.setVisible(boolean).

You can find the answer here Remove a marker from a GoogleMap

Please try the blow code :-

// Global Variable...
private Marker mPreviousMarker ;

     @Override
        public boolean onMarkerClick(Marker marker) {
            if (mPreviousMarker != null) {
                mPreviousMarker.remove();

            }
            mPreviousMarker = googleMap.addMarker(new MarkerOptions().position(latLng).icon(bitmapDescriptor));
        }

LatLng :- Your latlong where you want to add and bitmapDescroptor is icon. {Just for understanding}

For the people who have tried the first solution in this question and you get the error of Marker has not been intialized.

Because, we are comparing the marker and null. When we have not even intialized the marker in the first place.

Solution:

Int mMarkerCount = 0; //Global Variable
Marker mMarker; //Global Variable

if(mMarkerCount > 0){
    if(mMarker != null){
        mMarker.remove();              
    }
}

mMarker = mMap.addMarker(new MarkerOptions().position(latLng));
mMarkerCount++;

The Solution for the question:

Int mMarkerCount = 0; //Global Variable
Marker mMarker;

mMap.setOnMapClickListener(new OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latlng) {
        if(mMarkerCount > 0){
            if(mMarker != null){
                mMarker.remove();              
            }
        }

        mMarker = mMap.addMarker(new MarkerOptions().position(latLng));
        mMarkerCount++;  
    }
});

In the beginning, the count is going to be zero. So, the marker remove method will not be called. Once, the marker is intialized and the count is incremented. We can remove the previous marker by the help of the remove method and create a new marker as shown in the code.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!