0

How can i remove the old markers but still it can update my latest location in the map

@Override public void onMapReady(GoogleMap googleMap) {

    locationListener = new LocationListener() {


        @Override
        public void onLocationChanged(@NonNull Location location) {

            latLng = new LatLng(location.getLatitude(), location.getLongitude());
            googleMap.addMarker(new MarkerOptions().position(latLng).flat(true).title("My Position").icon(BitmapDescriptorFactory.fromResource(R.drawable.marker1)));
            googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        }
    };

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    try {
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME, MIN_DIST, locationListener);

    }
    catch (SecurityException e){
        e.printStackTrace();
    }


}[enter image description here][1]
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Java and Javascript are not the same thing – Cooper Feb 15 '22 at 17:31
  • You'r not updating the marker, you just add another marker. I see two possible solution. The first, you keep your code like this and just add a line to remove the old marker (something like `Markers.remove(name)` ). the second option is to update the location of the marker using the `setOption()` method. Maybe [this post](https://stackoverflow.com/questions/13692398/remove-a-marker-from-a-googlemap) can help you – PaulCrp Feb 17 '22 at 00:30
  • so, add Markers.remove(name) , and the name should be ? – Murali Dharan Feb 17 '22 at 13:21

1 Answers1

1

Try use googleMap.clear(); before googleMap.addMarker(); maybe it help you

ACTPOHOM
  • 93
  • 1
  • 6