Table of Contents for
OpenLayers 3 : Beginner's Guide

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition OpenLayers 3 : Beginner's Guide by Erik Hazzard Published by Packt Publishing, 2015
  1. Cover
  2. Table of Contents
  3. OpenLayers 3 Beginner's Guide
  4. OpenLayers 3 Beginner's Guide
  5. Credits
  6. About the Authors
  7. About the Reviewers
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Sections
  13. Time for action – heading
  14. Conventions
  15. Reader feedback
  16. Customer support
  17. 1. Getting Started with OpenLayers
  18. Advantages of using OpenLayers
  19. What, technically, is OpenLayers?
  20. Anatomy of a web mapping application
  21. Connecting to Google, Bing Maps, and other mapping APIs
  22. Time for action – downloading OpenLayers
  23. Time for action – creating your first map
  24. Where to go for help
  25. OpenLayers issues
  26. OpenLayers source code repository
  27. Getting live news from RSS and social networks
  28. Summary
  29. 2. Key Concepts in OpenLayers
  30. Time for action – creating a map
  31. Time for action – using the JavaScript console
  32. Time for action – overlaying information
  33. OpenLayers' super classes
  34. Key-Value Observing with the Object class
  35. Time for action – using bindTo
  36. Working with collections
  37. Summary
  38. 3. Charting the Map Class
  39. Time for action – creating a map
  40. Map renderers
  41. Time for action – rendering a masterpiece
  42. Map properties
  43. Time for action – target practice
  44. Map methods
  45. Time for action – creating animated maps
  46. Events
  47. Views
  48. Time for action – linking two views
  49. Summary
  50. 4. Interacting with Raster Data Source
  51. Layers in OpenLayers 3
  52. Common operations on layers
  53. Time for action – changing layer properties
  54. Tiled versus untiled layers
  55. Types of raster sources
  56. Tiled images' layers and their sources
  57. Time for action – creating a Stamen layer
  58. Time for action – creating a Bing Maps layer
  59. Time for action – creating tiles and adding Zoomify layer
  60. Image layers and their sources
  61. Using Spherical Mercator raster data with other layers
  62. Time For action – playing with various sources and layers together
  63. Time For action – applying Zoomify sample knowledge to a single raw image
  64. Summary
  65. 5. Using Vector Layers
  66. Time for action – creating a vector layer
  67. How the vector layer works
  68. The vector layer class
  69. Vector sources
  70. Time for action – using the cluster source
  71. Time for action – creating a loader function
  72. Time for action – working with the TileVector source
  73. Time for action – a drag and drop viewer for vector files
  74. Features and geometries
  75. Time for action – geometries in action
  76. Time for action – interacting with features
  77. Summary
  78. 6. Styling Vector Layers
  79. Time for action – basic styling
  80. The style class
  81. Time for action – using the icon style
  82. Have a go hero – using the circle style
  83. Multiple styles
  84. Time for action – using multiple styles
  85. Style functions
  86. Time for action – using properties to style features
  87. Interactive styles
  88. Time for action – creating interactive styles
  89. Summary
  90. 7. Wrapping Our Heads Around Projections
  91. Time for action – using different projection codes
  92. Time for action – determining coordinates
  93. OpenLayers projection class
  94. Transforming coordinates
  95. Time for action – coordinate transforms
  96. Time for action – setting up Proj4js.org
  97. Time for action – reprojecting extent
  98. Time for action – using custom projection with WMS sources
  99. Time for action – reprojecting geometries in vector layers
  100. Summary
  101. 8. Interacting with Your Map
  102. Time for action – converting your local or national authorities data into web mapping formats
  103. Time for action – testing the use cases for ol.interaction.Select
  104. Time for action – more options with ol.interaction.Select
  105. Introducing methods to get information from your map
  106. Time for action – understanding the forEachFeatureAtPixel method
  107. Time for action – understanding the getGetFeatureInfoUrl method
  108. Adding a pop-up on your map
  109. Time for action – introducing ol.Overlay with a static example
  110. Time for action – using ol.Overlay dynamically with layers information
  111. Time for action – using ol.interaction.Draw to share new information on the Web
  112. Time for action – using ol.interaction.Modify to update drawing
  113. Understanding interactions and their architecture
  114. Time for action – configuring default interactions
  115. Discovering the other interactions
  116. Time for action – using ol.interaction.DragRotateAndZoom
  117. Time for action – making rectangle export to GeoJSON with ol.interaction.DragBox
  118. Summary
  119. 9. Taking Control of Controls
  120. Adding controls to your map
  121. Time for action – starting with the default controls
  122. Controls overview
  123. Time for action – changing the default attribution styles
  124. Time for action – finding your mouse position
  125. Time for action – configuring ZoomToExtent and manipulate controls
  126. Creating a custom control
  127. Time for action – extending ol.control.Control to make your own control
  128. Summary
  129. 10. OpenLayers Goes Mobile
  130. Using a web server
  131. Time for action – go mobile!
  132. The Geolocation class
  133. Time for action – location, location, location
  134. The DeviceOrientation class
  135. Time for action – a sense of direction
  136. Debugging mobile web applications
  137. Debugging on iOS
  138. Debugging on Android
  139. Going offline
  140. Time for action – MANIFEST destiny
  141. Going native with web applications
  142. Time for action – track me
  143. Summary
  144. 11. Creating Web Map Apps
  145. Using geospatial data from Flickr
  146. Time for action – getting Flickr data
  147. A simple application
  148. Time for Action – adding data to your map
  149. Styling the features
  150. Time for action – creating a style function
  151. Creating a thumbnail style
  152. Time for action – switching to JSON data
  153. Time for action – creating a thumbnail style
  154. Turning our example into an application
  155. Time for action – adding the select interaction
  156. Time for action – handling selection events
  157. Time for action – displaying photo information
  158. Using real time data
  159. Time for action – getting dynamic data
  160. Wrapping up the application
  161. Time for action – adding dynamic tags to your map
  162. Deploying an application
  163. Creating custom builds
  164. Creating a combined build
  165. Time for action – creating a combined build
  166. Creating a separate build
  167. Time for action – creating a separate build
  168. Summary
  169. A. Object-oriented Programming – Introduction and Concepts
  170. Going further
  171. B. More details on Closure Tools and Code Optimization Techniques
  172. Introducing Closure Library, yet another JavaScript library
  173. Time for action – first steps with Closure Library
  174. Making custom build for optimizing performance
  175. Time for action – playing with Closure Compiler
  176. Applying your knowledge to the OpenLayers case
  177. Time for action - running official examples with the internal OpenLayers toolkit
  178. Time for action - building your custom OpenLayers library
  179. Syntax and styles
  180. Time for action – using Closure Linter to fix JavaScript
  181. Summary
  182. C. Squashing Bugs with Web Debuggers
  183. Time for action – opening Chrome Developer Tools
  184. Explaining Chrome Developer debugging controls
  185. Time for action – using DOM manipulation with OpenStreetMap map images
  186. Time for action – using breakpoints to explore your code
  187. Time for action – playing with zoom button and map copyrights
  188. Using the Console panel
  189. Time for action – executing code in the Console
  190. Time for action – creating object literals
  191. Time for action – interacting with a map
  192. Improving Chrome and Developer Tools with extensions
  193. Debugging in other browsers
  194. Summary
  195. D. Pop Quiz Answers
  196. Chapter 5, Using Vector Layers
  197. Chapter 7, Wrapping Our Heads Around Projections
  198. Chapter 8, Interacting with Your Map
  199. Chapter 9, Taking Control of Controls
  200. Chapter 10, OpenLayers Goes Mobile
  201. Appendix B, More details on Closure Tools and Code Optimization Techniques
  202. Appendix C, Squashing Bugs with Web Debuggers
  203. Index

