Book Image

OpenLayers 3: Beginner's Guide

By : Thomas Gratier, Paul Spencer, Erik Hazzard
Book Image

OpenLayers 3: Beginner's Guide

By: Thomas Gratier, Paul Spencer, Erik Hazzard

Overview of this book

<p>This book is a practical, hands-on guide that provides you with all the information you need to get started with mapping using the OpenLayers 3 library.</p> <p>The book starts off by showing you how to create a simple map. Through the course of the book, we will review each component needed to make a map in OpenLayers 3, and you will end up with a full-fledged web map application. You will learn the key role of each OpenLayers 3 component in making a map, and important mapping principles such as projections and layers. You will create your own data files and connect to backend servers for mapping. A key part of this book will also be dedicated to building a mapping application for mobile devices and its specific components.</p>
Table of Contents (22 chapters)
OpenLayers 3 Beginner's Guide
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – understanding the forEachFeatureAtPixel method


Let's try with another example to discover more about the forEachFeatureAtPixel method. It should remind you about the last sample from Chapter 5, Using Vector Layers. To complete the task, check the following steps:

  1. First, copy and paste the code from Time for action – converting your local or national authorities data into web mapping formats, that is, the file 2360_08_01_simple_select.html in a new HTML file 2360_08_03_foreachfeatureatpixel.html.

  2. Then change the ol.js JavaScript file reference with ol-debug.js.

  3. After that, add at the end of the JavaScript section of the new HTML file the following:

    map.on('click' , function(evt) {
      var pixel = evt.pixel;
      console.log(evt);
      console.log(pixel);});
  4. Open your browser with the console, and click where you have blue features to get a result like the following screenshot:

  5. Next, remove both console.log statements, and add the following code just after the line var pixel = evt.pixel...