0

I'm trying to build a tracking driver app using firebase.

I've retrieved all driver data correctly and I make on everyone a marker but I have to know how can I deleted if one of the drivers changes his position to Update it.

My code in Js:

var n=0;
var leadsRef = database.ref('Tracking');
leadsRef.on('value', function(snapshot) {

    snapshot.forEach(function(childSnapshot) {
      var childData = childSnapshot.val();

      console.log(" tracking The updated Chauffeur nb " +n +" " + childData.email);
      console.log(" tracking The updated Chauffeur nb " +n+ " "+ childData.latitude);
      console.log(" tracking The updated Chauffeur nb " +n+ " "+ childData.longitude);

      L.marker([parseFloat(childData.latitude), parseFloat(childData.longitude)], {icon: greenIcon}).addTo(mymap).bindPopup("I am nb "+childData.email+" hi");
    n++;
    });
});
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
aaala
  • 13
  • 1
  • 7

2 Answers2

0

You could use the child_removed event to detect when a child of the Tracking node is removed.

I don't know leaflet and therefore i don't know how to remove a marker in your case, but I guess there is a specific method for that and, therefore, you could do something along the following lines:

ref.on('child_removed', function(oldChildSnapshot) {
  var oldChildData = oldChildSnapshot.val();

  L.removeMarker(...)   //Just inventing a method name here!
});
Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
0

Get the key and then use remove() to delete:

snapshot.forEach(function(childSnapshot) {
      var childData = childSnapshot.val();
      var key = childSnapshot.key;
     database.ref('Tracking').child(key).remove();     
    });
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • what I need exactly not to delete firebase node or child but I need wheen node or value from my firebase real-time database changes or removed I need to initialise my map(delete all Markers) for update markers positions(add new Markers) – aaala Jan 06 '20 at 11:42
  • Then check the duplicate – Peter Haddad Jan 06 '20 at 11:43
  • but I don't see any posts about how to remove markers with firebase :| – aaala Jan 06 '20 at 11:48
  • https://stackoverflow.com/questions/9912145/leaflet-how-to-find-existing-markers-and-delete-markers – Peter Haddad Jan 06 '20 at 11:51