我遇到了麻烦。我在Realtime Firebase中获取经纬度并创建了一个标记,但每次纬度和经度都有更新时,它将创建另一个标记。
如何删除上一个标记?这是我的代码:
passRef = new Firebase(Config.FIREBASE_URL_PASSENGER);
passRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapShot : dataSnapshot.getChildren()) {
for (int cnt = 0; cnt <= dataSnapshot.getChildrenCount(); cnt++) {
if (snapShot.child("latitude").exists() && snapShot.child("longitude").exists() && snapShot.child("destination").exists()) {
passUsername = snapShot.child("name").getValue().toString();
//getting the passenger's current location
locLatPass = Double.valueOf(snapShot.child("latitude").getValue().toString());
locLongPass = Double.valueOf(snapShot.child("longitude").getValue().toString());
latlngpass = new LatLng(locLatPass, locLongPass);
MarkerOptions passLocMarkerOptions = new MarkerOptions();
passLocMarkerOptions.position(latlngpass);
passLocMarkerOptions.title(passUsername+ "'s Location");
passLocMarkerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
passLocationMarker = mMap.addMarker(passLocMarkerOptions);
} else {
}
}
Toast.makeText(MapsActivity.this, "Passenger Username" + passUsername + " latitude" + locLatPass + "Longitude" + locLongPass, Toast.LENGTH_LONG).show();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
答案 0 :(得分:3)
这将清除地图上绘制的所有现有标记:
mMap.clear();
答案 1 :(得分:1)
您可以通过调用remove()
方法删除标记。
marker.remove();
修改强>
如果每个孩子的价值都会发生变化,您应该考虑使用ChildEventListener
以下是您的问题的解决方案,您必须创建一个HashMap来保存标记,然后您可以删除或只修改上一个标记。
Map<String, Marker> markers = new HashMap();
Database.waypoints().addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
passUsername = dataSnapshot.child("name").getValue().toString();
//getting the passenger's current location
locLatPass = Double.valueOf(dataSnapshot.child("latitude").getValue().toString());
locLongPass = Double.valueOf(dataSnapshot.child("longitude").getValue().toString());
latlngpass = new LatLng(locLatPass, locLongPass);
MarkerOptions passLocMarkerOptions = new MarkerOptions();
passLocMarkerOptions.position(latlngpass);
passLocMarkerOptions.title(passUsername+ "'s Location");
passLocMarkerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
Marker marker = mMap.addMarker(passLocMarkerOptions);
markers.put(dataSnapshot.getKey(), marker);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
passUsername = dataSnapshot.child("name").getValue().toString();
//getting the passenger's current location
locLatPass = Double.valueOf(dataSnapshot.child("latitude").getValue().toString());
locLongPass = Double.valueOf(dataSnapshot.child("longitude").getValue().toString());
latlngpass = new LatLng(locLatPass, locLongPass);
MarkerOptions passLocMarkerOptions = new MarkerOptions();
passLocMarkerOptions.position(latlngpass);
passLocMarkerOptions.title(passUsername+ "'s Location");
if (markers.contains(dataSnapshot.getKey())) {
markers.get(dataSnapshot.getKey()).remove();
// you can also modify the marker instead of removing it and then add it again
}
passLocMarkerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
Marker marker = mMap.addMarker(passLocMarkerOptions);
markers.put(dataSnapshot.getKey(), marker);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});