编程的方式定义的OpenLayers 4 ol.format.filter.and

问题描述 投票:1回答:3

我有点新的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'],
    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)复制并粘贴到上述特征请求,我得到想要的结果。如果我引用filterString,我得到当OL试图构造过滤器的错误。

有什么明显的我失踪?

openlayers
3个回答
1
投票

它不直接相关的OpenLayers,它更多的是你的JavaScript知识

你可以创建一个函数来提供对象qazxsw POI

所以,你的代码

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')
    )
});

PS:使用ES5风格编写的代码,您可能要升级到ES6语法。


1
投票

这是因为,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)); 要求的对象不是string.You必须创建一个过滤器对象,像这样:

filter

0
投票

如果你想动态地改变过滤器的内容(添加或删除)可以定义的过滤器的阵列,然后使用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()
© www.soinside.com 2019 - 2024. All rights reserved.