Android Firebase remove Google Maps markers

2019-06-08 06:21发布

问题:

Trying to make a small Android app that shows the locations of people as Google Maps markers.

when the user presses a button to appear on the map, the app retrieves the users data from the Firebase JSON and shows the users on the map in real time. If a user moves, the marker moves with him (removed and recreated when his location changes).

Everything works great, except that when the user presses to leave the map, his marker remains in all the other phones even though it's deleted from the Firebase JSON.

Can anyone explain how to fix this problem? My code for retrieving the users and create markers in the map:

 ValueEventListener UsersActiveListener = new ValueEventListener() {

            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

                    UsersActive uA = userSnapshot.getValue(UsersActive.class);

                    if (uA.getUserIDcode() != firebaseauth.getCurrentUser().getUid()) {

                        Double uAlatindouble = Double.valueOf(uA.getUserlat());
                        Double uAlonindouble = Double.valueOf(uA.getUserlon());

                        LatLng uALTLG;

                        if (uAmarker != null) {
                            uAmarker.remove();
                        }

                        uALTLG = new LatLng(uAlatindouble, uAlonindouble);
                        MarkerOptions markerOptions = new MarkerOptions();
                        markerOptions.position(uALTLG);
                        markerOptions.title(uA.getUsername());
                        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.usermarker));
                        uAmarker = mMap.addMarker(markerOptions);

                    }
                }
                }

My code for entering or leaving the map (by pressing two FABs: goActive, goNotActive):

 public void goActiveFAB (View view){

        isActive = true;
        FABgoActive.setVisibility(GONE);
        FABDontgoActive.setVisibility(View.VISIBLE);

        // user in map class

        UsersActive CurrentUserActive = new UsersActive();

        CurrentUserActive.setUserIDcode(firebaseauth.getCurrentUser().getUid());
        CurrentUserActive.setUserlat(String.valueOf(mLastLocation.getLatitude()));
        CurrentUserActive.setUserlon(String.valueOf(mLastLocation.getLongitude()));
        CurrentUserActive.setUsername(currentUser.getNickname());

        groupPosShareRef.child(firebaseauth.getCurrentUser().getUid()).setValue(CurrentUserActive);

    }

    public void goNotActiveFAB (View view){

        isActive = false;
        FABgoActive.setVisibility(View.VISIBLE);
        FABDontgoActive.setVisibility(View.GONE);

        groupPosShareRef.child(firebaseauth.getCurrentUser().getUid()).removeValue();


    }

Thanks!

回答1:

Use ChildEventListener instead, then map each marker to each user's id.

Map<String, Marker> markers = new HashMap();

ref.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
        UsersActive uA = dataSnapshot.getValue(UsersActive.class);

        // ...

        Marker uAmarker = mMap.addMarker(markerOptions);
        markers.put(dataSnapshot.getKey(), uAmarker);
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {
        UsersActive uA = dataSnapshot.getValue(UsersActive.class);

        // ...

        if (markers.contains(dataSnapshot.getKey())) {
            Marker marker = markers.get(dataSnapshot.getKey());

            marker.remove();
            // or
            // marker.setPosition(newPosition);
        }

        Marker uAmarker = mMap.addMarker(markerOptions);
        markers.put(dataSnapshot.getKey(), uAmarker);
    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {
        if (markers.contains(dataSnapshot.getKey())) {
            Marker marker = markers.get(dataSnapshot.getKey());
            marker.remove();
        }
    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});