问题
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