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
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
Index

Styling based on feature attributes


So far, in this chapter, we took a look at styling features at the layer level using the setStyle method from ol.layer.Vector. We've also looked at styling individual features based on their geometry type, and then we applied the styles using the setStyle method from the ol.Feature class.

For this recipe, we'll look at a different way to style features at the feature level using a styling function at the ol.layer.Vector level. The vector layer class has a property named style, which not only accepts an instance of ol.style.Style or an array of various ol.style.Style instances, but it also accepts an ol.style.StyleFunction method. This method is called whenever a feature is rendered on the map, and the result of this method must return an array of ol.style.Style instances.

As part of this new styling technique, we'll determine how some of the styles will be applied, based on the feature attributes.

We will load a GeoJSON file of some USA cities and provide...