[GIS] Changin features’ pointRadius based on the current zoom level

featuresopenlayers-2style

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)

Best Answer

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;
vector.redraw(true);

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");
     }else{
        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;
       }else{
        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...