[OpenLayers-Users] Layer.Vector with multiple data sources

Arnd Wippermann arnd.wippermann at web.de
Fri Jan 9 08:37:42 EST 2009


Hi Andreas,

I have no luck so far.

The following code is from the example styles-context.html, added the
getVisibility. If you paste the code into firebug console for the openlayers
homepage http://openlayers.org/, it will add a vector layer with points. At
zoomLevel 0 the red points are set to display:"none", etc.

But you can see, that the display property have no effect. Do I miss
something?

Mit freundlichen Grüßen
Arnd Wippermann

// Strategy 2:  Style features based on something besides attributes.

// create 50 random features in the southern hemisphere
var features = new Array(50);
for (var i=0; i<features.length; i++) {
    features[i] = new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(
            (360 * Math.random()) - 180, -90 * Math.random()
        ), {
            type: 5 + parseInt(5 * Math.random())
        }
    );
}
// create the layer styleMap by giving the default style a context
var colors = ["red", "green", "blue"];
var context = {

//  ################################################ added part
    getVisibility: function(feature) {
        var zoomLevel   = map.getZoom();
        var region = parseInt((feature.geometry.x + 180) / 120);
        if(region==zoomLevel)
            var retValue = "none";
        else
            var retValue = "";
        console.log("zoom:" + zoomLevel + ", regionColor:" + colors[region]
+ ", return:'" + retValue + "'");
        return retValue;
    },
//  ################################################ end of added part

    getColor: function(feature) {
        var region = parseInt((feature.geometry.x + 180) / 120);
        return colors[region];
    },
    getType: function(feature) {
        return feature.attributes["type"];
    }
};
var template = {
    display:"${getVisibility}",
    pointRadius: "${getType}", // using context.getType(feature)
    fillColor: "${getColor}", // using context.getColor(feature)
    strokeColor:"black",
    strokeWidth:2
};
var style = new OpenLayers.Style(template, {context: context});
var layer2 = new OpenLayers.Layer.Vector('Points', { styleMap: new
OpenLayers.StyleMap(style)});
layer2.addFeatures(features);
map.addLayers([layer2]);




More information about the Users mailing list