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

Creating features programmatically

Loading data from an external source is not the only way to work with vector layers. Imagine a web-mapping application where the user can create new features on the fly, such as landing zones, perimeters, areas of interest, and so on, and add them to a vector layer with some style. This scenario requires the ability to create and add the features programmatically.

In this recipe, we will take a look at some ways to create a selection of features programmatically. The source code can be found in ch03/ch03-creating-features/. The following screenshot shows some features that are created programmatically:

How to do it…

Here, we'll create some features programmatically, without any file importing. Follow these instructions to find out how this is done:

  1. Start by creating a new HTML file with the required OpenLayers dependencies. In particular, add the div element to hold the map:

    <div id="js-map"></div>
  2. Create an empty JavaScript file and instantiate a...