2

Is it possible to make the symbolizer of a feature be a polygon? Openlayers 3 has a ol.style.Circle and ol.style.RegularShape symbolizers for example. Is there something equivalent to a hypothetical ol.style.Polygon? Whereby you could make a dynamic symbolizer from multiple points?

The reason I want to do this is because I have markers on my map that are dynamically shaped depending on the data for that marker. It is possible to simply draw a ol.geom.Polygon at each point, but then they are not zoom independent. I want to have markers that are zoom independent, meaning that their size on the screen does not change when I zoom in or out.

And just to be clear, using raster images (for example in ol.style.Icon) is not possible. There are way too many markers in way too many shapes and colours in my project.

Hooloovoo13
  • 241
  • 2
  • 12

1 Answers1

4

Yes, this is possible. ol.style.Style takes a geometry argument that you can use to overwrite the geometry that is used to render a feature.

var style = function(feature, resolution) {
  // construct the polygon taking the resolution into account
  var polygon = new ol.geom.Polygon(...);

  return [
    new ol.style.Style({
      geometry: polygon,
      stroke: ...
      fill: ...
    }),
  ];
};

Also see this question: Drawing a Speed Leader line in OpenLayers

Community
  • 1
  • 1
tsauerwein
  • 5,841
  • 3
  • 36
  • 49
  • Thanks, But let's say there are a lot of markers though. Would this still work nicely and efficiently, considering the fact that geometries are created for the same markers everytime the user zooms in and out? – Hooloovoo13 May 11 '15 at 11:52
  • Yes, performance-wise it will make a difference if you are rendering x points or x polygons. Maybe you can cache the polygon on the feature and only update the coordinates when the resolution changes (to reduce the number of objects that are created and disposed). Or you can play with the styling. For example if you are zoomed out and would see a lot of features, maybe just display a point style instead of a polygon. – tsauerwein May 12 '15 at 07:18
  • Thanks for the good suggestions. Using geometry as a style seems like the way to go. But, just to confirm my original idea, do you think it would make sense for there to be an ol.style.Polygon class as something to request from the OL3 team? – Hooloovoo13 May 13 '15 at 08:44
  • Yes, something like `ol.style.Polygon` might make sense to draw arbitrary shapes. Feel free to open a ticket: https://github.com/openlayers/ol3/issues – tsauerwein May 13 '15 at 13:41