Vector sources

The vector layer class by itself isn't very useful—it relies on other classes to do all the interesting work. Let's look at the source class, ol.source.vector, and its subclasses first as it's the only thing that is absolutely needed to get a working vector layer.

The vector source class is named after its purpose—to be a source of vector features for a vector layer. It is responsible for fetching features when needed, providing them to the vector layer for rendering, and also for retrieving features based on various criteria. We won't actually use the vector source class as it is a base class for the classes we'll actually be using, such as ol.source.GeoJSON in the preceding example. The following diagram shows how the vector source classes fit together in the OpenLayers architecture:

Vector sources

As you can see, there are quite a few classes in the OpenLayers library that deal with vector formats. The classes that you can create new objects from are highlighted in green. We'll briefly describe each class before we get into more detail:

  • ol.source.Vector: This is the base class for all vector sources. It provides a common API for accessing features in a vector source.
  • ol.source.Cluster: This source automatically groups features together when they are close to each other, and represents the group as a single feature. The cluster source uses another vector source to access features to be clustered.
  • ol.source.FormatVector: This class provides a common API for sources that use a format for reading and writing features. You cannot create an instance of this class.
  • ol.source.ServerVector: This source uses a loader function that can retrieve vector features on the fly from a server, for example, a WFS (Web Feature Service) server.
  • ol.source.TileVector: This source loads vector features in batches based on a tiling scheme similar to how raster layers load image tiles.
  • ol.source.StaticVector: This source loads vector features from a file in a specific format. It is considered static in that all the available features are read from the file when it is loaded. Although you can create an instance of this class, you will normally use one of the format-specific subclasses.
  • ol.source.GeoJSON: This source reads a file containing features in the GeoJSON format, a standardized encoding of geographic features using JSON.
  • ol.source.GPX: This source reads a file containing features in the GPX (GPS Exchange format), a common file format for GPS devices.
  • ol.source.IGC: This source reads a file in the IGC (International Glider Commission) format, a standard format for recording glider flights.
  • ol.source.KML: This source reads a file in the KML (Keyhole Markup Language), an XML-based format used in Google Maps and related products.
  • ol.source.OSMXML: This source reads a file using the Open Street Map XML schema.
  • ol.source.TopoJSON: This source reads a file encoded in JSON using the TopoJSON specification, an extension of GeoJSON that encodes topology information.

