Skip to content
Advertisement

google markers are updating but not removing previous markers (help needed) using Java

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]

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement