Bookmark is duplicated when updating User Location

Asked

Viewed 71 times

2

I implemented the Uusuario Location App every 15 seconds.

Variables are global class:

latitudeUsuario longitudeUsuario Marker

Within the method onLocationChanged i select the two location variables, and call the add .

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 the "Userss12" for being a static location is only with a Marker, so far so good.

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

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

  • try using mmap.clear() will clear everything on the map

1 answer

2


Occurs the following:

By invoking the marker.remove(); removes only 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"));
  • Thiago, the problem was that the global variables always kept the markers. You can not create only two , because they will be various, as they are in a radius of 3km.

  • Then use a List<Marker> and remove through a

  • Good...worked out well...

Browser other questions tagged

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