编程定义openlayers 4 ol.format.filter.和



我当前正在使用openlayers v4.6.4。

我成功地使用ol.format.wfs创建了我所需的层。

var featureRequest = new ol.format.WFS().writeGetFeature({
    srsName: 'EPSG:4326',
    featureNS: 'http://myserver',
    featurePrefix: 'locations',
    featureTypes: ['photos'],
    outputFormat: 'application/json',
    filter: ol.format.filter.and(
                ol.format.filter.during('DATE', '2015-11-27T05:00:00Z', 
                                        '2015-12-31T05:00:00Z'),
               ol.format.filter.exactTo('Category', 'Church')
           )

此时,过滤器是硬编码的。我需要做的就是使用下拉框中的值构造ol.format.filter.。我成功创建了一个功能,该功能输出一个与上述过滤器完全匹配的字符串。当我将字符串输出(滤波器(从功能中复制并粘贴到上述功能上时,我会得到所需的结果。如果我引用过滤器,则在OL试图构建过滤器时会出现错误。

我很明显吗?

它与openlayers并不直接相关,更多的是您的JavaScript知识

您可以创建一个函数,以向new ol.format.WFS().writeGetFeature(

提供OBJET

所以您的代码

var featureRequest = new ol.format.WFS().writeGetFeature({
    srsName: 'EPSG:4326',
    featureNS: 'http://myserver',
    featurePrefix: 'locations',
    featureTypes: ['photos'],
    outputFormat: 'application/json',
    filter: ol.format.filter.and(
        ol.format.filter.during('DATE', '2015-11-27T05:00:00Z',
                                '2015-12-31T05:00:00Z'),
        ol.format.filter.exactTo('Category', 'Church')
    )
});

会转向

var getFeatureParams = function(filter) {
    return {
        srsName: 'EPSG:4326',
        featureNS: 'http://myserver',
        featurePrefix: 'locations',
        featureTypes: ['photos'],
        outputFormat: 'application/json',
        filter: filter
    }
}
var yourDynamicFilter = ol.format.filter.and(
    ol.format.filter.during('DATE', '2015-11-27T05:00:00Z',
                            '2015-12-31T05:00:00Z'),
   ol.format.filter.exactTo('Category', 'Church')
);
var featureRequest = new ol.format.WFS().writeGetFeature(getFeatureParams(yourDynamicFilter));

ps:使用ES5样式编写的代码,您可能需要升级到ES6语法。

那是因为, filter需要一个对象而不是字符串。

var value1 = '<source of value>';
var value2 = '<source of value>';
var value3 = '<source of value>';
var f = ol.format.filter;
var filters = f.and(
  ol.format.filter.during('DATE', value1,
    value2),
  ol.format.filter.exactTo('Category', value3)
)
var featureRequest = new ol.format.WFS().writeGetFeature({
      srsName: 'EPSG:4326',
      featureNS: 'http://myserver',
      featurePrefix: 'locations',
      featureTypes: ['photos'],
      outputFormat: 'application/json',
      filter: filters
})

如果要动态更改过滤器的内容(添加或删除(,则可以定义一个过滤器,然后使用function.prototype.apply((将其传递给函数。

var filterArray = [
  ol.format.filter.during('DATE', '2015-11-27T05:00:00Z', '2015-12-31T05:00:00Z'),
  ol.format.filter.exactTo('Category', 'Church')
];
var featureRequest = new ol.format.WFS().writeGetFeature({
  srsName: 'EPSG:4326',
  featureNS: 'http://myserver',
  featurePrefix: 'locations',
  featureTypes: ['photos'],
  outputFormat: 'application/json',
  filter: ol.format.filter.and.apply(null, filterArray)
});

最新更新