Book Image

OpenLayers 3.x Cookbook - Second Edition

By : Peter J. Langley, Antonio Santiago Perez
Book Image

OpenLayers 3.x Cookbook - Second Edition

By: Peter J. Langley, Antonio Santiago Perez

Overview of this book

OpenLayers 3 is one of the most important and complete open source JavaScript mapping libraries today. Throughout this book, you will go through recipes that expose various features of OpenLayers 3, allowing you to gain an insight into building complex GIS web applications. You will get to grips with the basics of creating a map with common functionality and quickly advance to more complicated solutions that address modern challenges. You will explore into maps, raster and vector layers, and styling in depth. This book also includes problem solving and how-to recipes for the most common and important tasks.
Table of Contents (14 chapters)
OpenLayers 3.x Cookbook Second Edition
About the Authors
About the Reviewer

Styling features based on geometry type

We can summarize that there are two ways to style a feature. The first is by applying the style to the layer so that every feature inherits this styling, as seen in the Styling layers recipe. The second is to apply the styling options directly to the feature, which we'll see in this recipe.

This recipe shows us how we can choose which flavor of styling to apply to a feature, depending on the geometry type. We will apply the style directly to the feature using the ol.Feature method, setStyle.

When a point geometry type is detected, we will actually style the representing geometry as a star, rather than the default circle shape. Other styling will be applied when a geometry type of line string is detected.

The source code can be found in ch06/ch06-styling-features-by-geometry-type, and the output of the recipe will look like the following screenshot:

How to do it…

To customize the feature styling based on the geometry type, use the following steps:

  1. Create...