Openlayers 3:每次将一个段添加到LineString时重绘地图图层

时间:2015-10-09 08:20:47

标签: canvas render openlayers-3

跟进

Dynamically adding and removing segments from a track in OpenLayers 3

我正在使用setInterval循环逐段绘制LineString。每次循环遍历函数并添加一个段时,我最终会重新渲染该层。这是最有效的方法吗?代码有效,但我得到了

AssertionError: Assertion failed: listeners already registered
第一个之后每个循环的

错误。

以下相关代码:

    function makeLineString(multipointCoords) {

        var trackStyle = new ol.style.Style({
            stroke: new ol.style.Stroke({
                color: 'rgba(255,0,255,0.2)',
                width: 0.5
            })
        });

        var trackFeature = new ol.Feature({
            geometry: new ol.geom.LineString([
                ol.proj.transform(multipointCoords[0][0], 'EPSG:4326', 'EPSG:3857')
            ])
        });

        var trackLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
                features: [trackFeature],
            }),
            style: trackStyle
        });

        var coordinate, i = 1,
            length = multipointCoords[0].length;
        var timeInterval = 250;
        console.log(i);

        var timer;
        timer = setInterval(function() {
            segmentConstruction(multipointCoords);
        }, timeInterval);

        function segmentConstruction(multipointCoords) {

            coordinate = ol.proj.transform(multipointCoords[0][i], 'EPSG:4326', 'EPSG:3857');

            trackFeature.getGeometry().appendCoordinate(coordinate);

            if (i === length-1) {
                clearInterval(timer);
            } else {
                i++;
                map.addLayer(trackLayer);
            };
        };
    };

1 个答案:

答案 0 :(得分:0)

不,不要这样做。你可能想要这样的东西:

CREATE EVENT test_event_03
ON SCHEDULE EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
DO
    INSERT IGNORE INTO valid_employees
    (empid, name, place, position, date_of_joining, status)
    SELECT (empid, name, place, position, date_of_joining, status)FROM employees
    WHERE status='active' and position='Software Engineer' and Date('2010-01-01')<date_of_joining

然后你的追加功能:

var lineString = new ol.geom.LineString([
    ol.proj.fromLonLat(multipointCoords[0][0])
]);
var trackFeature = new ol.Feature({
    geometry: lineString
});

http://jsfiddle.net/jonataswalker/ots444of/