我的项目有问题。 当我(例如)按下删除按钮时,我想删除特定的谷歌地图标记。但我想删除我将在我的应用程序中选择的标记,而不是所有标记。我的数据库看起来像这样:Firebase Database
我需要的是,如果我使标记处于 Activity 状态(可能是在我单击它时),我将能够从我的应用程序的数据库和 map 中删除该标记。添加标记的代码:
btnPridatKontejner = (Button)findViewById(R.id.btn_pridat_kontejner);
btnPridatKontejner.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final AlertDialog.Builder dialog = new AlertDialog.Builder(Welcome.this);
dialog.setTitle("Přidat kontejner");
dialog.setMessage("Prosím, zadejte typ kontejneru pro přidání");
LayoutInflater inflater = LayoutInflater.from(Welcome.this);
View info_layout = inflater.inflate(R.layout.layout_info_map, null);
final MaterialEditText edtTypKontejneru = info_layout.findViewById(R.id.edtTypKontejneru);
dialog.setView(info_layout);
dialog.setPositiveButton("Přidat kontejner", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
final double latitude = mLastLocation.getLatitude();
final double longitude = mLastLocation.getLongitude();
MarkerOptions mp = new MarkerOptions();
long date = System.currentTimeMillis();
SimpleDateFormat sdf = new SimpleDateFormat("dd. MM. yyyy, k:mm");
String dateString = sdf.format(date);
mp.position(new LatLng(latitude, longitude));
mp.title(edtTypKontejneru.getText().toString());
mp.snippet(dateString);
mMap.addMarker(mp);
DatabaseReference markerRef = FirebaseDatabase.getInstance().getReference("markers");
String key = markerRef.push().getKey();
markerRef.child(key).child("long").setValue(longitude);
markerRef.child(key).child("lat").setValue(latitude);
markerRef.child(key).child("title").setValue(mp.getTitle());
markerRef.child(key).child("snippet").setValue(mp.getSnippet());
if (TextUtils.isEmpty(edtTypKontejneru.getText().toString())){
return;
}
}
});
dialog.setNegativeButton("Zavřít", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
dialog.show();
}
}
这是我从 Firebase 加载标记到我的应用程序的代码:
mMap = googleMap;
//Disable Map Toolbar:
//map.getUiSettings().setMapToolbarEnabled(false);
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef
.child("markers");
ValueEventListener eventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String latitude_Display = ds
.child("lat")
.getValue().toString();
String longitude_Display = ds
.child("long")
.getValue().toString();
String title_Display = ds
.child("title")
.getValue().toString();
String info_Display = ds
.child("snippet")
.getValue().toString();
String latLng = latitude_Display;
String latLng1 = longitude_Display;
String title = title_Display;
String info = info_Display;
double latitude = Double.parseDouble(latLng);
double longitude = Double.parseDouble(latLng1);
String titleInfo = title;
String infoInfo = info;
LatLng currentLocation = new LatLng( latitude, longitude );
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position( currentLocation );
mMap.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(titleInfo).snippet(infoInfo));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
usersRef.addListenerForSingleValueEvent(eventListener);
}
有没有可能做出那样的东西?感谢您的所有回答。
最佳答案
void del(String key) {
DatabaseReference usersRef = rootRef.child("markers").child(key).removeValue();
}
传递要删除的标记的键。
关于Android - 从 firebase 数据库中删除 "active"标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040751/