Remove marker when adding a new one

Asked

Viewed 210 times

2

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

    @Override

    public void onMapClick(LatLng point) {


        MarkerOptions marker = new MarkerOptions().position(
                new LatLng(point.latitude, point.longitude)).title("New Marker");

        mMap.addMarker(marker);

        System.out.println(point.latitude+"---"+ point.longitude);
    }
});}}

in case this code adds a new marker on the map, plus each click I give adds a new marker, then I want to Remove the old marker when add a new one

  • Newbie, Voce already tried mMap.clear()

  • Newbie, put the solution on the record, it helps the next ones to search.

  • In this case it would not be better to create the marker once and move when clicking?

1 answer

0


mmap.setOnMapLongClickListener(new Googlemap.Onmaplongclicklistener() {

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.