Добавление и удаление определенного маркера из нескольких маркеров в Google Maps JavaScript (Typescript) с помощью Firebase - PullRequest
0 голосов
/ 02 июля 2019

У меня есть массив, содержащий список имен узлов.Эти имена узлов являются узлами в Firebase.В Firebase каждый узел содержит дочерние узлы с долготой и широтой.Мне нужно подписаться на Firebase, для каждого элемента массива, для каждого узла, затем извлечь долготу и широту в этом узле и отобразить это на Google Maps в качестве маркеров.Код работает нормально при первой загрузке, за исключением того, что когда Firebase отправляет обратно измененное значение любого узла, я не могу определить, для какого маркера он отправлен, и, следовательно, не могу удалить и добавить определенный маркер из всех множественныхмаркеры отображаются через массив маркеров.Ниже приведен код с комментариями:

initMap() {
  // Below is the array holding the nodes
  let vehiclesarray = [
    dl3c2424,
    hr29b4343,
    dl9945,
    dl6574
  ];

  // Get a reference to the database service
  let database = firebase.database();

  let markers: any = [];
  let locations: any = [];
  let i = 0;

  // Initial map is loaded and centered at defined location co-ordinates
  let map = new google.maps.Map(document.getElementById('map'), {
    zoom: 14,
    center: {lat:28.4595, lng:77.0266}
  });


    // The array (containing the nodes) is looped so as to get the location of the corresponding nodes from the firebase db.
    // Each node is subscribed to in Firebase so as to receive the regular notification in real time
    // For each location the marker is added and the marker is to be removed right before it is added

    vehiclesarray.forEach(element => {
      this.subscription = this.db.object('/' + element.vehicleid).valueChanges()
      .subscribe (vehicleLocations => {
        this.vehicleLocations = vehicleLocations;
        let location = {
          lat: this.vehicleLocations.latitude,
          lng: this.vehicleLocations.longitude
        };
// This is the place where I try to remove the markers but it doesn't work as the code ends up removing all but last marker (because of the loop)
// I have removed the marker removal line here and wrote this comment instead 
        markers[i] = new google.maps.Marker({
          position: location,
          map: map
        });

        i++;
      },
        err => console.error(err)
      );
    });
}

Любая помощь или предложение или указатели приветствуются Спасибо Ritesh

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...