简体   繁体   English

实时更新多边形传单

[英]Update polygon leaflet realtime

I am trying to display a polygon from an external geojson file, the data loads but does not update the polygon in real time.我试图从外部 geojson 文件显示多边形,数据加载但不实时更新多边形。

The polygon is added but color is not updated after interval when level changes.当级别改变时,多边形被添加但颜色不会在间隔后更新。

Heres is my code:这是我的代码:

L.realtime({
 url: 'js/areas.json',
 crossOrigin: true,
 type: 'json'
}, {
interval: 60 * 1000,
onEachFeature: function (feature, latlng) {

 var level = feature.properties.level;

 if (level == 0) {

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

var polygon = L.polygon(latlng._latlngs, {
  color: '#F43B19',
  opacity: 0.3,
  fillOpacity: 0.1
 }).addTo(map);
}
return polygon;
},
updateFeature: function (feature, oldLayer, newLayer) {

 var level = feature.properties.level;
 if (!oldLayer) {
 return;
 }

 if (level== 0) {
  oldLayer.setStyle({color: '#51F03B'});
 } else if (level == 1) {
  oldLayer.setStyle({color: '#F43B19'});
 }
 return oldLayer;
 }
});

If i don´t return oldLayer , the polygon color changes but don´t remove the previous polygon.如果我不return oldLayer ,则多边形颜色会发生变化,但不会删除前一个多边形。

geoJson file:地理JSON文件:

{
"type": "FeatureCollection",
"features": [{
    "type": "Feature",
    "properties": {
        "level": 0,
        "id": 1
    },
    "geometry": {
        "type": "Polygon",
        "coordinates": [[
            [-75.360297, 6.071571],
            [-76.005083, 6.063846],
            [-76.051694, 6.511708],
            [-75.298149, 6.573451]
        ]]
    }
}]
}

I show markers and more in this way but I don't know if being polygons is different.我以这种方式显示标记等,但我不知道多边形是否不同。

The way I worked with "real-time" with polygon was cleaning the previous polygon and creating a new one.我使用多边形进行“实时”处理的方式是清理前一个多边形并创建一个新的多边形。 With that in mind, you will need to keep track of the layers that you have created (like in an array), a method to clear that layer (or clear all layers, there's a leaflet method for that ) and a method to set a timeOut to call an update method.考虑到这一点,您将需要跟踪您创建的层(如在数组中)、清除该层的方法(或清除所有层,有一个传单方法)和一种设置超时调用更新方法。

I say "real-time" because currently, I keep asking back-end for an update using a timeOut function.我说“实时”是因为目前,我一直在使用timeOut函数要求后端进行更新。

first, when you received the geojson draw the polygon, add it to your map and call the setTimeout with your update method.首先,当您收到 geojson 绘制多边形时,将其添加到您的地图并使用您的更新方法调用 setTimeout。

second, you will need a method to remove the old layer, something like this:其次,您将需要一种删除旧层的方法,如下所示:

const resetPolygonArray = polygonId => { 
    myPolygon = polygonArray.filter(polygon => {
        if (polygon.id != polygonId) {
            return myPolygon
        } else {
        map_machiney.removeLayer(myPolygon.geojson)
        }
    })
}

even though you can use that array to store the polygon and the marker related to it, like this structure:即使您可以使用该数组来存储多边形和与其相关的标记,如以下结构:

polygonArray.push({
    id: polygonId,
    geojson: geojson,
    marker: marker
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM