The style class, ol.style.Style, contains the drawing instructions to be used when rendering a feature. There are five properties we can use when creating a style property:
As you can see, a style is really a composite of several specific types. Including a property turns on drawing of the relevant type and excluding it turns it off. When you specify the style property for a vector layer, this replaces all the default styles; so, you don't need to override all the properties all the time—just specify the ones that are needed and the rest will not be drawn at all. For example, creating a style with just a stroke property will draw polygons with an outline and no fill:
var style = new ol.style.Style({
stroke: new ol.style.Stroke({
color: [127,127,127,1]
})
});Now, let's look at each specific property type with some examples.
The fill style—ol.style.Fill—is used to fill shapes with a solid color. The fill style is used by ol.style.Style as well as a couple of other objects we'll see shortly. It has a single property, color, of the type ol.Color that is used when drawing filled shapes.
Colors may be specified in three ways:
[0,0,0,1], white is represented as [255,255,255,1], and a semitransparent blue green is [0,255,0,0.5]."rgba(red,green,blue,alpha)", where red, green, blue, and alpha are the same as the preceding array form; for example, our semitransparent green color will be "rgba(0,255,0,0.5)".#RRGGBB, where RR is the red value, GG is the green value, and BB is the blue value. The values are a hexadecimal equivalent of the numeric values between 0 and 255, written as 00 to FF. The alpha value is assumed to be 1 in this case.These different representations are equivalent (except for the missing alpha control in hex colors) and you can use whichever is more convenient.
The stroke style— ol.style.Stroke—is used to draw lines. The line style is used by ol.style.Style as well as a couple of other objects we'll see shortly. A stroke style has the following options.
The following diagram illustrates the effect of the various values for lineJoin (top) and lineCap (bottom).

Modify the last example and try out some fill and stroke style properties. In particular, try changing the lineJoin and lineCap properties. Use a wider stroke width to see the effect it produces. Note that the lineCap style won't be apparent when drawing polygons—to see it in action, you'll need a line layer, perhaps, using the fells_loop.gpx file we saw in the previous chapter.
Here are a couple of examples:
var countryStyle = new ol.style.Style({
fill: new ol.style.Fill({
color: [0, 255, 255, 1]
}),
stroke: new ol.style.Stroke({
color: [127,127,127,1.0],
width: 10,
lineJoin: 'bevel',
})
});
var timezoneStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: [64, 200, 200, 0.5],
lineJoin: 'round',
width: 10
})
});The image style—ol.style.Image—is used to style point data. You won't be using it directly though. Instead, there are two subclasses that you'll be using: ol.style.Icon and ol.style.Circle. Let's look at the icon style first.
The icon style displays an image at the location of a point. There are quite a few properties associated with the icon style that allow you to align the placement of the image relative to the precise geographic location being represented.
The following diagram illustrates the meaning of the origin, size, and anchor options:
