Marker is duplicating when updating User Location

2

Implemented the Uusuario Localization activity every 15 seconds.

Variables are global class:

LatitudeUsers UserLength marker

Within the onLocationChanged method I set the two location variables, and call the addMarqueries.

private void adicionarMarcadores() {

        if(marker != null){
            marker.remove();

        }

        marker = mMap.addMarker(new MarkerOptions().position(new LatLng(latitudeUsuario, longitudeUsuario)).title("Minhaaa"));
        marker = mMap.addMarker(new MarkerOptions().position(new LatLng(-23.618439,-46.605477)).title("Userss12"));


       mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitudeUsuario, longitudeUsuario), 18.0f));

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        mMap.setMyLocationEnabled(true);
    }

The problem is that "Userss12" because it is a static location is only with a Bookmark, so that's fine.

But "Minhaaa" is repeating the bookmark every time it updates!

leaving a lot of marker on the map ....

    
asked by anonymous 09.08.2016 / 15:24

1 answer

2

The following occurs:

Invoking marker.remove(); removes just the last reference!

To solve, create two marker's:

if(markerUm != null){
        markerUm.remove();
 }
if(markerDois != null){
        markerDois.remove();
 }

 markerUm = mMap.addMarker(new MarkerOptions().position(new LatLng(latitudeUsuario, longitudeUsuario)).title("Minhaaa"));
 markerDois = mMap.addMarker(new MarkerOptions().position(new LatLng(-23.618439,-46.605477)).title("Userss12"));
    
09.08.2016 / 23:24