проблема с загрузкой векторного слоя в openlayers 3

У меня проблема с загрузкой векторов в 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
        });
    }

Проблема здесь в том, что vectorSource, используемый загрузчиком, всегда [index - 1]

Я нашел Hack, определив функцию загрузчика после создания экземпляра:

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

Я нахожу это достаточно уродливым, но setLoader() не доступен для типа ServerVector. Знаете ли вы, есть ли другое решение, которое не предполагает использование другого геосервера?


person Alexandre Mélard    schedule 02.02.2015    source источник


Ответы (1)


Я сохраняю ссылку на экземпляр объекта VectorSource в функции buildLoader (вместо аргумента).

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));
        });
    }
}
person Alexandre Mélard    schedule 02.02.2015