[OpenLayers-Users] changing filters on the fly

Trond Michelsen trondmm-openlayers at crusaders.no
Wed Nov 19 11:50:11 EST 2008


On Wed, Nov 19, 2008 at 03:34:19PM +0100, Trond Michelsen wrote:
> On Wed, Nov 19, 2008 at 03:14:36PM +0100, Trond Michelsen wrote:
>> I tried to simply keep a copy of the filter and layer, and do
>> something like this:
[...]
>> But nothing changed on the vectorlayer when I triggered the function.
> Oh, wait... This works. I'm not sure why it didn't work on my first
> try, but it works now.

I got adding and removing rules working as well, so now I've got basic
time functionality on a vector layer working. It's still a bit
hackish, but it does what I want it to do.

If anyone's interested, here's the code I used:

function setFilterValues (layer, filters, value) {
  for (var i in filters) {
    filters[i].value = value;
  }
  layer.redraw();
}

function addRule (layer, rule) {
  for (var type in layer.styleMap.styles) {
    var style = layer.styleMap.styles[type];
    var rule_exists = false;
    for (var i in style.rules) {
      if (rule === style.rules[i]) rule_exists = true;
    }
    if (! rule_exists) style.addRules(rule);
  }
}

function removeRule (layer, rule) {
  for (var type in layer.styleMap.styles) {
    var style = layer.styleMap.styles[type];
    for (var i = style.rules.length-1; i>=0; i--) {
      if (rule === style.rules[i]) style.rules.splice(i, 1);
    }
  }
  layer.redraw();
}

and here's the rule:

var filter1 = new OpenLayers.Filter.Comparison ({
  type: '<=',
  property: "FROM",
  value: ""
});

var filter2 = new OpenLayers.Filter.Comparison ({
  type: '>',
  property: "TO",
  value: ""
});

window.GLOBAL.rule = new OpenLayers.Rule({
  filter: new OpenLayers.Filter.Logical({
    filters: [filter1,filter2], 
    type: '&&'
  })
});


When no time is selected, the rule is removed, and when times are
available for selection, the rule is first added, then setFilterValues
is called with the default time value. After that, setFilterValues is
called again every time the user selects a new time.

The time value is always in a full iso8601 format
(YYYY-MM-DDThh:mm:ss), so a simple string comparison should always
work.

-- 
Trond Michelsen



More information about the Users mailing list