【问题标题】:vector layer loading issue in openlayers 3openlayers 3中的矢量图层加载问题
【发布时间】:2015-02-02 08:36:02
【问题描述】:

我有一个关于 ol3 中矢量加载的问题。

我正在使用不处理 js 回调

的地理服务器

为了加载我的功能,我必须将它们添加到加载器的 ajax done 函数中:

var buildLoader = function(vectorSource, $vector) {
    return function(extent, resolution, projection) {
        extent = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:3857", "EPSG:4326"));
        var url = 'https://myurl/' + $vector.attr("url") +
            '?service=WFS' +
            '&version=' + $vector.attr("version") +
            '&request=GetFeature' +
            '&typename=' + $vector.attr("typename") +
            '&srs=' + $vector.attr("projection") +
            '&bbox=' + extent +
            '&outputFormat=json';
        $.ajax({
            url: url,
            dataType: 'json',
            xhrFields: {
              withCredentials: true
            }
        })
        .done(function(response) {
            vectorSource.addFeatures(vectorSource.readFeatures(response));
        });
    }
}

然后我构建一个向量数组以添加到我的地图上。

    for (index=0; index < vectors.length; index++) {
        var $vector = $(vectors[index]);
        var vectorSource = new ol.source.ServerVector({
            format: new ol.format.GeoJSON(),
            loader: buildLoader(vectorSource, $vector),
            projection: 'EPSG:3857'
        });

        // ... //
        datas[index] = new ol.layer.Vector({
            source: vectorSource,
            visible: 'false',
            style: iconStyle
        });
    }

这里的问题是loader使用的vectorSource总是[index - 1]

我发现了一个Hack,通过在实例化之后定义loader函数:

        var vectorSource = new ol.source.ServerVector({
            format: new ol.format.GeoJSON(),
            projection: 'EPSG:3857'
        });
        vectorSource.loader_ = buildLoader(vectorSource, $vector);

我觉得这很丑陋,但是没有可用于 ServerVector 类型的 setLoader()。您知道是否有其他解决方案不涉及使用其他地理服务器?

【问题讨论】:

    标签: openlayers-3


    【解决方案1】:

    我在 buildLoader 函数中保存了对 VectorSource 对象实例的引用(而不是参数)。

    vectorSource = this;
    

    然后我在 done() 函数中使用该引用:

    vectorSource.addFeatures(vectorSource.readFeatures(response));
    

    完整来源:

    var buildLoader = function($vector) {
        return function(extent, resolution, projection) {
            vectorSource = this;
            extent = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:3857", "EPSG:4326"));
            var url = 'https://myurl/' + $vector.attr("url") +
                '?service=WFS' +
                '&version=' + $vector.attr("version") +
                '&request=GetFeature' +
                '&typename=' + $vector.attr("typename") +
                '&srs=' + $vector.attr("projection") +
                '&bbox=' + extent +
                '&outputFormat=json';
            $.ajax({
                url: url,
                dataType: 'json',
                xhrFields: {
                  withCredentials: true
                }
            })
            .done(function(response) {
                vectorSource.addFeatures(vectorSource.readFeatures(response));
            });
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      相关资源
      最近更新 更多