gpt4 book ai didi

Android Firebase 删除谷歌地图标记

转载 作者:行者123 更新时间:2023-11-30 00:58:16 24 4
gpt4 key购买 nike

尝试制作一个小型 Android 应用,将人们的位置显示为 Google map 标记。

当用户按下按钮出现在 map 上时,应用程序会从 Firebase JSON 中检索用户数据,并在 map 上实时显示用户。如果用户移动,标记也会随之移动(当他的位置发生变化时,标记会被移除并重新创建)。

一切正常,除了当用户按下离开 map 时,他的标记会保留在所有其他手机中,即使它已从 Firebase JSON 中删除。

谁能解释一下如何解决这个问题?我用于检索用户并在 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);

}
}
}

我的进入或离开 map 的代码(通过按两个 FAB:goActivegoNotActive):

 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();


}

谢谢!

最佳答案

改为使用 ChildEventListener,然后将每个标记映射到每个用户的 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) {

}
});

关于Android Firebase 删除谷歌地图标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39821199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com