I need to have my features change their pointRadius based on the current Zoom level. My questions are:

  1. Would it be possible to use Rule Based Styling techniques to overcome the problem?
  2. If yes, it will work on map zoom changed as well?
  3. If no, what are the alternative solutions and what you offer instead? (except redrawing features)

Update 2

//exmp for zoom level 5
var new_style = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
new_style.pointRadius = 5;
vector.styleMap.styles['default'].defaultStyle = new_style;

Update 1

if you want to catch zoom event, you should register your zoomend to map.

map.events.register(type, obj, listener);

example code:

 map.events.register("zoomend", map, function() {
     if(zoom == 3){
        console.info("your zoom is at level 3");
        console.warn("your zoom is not at level 3");

try to use following code.

var zoom = map.getZoom();
var result;

var style = new OpenLayers.Style({
    pointRadius: "${radius}"
  } , {
    context: {
      radius: function(feature) {
      if (zoom == 5){
        result = 5 * defaultPointRadius;
        result = 2 * defaultPointRadius;
        return result

var layer= new OpenLayers.Layer.Vector("Vector Layer", {
    strategies: [strategy],
    styleMap: new OpenLayers.StyleMap({"default": style})});

i hope it helps you...

| improve this answer | |
  • Thanks Aragon, I will give it a try soon. BTW, should I set the defaultPointRadius or that's internal? – Mahdi Apr 10 '13 at 14:52
  • it is only variable, you can define what you want. good luck :) – Aragon Apr 10 '13 at 15:10
  • this works fine, but not when the zoom level is changed. Is there any way to fire it again when user changes the zoom level? I have lot's of layers and features, I really don't want to redraw them every time. – Mahdi Apr 11 '13 at 6:57
  • @Mahdi, i have updated my answer. hope it helps you... – Aragon Apr 11 '13 at 7:08
  • Thanks for the effort. I already have that in my code which is redrawing all the features when the zoom level is changes, my question is how to bind it to the style so it doesn't need to redraw everything each time zoom changes. Is it even possible? – Mahdi Apr 11 '13 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.