4

I am using OpenLayers 2 and Geoserver.
I have a few WFS layers and an OpenStreetMap as a Baselayer. I want to change the vector layers' visibility at certain zoom levels, by using min/maxScale or min/maxResolution, but it doesn't seem to work. In the following example I use only one of the layers as a test.

My code using min/maxScale:

<script defer="defer" type="text/javascript">           
    var map;

    // pink tile avoidance
    OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
    // make OL compute scale according to WMS spec
    OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;

    function init(){

        // Save-strategy
        var saveStrategy = new OpenLayers.Strategy.Save();

        // Controls
        var controls = [                
            new OpenLayers.Control.ArgParser(), 
            new OpenLayers.Control.Navigation(),
            new OpenLayers.Control.PanZoomBar({position: new OpenLayers.Pixel(2, 15)}),
            new OpenLayers.Control.LayerSwitcher(),
        ];              

        //Options
        var options = {
            scales: [500000, 100000, 50000, 25000, 10000, 5000],
            projection: "EPSG:4326",
            displayProjection: "EPSG:4326",
            units: 'm'
        }   

        //Map
        map = new OpenLayers.Map('map', /*{controls: controls}*/ options);  //Don't quite know how to use both: controls and options; 
                                                                            //options doesn't work anyway

        //Basemap
        var osm = new OpenLayers.Layer.OSM("Simple OSM Map");               
        map.addLayer(osm);

        //Layer
        var test = new OpenLayers.Layer.Vector("xName", {               
            strategies: [new OpenLayers.Strategy.Fixed(), saveStrategy],
            minScale: 1/50000,
            maxScale: 1/25000,
            protocol: new OpenLayers.Protocol.WFS({
            url: SETTINGS.BASE_URL,
            featurePrefix: 'xNS',
            featureNS: "xNS",
            //extractAttributes: true,
            featureType: "xName",
            }),
                styleMap: new OpenLayers.StyleMap({
                    "default": new OpenLayers.Style({
                        "pointRadius": 2,
                        fill: true,
                        fillColor: "#8d006a",
                        strokeColor: "#000000",
                        strokeWidth: 0.2
                    })
                })
            });
        //Add Layer

        map.addLayer(test); 

        //Set Map Center                
        map.setCenter(
            new OpenLayers.LonLat(27.1, 47.55).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject()
            ), 9
        );          
    }
</script>  

If I use min/max Scale nothing happens, the layers are visible at all zoom levels. Also, in the browser Console I see that the units didn't change, even if I specified "meters" in my options, I still see that "degrees" are used.

I tried the solution listed here: "Layer visibility using maxscale minscale not working" - using min/maxResolution, but it still doesn't work.

"maxScale or minScale dont working in vector (from wfs) layer" doesn't work either.

My code using min/maxResolution:

        //Basemap
        var osm = new OpenLayers.Layer.OSM("Simple OSM Map");               
        map.addLayer(osm);

        //Layer
        var test = new OpenLayers.Layer.Vector("xName", {               
            strategies: [new OpenLayers.Strategy.Fixed(), saveStrategy],
            minResolution: 14.035643,
            maxResolution: 7.01782148,
            protocol: new OpenLayers.Protocol.WFS({
            url: SETTINGS.BASE_URL,
            featurePrefix: 'xNS',
            featureNS: "xNS",
            //extractAttributes: true,
            featureType: "xName",
            }),
                styleMap: new OpenLayers.StyleMap({
                    "default": new OpenLayers.Style({
                        "pointRadius": 2,
                        fill: true,
                        fillColor: "#8d006a",
                        strokeColor: "#000000",
                        strokeWidth: 0.2
                    })
                })
            });
        //Add Layer

        map.addLayer(test); 

When using min/maxResolution, the layers are no longer visible at any zoom level. They can be seen on the LayerSwitcher but cannot be selected. I suppose that at a certain resolution (the one specified) they should be visible and selectable, but that's not the case.

1 Answer 1

2

After several hours, I finally found the answer. It turns out that the scale on the map shows different values than the scale in the browser console (which I find odd), so I was adding the wrong values to the min/maxResolution.

Working code:

var test = new OpenLayers.Layer.Vector("xName", {               
                strategies: [new OpenLayers.Strategy.Fixed(), saveStrategy],
                minResolution: 0.001,
                maxResolution: 200,
                protocol: new OpenLayers.Protocol.WFS({
                url: SETTINGS.BASE_URL,
                featurePrefix: 'xNS',
                featureNS: "xNS",
                //extractAttributes: true,
                featureType: "xName",
                }),
2
  • How did you determine the min/max resolution for your vector, out of curiosity?
    – Sleep6
    Commented Jan 9, 2017 at 20:26
  • Thanks. I did my maxResolution by trial and error (Binary Chop) Commented Apr 3, 2018 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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