perliedman / leaflet-realtime

Put realtime data on a Leaflet map
ISC License
737 stars 168 forks source link

bringToBack issues #165

Open mivilleb opened 4 years ago

mivilleb commented 4 years ago

If I used this to get the a geojson polygon layer, the bringToBack works when I toggle the layer on and off:

Partial code:

. . .

var vaisigano_catchment = L.geoJson(null ,{
style: polystyle,
zindex: -999,
});

$.getJSON("data/vaisigano_catchment.geojson",function(data){
vaisigano_catchment.addData(data);
});

var map = L.map('map', {
    center: [-13.9,-171.7],
    zoom: 14,
    layers: [osm,obs_status,vaisigano_catchment]
});

. . .

map.on('overlayadd', function(e) {
    vaisigano_catchment.bringToBack();
});

If I use L.realtime instead of L.geojson, the call for bringToBack says it is not a function:

var vaisigano_catchment = L.realtime({
  url: 'data/vaisigano_catchment.geojson',
  crossOrigin: true,
  type: 'json'
}, {
   interval: 5 * 1000,
  onEachFeature: function (feature, latlng) {

   var level = feature.properties.Labels;

  if (level > 0) {

    var polygon = L.polygon(latlng._latlngs, {
      color: 'blue',
      opacity: 0.3,
      fillOpacity: 0.1
    }).addTo(map);
 } else if (level == 1) {

    var polygon = L.polygon(latlng._latlngs, {
      color: 'red',
      opacity: 0.3,
      fillOpacity: 0.1
    }).addTo(map);
 }
 return polygon;
}
});

vaisigano_catchment.on('update', function(e) {
  map.fitBounds(vaisigano_catchment.getBounds(), {maxZoom: 14});

  Object.keys(e.update).forEach(function(id) {
   var level = feature.properties.Labels;

  if (level > 0) {

    var polygon = L.polygon(latlng._latlngs, {
      color: 'blue',
      opacity: 0.3,
      fillOpacity: 0.1
    }).addTo(map);
 } else if (level == 1) {

    var polygon = L.polygon(latlng._latlngs, {
      color: 'red',
      opacity: 0.3,
      fillOpacity: 0.1
    }).addTo(map);
 }
 return polygon;
}.bind(this));
});

var map = L.map('map', {
    center: [-13.9,-171.7],
    zoom: 14,
    layers: [osm,obs_status,vaisigano_catchment]
});

. . .

map.on('overlayadd', function(e) {
    vaisigano_catchment.bringToBack();
});

What do I have to do to make bringtoBack work when using realtime?

perliedman commented 4 years ago

It's correct that the realtime layer does not have bringToFront/bringToBack. It would not be too hard to add those, and maybe also eachLayer would be useful.

Help to add these would be welcome, I no longer actively work with this plugin, so it's not likely I will do it myself.