以编程方式定义openLayers 4 ol.format.filter.and
我目前正在使用OpenLayers v4.6.4 我正在成功地使用ol.format.WFS创建所需的图层以编程方式定义openLayers 4 ol.format.filter.and,openlayers,Openlayers,我目前正在使用OpenLayers v4.6.4 我正在成功地使用ol.format.WFS创建所需的图层 var featureRequest = new ol.format.WFS().writeGetFeature({ srsName: 'EPSG:4326', featureNS: 'http://myserver', featurePrefix: 'locations', featureTypes: ['photos'], outputForma
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.and对象。我已经成功地创建了一个函数,该函数输出一个与上述过滤器完全匹配的字符串。当我从函数中物理复制字符串输出(filterString)并将其粘贴到上述featureRequest中时,我得到了所需的结果。如果我引用filterString,当OL试图构造筛选器时,我会得到一个错误
有什么明显的地方我遗漏了吗?这与OpenLayers没有直接关系,更多的是关于你的JavaScript知识 您可以创建一个函数,为
new ol.format.WFS().writeGetFeature(
所以你的代码
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
需要的是对象而不是字符串。您必须创建一个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
})
var值1='';
var值2='';
var值3='';
var f=ol.format.filter;
var过滤器=f和(
ol.format.filter.during('DATE',value1,
价值2),
ol.format.filter.exactTo('类别',值3)
)
var featureRequest=new ol.format.WFS().writeGetFeature({
srsName:'EPSG:4326',
特写:'http://myserver',
featurePrefix:“位置”,
功能类型:[“照片”],
outputFormat:'应用程序/json',
过滤器:过滤器
})
如果要动态更改过滤器的内容(添加或删除),可以定义过滤器数组,然后使用
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)
});