0

I'm using the clastering feature in my project. I collect list of points like so:

cluster_features.push(new OpenLayers.Feature.Vector(
     new OpenLayers.Geometry.Point(lon,lat),
     {heading:deg}
));

Set style:

var clusterstyle = new OpenLayers.Style({
     pointRadius: 12, 
     rotation: "${heading}", 
     externalGraphic: "ship.png"
});

And set the layer:

clusters = new OpenLayers.Layer.Vector("Clusters", {strategies: [strategy],
     styleMap: new OpenLayers.StyleMap({
          "default": clusterstyle
     })
});

And the problem is 'rotation' in styling. When all the points are unclustered in high zoom, the rotation works just fine, BUT with clustered points it doesn't work at all (the icons/images are all rotation=0 / same direction / no ratation).

P.S. I'm relatively new to Openlayers.

  • If you are new to OpenLayers, how come you are using OpenLayers 2, when the last major version is 5? – TomazicM Jun 21 at 18:04
  • @TomazicM My project is based on OpenSeaMap and unfortunately they use OpenLayers version 2.13.1 – Sam Zadworny Jun 21 at 18:11
  • OpenLayers home page says: Latest v2: 2013 i.e. really old. – TomazicM Jun 21 at 18:18
0

Ok, I found a workaround: 'context' in 'StyleMap': I place a condition there recognising whether there's clustering in place or not and then based on that get the 'heading' attribute from the point.

Also, if there's clustering in place, I get the 'heading' attribute from the middle point in the cluster (first point would be feature.cluster[0].attributes.heading;), here's the StyleMap code:

styleMap: new OpenLayers.StyleMap({
    'default':new OpenLayers.Style({
        pointRadius: 12,
        rotation: "${heading}",
        externalGraphic: "resources/ais/green.png"
    },{
        context:{
            heading:function(feature){
                if (feature.cluster) {
                    //get data from the middle point in the cluster
                    countme = Math.round(feature.attributes.count/2);
                    return feature.cluster[countme].attributes.heading;
                } else {
                    return feature.attributes.heading;
                }
            }  
        }
    }),
    "select": {
        opacity:0.5
    }
})
  • The thing is that when points are clustered, there's the cluster object created and then inside of it the points objects, so first the cluster object needs to be accessed and THEN the point. – Sam Zadworny Jun 28 at 9:18

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.