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

Time for action – creating animated maps

The best way to understand these animation functions is to try them out. Start from the previous example:

  1. First, add some buttons to trigger the animation effects:
    <button onclick="doBounce(london);">Bounce To London</button>
    <button onclick="doBounce(rome);">Bounce To Rome</button
    <button onclick="doPan(london);">Pan To London</button>
    <button onclick="doPan(rome);">Pan To Rome</button>
    <button onclick="doRotate();">Rotate</button>
    <button onclick="doZoom(2);">Zoom Out</button>
    <button onclick="doZoom(0.5);">Zoom In</button>

    These are regular HTML buttons that call a function when clicked. We'll add the functions in a moment.

  2. Next, add a new location for Rome, next to the line where we defined the location of London:
    var rome = ol.proj.transform([12.5, 41.9], 'EPSG:4326', 'EPSG:3857');
  3. Now, we'll add functions at the end of our <script> tag to handle the button clicks. Start with the doBounce() function:
    function doBounce(location) {
      var bounce = ol.animation.bounce({
        resolution: map.getView().getResolution() * 2
      });
      var pan = ol.animation.pan({
        source: map.getView().getCenter()
      });
      map.beforeRender(bounce);
      map.beforeRender(pan);
      map.getView().setCenter(location);
    }
  4. Next, add the doPan() function:
    function doPan(location) {
      var pan = ol.animation.pan({
        source: map.getView().getCenter()
      });
      map.beforeRender(pan);
      map.getView().setCenter(location);
    }
  5. Next, add the doRotate() function:
    function doRotate() {
     var rotate = ol.animation.rotate({
     rotation  : Math.PI * 2
     });
     map.beforeRender(rotate);
    }
  6. Finally, add the doZoom() function:
    function doZoom(factor) {
      var resolution = map.getView().getResolution();
      var zoom = ol.animation.zoom({
        resolution: resolution
      });
      map.beforeRender(zoom);
      map.getView().setResolution(resolution * factor);
    }
  7. Now, reload the example in your web browser and try clicking on the buttons. You should see the map zoom, pan, and bounce between London and Rome.

What just happened?

Not too bad! In a few lines of code, we managed to create some pretty impressive animation effects using ol.animation functions and the Map's beforeRender() method.

In step 3, we created the doBounce() function. This function takes a single parameter, location, which is the location that we want to bounce to. The effect we want to achieve is to smoothly zoom out from our current location and then into the new location. We called ol.animation.bounce() to create a function that implements the bounce animation effect. For this example, we provided only the resolution property and let the other properties (start, duration, and easing) take their default values. We set the resolution property to two times the current resolution of the map's view. This is equivalent to clicking the ZoomOut control (the button in the top-left corner of the map with the minus sign) once. What this does is smoothly zoom out from the current resolution to the next zoom level, and then back into the current resolution.

Next, we created a function that implements the pan animation effect. Again, we used the defaults for start, duration, and easing by not specifying them and set the source property to the current center of the map's view. We added both bounce and pan functions to the map by calling beforeRender(); then, we changed the map's view to go to the location we passed into this function.

In step 4, the doPan() function takes a single parameter, location, and smoothly pans the map to it. To do this, we called ol.animation.pan() with the map's current center for the source as we did in the doBounce() function, then we told the map's view to go to the new location.

In step 5, the doRotate() function doesn't take any parameters; we just wanted to spin the map 360 degrees. We used the ol.animation.rotate() function for this and set the rotation option to specify how much to rotate. When we add this to the map using beforeRender(), the effect happens immediately.

Tip

In OpenLayers, rotation is always specified in radians. When we think about rotation, we normally think in degrees. To convert from degrees to radians, it is useful to remember that 180 degrees = PI * radians. We don't need to know the value of PI, JavaScript provides us with the useful constant, Math.PI. The formula to convert degrees to radians is var radians = degrees * Math.PI / 180;. The inverse is var degrees = radians * 180 / Math.PI;. In our example, we wanted to rotate the map 360 degrees. Using the formula above, this becomes 2 * Math.PI, which is the value we used.

Step 6 adds the doZoom() function. It takes a single parameter, factor, which is the amount to zoom. We used ol.animation.zoom() to create our animation function using the view's current resolution as our starting point. Then, we added our animation function and told the map's view to zoom to a new resolution by multiplying the current resolution by the factor parameter. A factor of two will cause the map to zoom out to the next zoom level and a factor of 0.5 will cause the map to zoom in to the next zoom level.

Have a go hero – exploring animation properties

Now that we've seen how the basic animations work, try modifying the animation properties in each function to see how to change its parameters and overriding the default duration, start time and easing functions. You can also try combining the animations in different ways.

Conversion methods

When we click on a web page, the browser generates a MouseEvent that contains, among other things, the position that the click happened at. This position is in pixels and is relative to the browser window. In an OpenLayers application, we will often want to respond to the user interacting with the map and it is important to understand these events specify the position in pixels. It is common to need to determine the geographic coordinate that corresponds to this position. OpenLayers provides several methods that allow us to convert between the browser's pixel space and geographic coordinates:

Method

Parameters

Description

getCoordinateFromPixel( pixel )

pixel – ol.Pixel

This method converts a pixel position to a geographic coordinate and returns an ol.Coordinate. The pixel position is relative to the HTML element the map is contained in.

getPixelFromCoordinate( coordinate )

coordinate – ol.Coordinate

This method converts a geographic coordinate into a pixel position relative to the HTML element that the map is contained in and returns an ol.Pixel.

getEventCoordinate( event )

event – BrowserEvent

This method computes the geographic coordinate from a browser event (such as a click or mouse move) and returns an ol.Coordinate.

getEventPixel( event )

event - BrowserEvent

The method computes the pixel location of a browser event relative to the map's HTML element and returns an ol.Pixel.

Other methods

The map object contains a few other methods that don't neatly fit into the previous groups; so, we've included them here for completeness:

Method

Parameters

Description

forEachFeatureAtPixel(pixel, callback, opt_this, opt_layerFilter, opt_this2 )

  • pixel – ol.Pixel
  • callback – function
  • opt_this – object, or null
  • opt_layerFilter – function or null
  • opt_this2 – object or null

This method queries all layers contained in the map for any features that intersect the pixel location provided. The callback function will be called once for each feature found with the feature as the first argument and the layer it was found in as the second argument. If provided, opt_this will be used as the context for the callback function (the value of this inside the function). By default, all layers will be queried; however, you can choose which layers to query by providing a function for the opt_layerFilter argument. This function will be called once for each layer, passing the layer as the only argument, and is expected to return true if the layer is to be queried and false if the layer is not to be queried. The final opt_this2 argument will be used as the context for the layer filter function if provided.

getViewport( )

None

This method returns the HTML element that the map is contained within. Unlike the map's target property, which may return either a string or an HTML element, this method will always return the HTML element.

updateSize()

None

This method tells the map to recalculate its size based on its container. This is used when other code changes the size of the map's target element.