S
S
Sergey questions2018-06-14 00:42:21
JavaScript
Sergey questions, 2018-06-14 00:42:21

How to dynamically display, remove and change markers on a leaflet map?

Hello.

There is such a script for displaying markers on the site:

var markers;
  function reload_marker(map_left,map_right,map_top,map_down) {
    $.getJSON('/json.php', {left:map_left,right:map_right,top:map_top,down:map_down}, function (serv) {
      massiv =[]; // Создаем массив
      
      // Получаем данные из json.php
      serv.features.forEach(function(n) {
        marker = new L.marker([n.geometry.coordinates[0],n.geometry.coordinates[1]]);
        massiv.push(marker); // добавляем marker к массиву
      });
      
    })
    .done(function() {
      // Удаляем старые маркеры
      if(markers) {
         markers.clearLayers();
      }
      // Добавляем новые 
      markers = L.featureGroup(massiv).addTo(map);
    });
  }


The bottom line is that when moving the map / loading the map for the first time / several times in the process of moving (), the function reload_marker (map_left, map_right, map_top, map_down) is executed where map_left, etc. are the boundaries of the map. Only those markers that fall within the boundaries viewed by the user are output from json.

The code above, when executing the function, deletes all markers and loads, adds new ones to the map based on new data from json.php

The problem is that I can’t implement it in such a way that those markers that are currently on the map are not deleted, but the function is used setLatLng, those markers that did not return their json but are currently present on the map have been removed, new ones have been added .. How can I implement such a function, which way to look?
The problem is also that the leaflet_id is constantly changing for the markers..

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2018-06-25
@freeExec

* Tie not to leaflet_id, but to your marker_id, you probably have it with the database there.
* The fact that you want to separate new / old markers is called working with sets that can be combined, intersected, found the difference, etc., look for either.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question