Open Layers 3使用loadingstrategy.bbox创建一个进度条加载器

时间:2016-03-07 17:43:09

标签: javascript ajax openlayers-3

我已将vectorSource设置为:

var vectorSource = function (date) {
return new ol.source.Vector({
    // Start loader
    loader: function (extent, resolution, projection) {
        var url = 'urlpath?date=' + date;
        // Show progress bar
        $('#progress').fadeIn();
        $.ajax({
            url: url,
            success: function (data) {
                var format = new ol.format.GeoJSON();
                var features = format.readFeatures(data, function () {
                    featureProjection: 'EPSG:3857'
                });
                var source = vectorSource();
                // Add the features not printing on the map but the data is there                    
                source.addFeatures(features);
                // When source is ready, then finish
                if (source.getState() == 'ready') {
                    setTimeout(function () {
                        // Hide progress bar
                        $('#progress').fadeOut(750);
                    }, 1000)
                }
            }
        });
    },
    strategy: ol.loadingstrategy.all,
});

}

我可以阅读这些功能,但我无法将它们打印到地图中。我试着看看它是投影问题还是尝试再次渲染地图,但不知道答案。

1 个答案:

答案 0 :(得分:0)

这是一个ajax请求,你无法预测需要加载的时间,因此制作进度条。相反,您已经完成了start loadingend loading事件。

    var vectorSource = new ol.source.Vector({
    loader: function (extent, resolution, projection) {
        var url = requestedUrl;
        alert("START loading here");
        $.ajax({
            url: url,
            success: function (data) {                
                var features = geoJSONFormat.readFeatures(data);
                vectorSource.addFeatures(features);
                alert("FINISH loading here");
            }
        });
    },
    strategy: ol.loadingstrategy.bbox
});