У меня проблема с моим проектом.
Я хочу удалить определенный маркер карты Google, когда я (например) нажимаю кнопку для удаления. Но я хочу удалить маркер, который я выберу в своем приложении, а не все маркеры. Моя база данных выглядит так: База данных Firebase
И все, что мне нужно, это то, что если я сделаю маркер активным (возможно, когда я нажму на него), я смогу удалить этот маркер из базы данных и с карты в моем приложении. Код, который добавляет маркер:
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);
}
Можно ли вообще сделать что-то подобное? Спасибо за все ответы.