The vector source class

The vector source class, ol.source.vector, not only provides a common API for all vector sources but is also be instantiated and used directly. All of its subclasses primarily deal with actually retrieving vector features, but all the useful methods for actually interacting with features once they have been processed exist in this class. You can use this class if you have an existing set of vector features or are getting features in some way not directly supported by the subclasses. Some examples might include:

  • Using a mobile device's GPS to track the user's current location and creating waypoints or tracks based on changes in location
  • Programmatically generating features
  • Reading features from a legacy system that is not directly supported by OpenLayers

The vector source constructor takes the following options:

Name

Type

Description

attributions

Array.<ol.Attribution> | undefined

This is an array of attribution objects that describe the provenance of the data. The attributions will be displayed on the map in the attribution area when features from this source are rendered on the map.

features

Array.<ol.Feature> | undefined

This is an array of feature objects initially added to this source.

logo

string | olx.LogoOptions | undefined

This is a logo object that represents an image logo to be displayed on the map when features from this source are rendered on the map.

projection

ol.proj.ProjectionLike

This is the projection of the features in this source, or rather the projection of the features' geometries. The projection must be specified.

state

ol.source.State | undefined

This is the state of the source, either one of loading, ready, or error.

The vector source also provides the following methods:

Method

Parameters

Description

addFeature(feature)

ol.Feature

This adds a single feature to this source, triggering the addfeature event. A redraw of the map will be scheduled if the feature should appear in the current map area being viewed.

addFeatures(features)

Array.<ol.Feature>

This adds an array of features to this source. The addfeature event will be triggered for each feature added. As with addFeature, a redraw of the map will be scheduled if any of the features need to be drawn.

clear

none

This removes all features from the source. This will trigger the removefeature event for each feature as it is removed and schedule a redraw of the map if any of the features were visible on the map.

forEachFeature(callback, scope)

  • callback – function
  • scope – object | null

This method calls the callback function for each feature in the source. The callback function will be called with a feature as its only argument. Within the callback function, the value of this will be the scope, if provided.

forEachFeatureInExtent(extent, callback, scope)

  • extent – ol.Extent
  • callback – function
  • scope – object | null

This method works the same way as forEachFeature except that it first filters the features by the provided extent. Contrary to what the method name implies, the features may not actually be within the provided extent. Rather, features whose extent intersects the provided extent are included.

getClosestFeatureToCoordinate(coordinate)

ol.Coordinate

This method returns the feature that is closest to the provided coordinate. If there are several features equidistant to the coordinate, it is indeterminate which will be returned.

getExtent()

none

This returns an ol.Extent object representing the extent all the features currently in this source.

getFeatureById()

id – string | number

This method returns a feature whose ID equals the passed value. For performance, the type of the id is not used to determine equality. This means that a string value of 2 will match a numeric value of 2. If multiple features have the same id, it is indeterminate which will be returned.

getFeatures()

none

This method returns all the features of this source as an array.

getFeaturesAtCoordinate(coordinate)

ol.Coordinate

This method returns an array of features whose extent contains the given coordinate.

removeFeature(feature)

ol.Feature

This method removes a single feature from the source and triggers the removefeature event. Removing a feature will schedule a redraw of the map if the feature is currently visible.

The vector source can trigger the following events:

  • addfeature: This event is triggered when a feature is added to the source
  • removefeature: This event is triggered when a feature is removed from the source

The cluster source

There are two subclasses of the vector source, FormatVector and Cluster. The FormatVector class is the basis for many subclasses that obtain features by reading them from some specifically formatted data such as GeoJSON or KML. We'll look into these classes shortly, but first let's take a brief look at the cluster source.

The cluster source dynamically groups (or clusters) features that are near each other based on the current resolution, or zoom level, of a map's view and represents those groups as individual features. It does this by finding features within a certain distance of each other and creating a new feature to represent them. The new feature contains a reference to the original features that are represented by the clustered feature.

Creating a cluster source is the same as creating a vector source except the constructor options include two additional properties:

Name

Type

Description

distance

number

This is the minimum distance, in pixels, between clusters. Features that are less than this distance apart at the current zoom level will be clustered into a single feature.

source

ol.source.vector

This is a vector source that provides the features to be clustered.