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

The style class

The style class, ol.style.Style, contains the drawing instructions to be used when rendering a feature. There are five properties we can use when creating a style property:

Name

Type

Description

fill

ol.style.Fill

This style is used when filling polygons. To draw unfilled polygons, leave this property out or set to null.

stroke

ol.style.Stroke

This style is used when drawing lines and drawing the outline of polygons. To draw polygons without an outline, leave this property out or set to null.

image

ol.style.Image

This style is used when drawing points.

text

ol.style.Text

This style is used when drawing text.

zIndex

number

The z-index determines the order in which features are drawn. To ensure that certain features are drawn on top of other features—for instance, points on top of polygons—assign a higher zIndex value. Note that this only affects features within the same layer.

As you can see, a style is really a composite of several specific types. Including a property turns on drawing of the relevant type and excluding it turns it off. When you specify the style property for a vector layer, this replaces all the default styles; so, you don't need to override all the properties all the time—just specify the ones that are needed and the rest will not be drawn at all. For example, creating a style with just a stroke property will draw polygons with an outline and no fill:

var style = new ol.style.Style({
  stroke: new ol.style.Stroke({
    color: [127,127,127,1]
  })
});

Now, let's look at each specific property type with some examples.

Fill styles

The fill style—ol.style.Fill—is used to fill shapes with a solid color. The fill style is used by ol.style.Style as well as a couple of other objects we'll see shortly. It has a single property, color, of the type ol.Color that is used when drawing filled shapes.

Note

Colors may be specified in three ways:

  • An array of four values representing the red, green, blue, and alpha components of the color. The color components are numbers between 0 and 255, while the alpha value is between 0 (transparent) and 1 (opaque). For example, black is represented as [0,0,0,1], white is represented as [255,255,255,1], and a semitransparent blue green is [0,255,0,0.5].
  • A CSS RGBA string expression, written as "rgba(red,green,blue,alpha)", where red, green, blue, and alpha are the same as the preceding array form; for example, our semitransparent green color will be "rgba(0,255,0,0.5)".
  • A CSS hexadecimal, or hex, color value written as #RRGGBB, where RR is the red value, GG is the green value, and BB is the blue value. The values are a hexadecimal equivalent of the numeric values between 0 and 255, written as 00 to FF. The alpha value is assumed to be 1 in this case.

These different representations are equivalent (except for the missing alpha control in hex colors) and you can use whichever is more convenient.

Stroke styles

The stroke style— ol.style.Stroke—is used to draw lines. The line style is used by ol.style.Style as well as a couple of other objects we'll see shortly. A stroke style has the following options.

Name

Type

Description

color

ol.Color

This is the color to use when drawing lines.

lineCap

string

This is the style to draw the end of lines in. This may be one of the following:

  • butt: These finish lines squarely right at the exact point the line ends at.
  • round: These finish lines by rounding them, radius depends on width at the bottom. This is the default value if not specified.
  • square: These finish lines with a square the size of width (line extends past the last point by the line width)

See the diagram following this table for an example of each lineCap style.

lineJoin

string

The line join style is used when drawing segments that are part of the same line. This may be one of the following:

  • bevel: This joins lines with a bevel.
  • round: This joins lines by rounding them. This is the default value if not specified.
  • miter: This joins lines by mitering them (see miterLimit below).

See the diagram following this table for an example of each lineJoin style.

lineDash

Array.<number>

This is an array of numbers that define the on-off pattern for drawing lines with a dash pattern. The default is none (no dash pattern).

miterLimit

number

This is the limit for drawing miter joins; the default is 10.

width

number

This is the width, in pixels, to draw the line. This number may be a floating point number.

The following diagram illustrates the effect of the various values for lineJoin (top) and lineCap (bottom).

Stroke styles

Have a Go Hero – fill and stroke styles

Modify the last example and try out some fill and stroke style properties. In particular, try changing the lineJoin and lineCap properties. Use a wider stroke width to see the effect it produces. Note that the lineCap style won't be apparent when drawing polygons—to see it in action, you'll need a line layer, perhaps, using the fells_loop.gpx file we saw in the previous chapter.

Here are a couple of examples:

var countryStyle = new ol.style.Style({
  fill: new ol.style.Fill({
    color: [0, 255, 255, 1]
  }),
  stroke: new ol.style.Stroke({
    color: [127,127,127,1.0],
    width: 10,
    lineJoin: 'bevel',
  })
});
var timezoneStyle = new ol.style.Style({
  stroke: new ol.style.Stroke({
    color: [64, 200, 200, 0.5],
    lineJoin: 'round',
    width: 10
  })
});

Image styles

The image style—ol.style.Image—is used to style point data. You won't be using it directly though. Instead, there are two subclasses that you'll be using: ol.style.Icon and ol.style.Circle. Let's look at the icon style first.

The icon style

The icon style displays an image at the location of a point. There are quite a few properties associated with the icon style that allow you to align the placement of the image relative to the precise geographic location being represented.

Name

Type

Description

anchor

Array.<number>

This property states where to position the image relative to the geographic location of the point specified as an array of two numbers. The default value is [0.5, 0.5] and assumes units of fraction (see anchorXUnits and anchorYUnits). This will specify aligning the center of the image to the geographic location. The position within the image is measured relative to the anchorOrigin property, which defaults to top-left.

anchorOrigin

string

This specifies where the anchor value is measured from. One of the following values can be used:

  • top-left (the default)
  • top-right
  • bottom-left
  • bottom-right

anchorXUnits

string

This specifies the units of the X anchor value, either pixels or fraction. The default value is fraction. When fraction is used, the value of the associated value is a floating point number between 0 and 1 as a percentage of the width or height of the image.

anchorYUnits

string

This specifies the units of the Y anchor value.

crossOrigin

string

The crossOrigin setting for the image allows you to leverage CORS (Cross Origin Resource Sharing) when loading an image.

img

Image

This is an image object to use for the icon. This may be used instead of the src property but the provided image object must already be loaded.

offset

Array.<number>

This is the top-left corner of the image to draw the icon from. When combined with the size property, this would allow you to use an image sprite and selecting a portion of the sprite to display for a specific icon. The default is [0,0].

offsetOrigin

String

This sets the origin of the offset property, one of the following:

  • bottom-left
  • bottom-right
  • top-left (the default)
  • top-right

scale

number

This is a scale factor to use when drawing the image, the default is 1 (do not scale the image). A value of 2 will double the size of the icon and a value of 0.5 will half the size of the icon.

snapToPixel

Boolean

If true, this property will cause images to be snapped to integer pixel values and result in sharper display of images. If false, the image will be placed more accurately but may appear blurry. The default value is true. You may want to set this to false if you are animating an icon's position, as snapping to pixels would cause noticeable jitter.

rotateWithView

Boolean

If true, the icon will rotate when the map's view is rotated. The default is false (always stay upright).

rotation

number

This is a rotation (in radians) to apply to the icon.

size

ol.Size

This is the size of the icon in pixels as an array of two values—width and height. The default value is the size of the image being used. Using a different value, when combined with origin, allows the use of image sprites.

src

string

This is the URL to load the image from.

The following diagram illustrates the meaning of the origin, size, and anchor options:

The icon style