OpenLayers apply style to only part of a road

2019-08-18 00:15发布

I want to make a map that represent the traffic for a city. So I have some traffic metrics that I want to visualize on the map. I wish to color each road different colours depending it's current traffic.

Something like that:

image

I tried to find a tutorial on how to apply style to only part of a roads, but I could not find how to do that.

Thank you very much it will help a lot thanks!!

1条回答
神经病院院长
2楼-- · 2019-08-18 00:48

You should go with a stylefunction for a vector layer:

https://openlayers.org/en/v4.6.5/apidoc/ol.html#.StyleFunction

A sample from OL3:

http://openlayersbook.github.io/ch11-creating-web-map-apps/example-02.html

Elaborating on the above example

function flickrStyle(feature) {
    var style = null;
    if (feature.get("name")=="Küstenschwalbe") {
        style = new ol.style.Style({
        image: new ol.style.Circle({
          radius: 6,
          stroke: new ol.style.Stroke({
            color: 'white',
            width: 2
          }),
          fill: new ol.style.Fill({
            color: 'green'
          })
        })
        });
    }
    else {
        style = new ol.style.Style({
        image: new ol.style.Circle({
          radius: 6,
          stroke: new ol.style.Stroke({
            color: 'yellow',
            width: 2
          }),
          fill: new ol.style.Fill({
            color: 'red'
          })
        })
        });
    }
    return [style];
}

var flickrLayer = new ol.layer.Vector({
  source: flickrSource,
  style: flickrStyle
});
查看更多
登录 后发表回答