4

I'm trying to load different layers based on the user zoom level with OpenLayers. I've seen some similar questions in here, but they load the layer and then hide/show it based on the zoom level.

I don't want the layer to be loaded. I've managed to use some answers and came with a solution, but I don't like it that much. There must be an easier way. Can anybody point me in the right direction?

Here is the solution I've found so far.

map.events.on({ "zoomend": function (e) {
        if (this.getZoom() > 8) {
            // Setup single tiled layer
            var untiled = new OpenLayers.Layer.WMS("cite:ly_parc - Untiled", "http://localhost:8082/geoserver/cite/wms", 
                {
                    LAYERS: 'cite:ly_parc',
                    STYLES: '',
                    format: 'image/png',
                    transparent: true
                }, 
                {
                    maxExtent: bounds,
                    isBaseLayer: false
                }
            );

            this.addLayers([untiled]);
        } else {
            //TODO
        }
    }
});
5
  • Why don't you like it? It loads the layer when zoom is reached.
    – CaptDragon
    Commented Aug 16, 2012 at 18:39
  • It's messy. I was hoping I could do this with some open layers params.
    – Federico
    Commented Aug 16, 2012 at 18:52
  • question: when zooming back out is the layer just not visible and still present?
    – geomajor56
    Commented Aug 17, 2012 at 13:26
  • the code it's just a demo. I haven't try that, but I guess the layer will still be visible and present, because it's loaded. But it's very easy to change the visibility if that's what you want.
    – Federico
    Commented Aug 20, 2012 at 22:42
  • 1
    Do you want the layer to be visible only within a particular scale range? Have you tried the maxScale & minScale parameters? Commented Oct 16, 2012 at 3:40

2 Answers 2

1

There are parameters, but there's an issue with XYZ layers. But there is a patch.

All the info is here: https://stackoverflow.com/a/4294980/229875

2
  • Thanks, I'll check it. Did you tried that?
    – Federico
    Commented Aug 16, 2012 at 21:06
  • No, I have not tried.
    – Vadim
    Commented Aug 16, 2012 at 21:10
0

you create 2 styles, one standard, one temp , which is what you typically use when the feature is hovered, then you apply rules, that is the magic.

  var halte_temp_styled = new OpenLayers.Style({
      fillColor: "red",
      fontColor: "#000000",
      fontWeight: "normal",
      pointRadius: 8,
      fontSize: "11px",
      strokeColor: "#ff9933",
      strokeWidth: 2,
      pointerEvents: "all",
      fillOpacity: 0.3,
      label : "${name}",
      labelOutlineColor: "white",
      labelAlign: "rb",
      labelOutlineWidth: 8,
      cursor: "pointer",
      fontFamily: "sans-serif"
   });

  var halte_styled = new OpenLayers.Style({
      fillColor: "red",
      fillOpacity: 0.6,
      fontColor: "#000000",
      fontWeight: "light",
      pointRadius: 8,
      fontSize: "11px",
      strokeColor: "#ff9963",
      strokeWidth: 3,
      pointerEvents: "all",
      labelOutlineColor: "white",
      labelOutlineWidth: 8,
      labelAlign: "cm",
      cursor: "pointer",
      fontFamily: "sans-serif"
   });

   var halte_style = new OpenLayers.StyleMap({
      'default' : halte_styled,
      'temporary' : halte_temp_styled
   });

Then add the rules , who will influence the behavior of the (default) style, in the example below it will follow the scale of the layer and act accordingly. In this example, once you are zoomed at level 18, the labels of the feature will be displayed, else they will only be displayed when hovered over.

/* Style the halte layer acc to res */
   halte_style.styles['default'].addRules([
         new OpenLayers.Rule({
         maxScaleDenominator: 215000,
         minScaleDenominator: 27000,
         symbolizer: {
            fillColor: "red",
            fillOpacity: 0.6,
            fontColor: "#000000",
            fontWeight: "light",
            pointRadius: 4,
            fontSize: "11px",
            strokeColor: "#ff9963",
            strokeWidth: 2,
            pointerEvents: "all",
            labelOutlineColor: "white",
            labelOutlineWidth: 8,
            labelAlign: "cm",
            cursor: "pointer",
            fontFamily: "sans-serif"

            }
         }),

      new OpenLayers.Rule({
         maxScaleDenominator: 27000,
         minScaleDenominator: 3384,
         symbolizer: {
            fillColor: "red",
            fillOpacity: 0.6,
            fontColor: "#000000",
            fontWeight: "light",
            pointRadius: 10,
            fontSize: "11px",
            strokeColor: "#ff9963",
            strokeWidth: 3,
            pointerEvents: "all",
            labelOutlineColor: "white",
            labelOutlineWidth: 8,
            labelAlign: "cm",
            cursor: "pointer",
            fontFamily: "sans-serif"

            }
         }),
      new OpenLayers.Rule({
         maxScaleDenominator: 3384,
         minScaleDenominator: 1,
         symbolizer: {
            fillColor: "red",
            fillOpacity: 0.6,
            fontColor: "#000000",
            fontWeight: "light",
            pointRadius: 10,
            fontSize: "11px",
            strokeColor: "#ff9963",
            strokeWidth: 3,
            label : "${name}",
            labelAlign: "cm",
            //labelAlign: "cm",
            pointerEvents: "all",
            labelOutlineColor: "white",
            labelOutlineWidth: 8,
            cursor: "pointer",
            fontFamily: "sans-serif"
            }
         })
      ]);

In the vector layer in question you set this stylemap:

    styleMap: halte_style,

You probably could play as well with

minScale: 54168.1

On your vector layer. But that isn't as fine-grained as rules.

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.