OpenLayers apply style to only part of a road

不打扰是莪最后的温柔 提交于 2019-12-12 01:15:08

问题


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:

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:


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
});


来源:https://stackoverflow.com/questions/54452990/openlayers-apply-style-to-only-part-of-a-road

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!