enter code here // counter for online drivers... var loc_count = 0; // markers array to store all the markers, so that we could remove marker when any drivers goes offline and its data will be remove from realtime database... var markers = []; var map; function initMap() { // Google Map Initialization... map = new google.maps.Map(document.getElementById('map'), { zoom: 10, center: new google.maps.LatLng(1.558490, 103.638183), //map centered at places // mapTypeId: 'terrain' }); } // This Function will add/display that marker on the map function AddDriver(data) {
var places = { lat: data.val().latitude, lng: data.val().longitude };
var marker = new google.maps.Marker({
position: places,
map: map
});
markers[data.key] = marker; // add marker in the markers array...
document.getElementById("loc").innerHTML = loc_count;
}
// get firebase database reference...
var locationsRef = firebase.database().ref().child("lastOnline");
// this event will be triggered when a new object will be added in the database...
locationsRef.on("child_added", function (data) {
loc_count++;
AddDriver(data);
});
// this event will be triggered on location change of any drivers...
locationsRef.on('child_added', function (data) {
markers[data.key].setMap(null);
AddDriver(data);
});
// If any driver goes offline then this event will get triggered and we'll remove the marker of that driver...
//var locationsRef = firebase.database().ref().child("email");
locationsRef.on('child_removed', function (data) {
markers[data.key].setMap(null);
loc_count--;
document.getElementById("loc").innerHTML = loc_count;
});
Aucun commentaire:
Enregistrer un commentaire