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 – finding your mouse position

Follow these steps to get started with the mouse position behavior:

  1. Reuse the usual HTML for the chapter by copying it in a new file.
  2. Now, edit the file to be sure that you create an HTML element with an ID myposition such as <div id="myposition"></div>.
  3. Declare a JavaScript variable that references the control:
    var mousePosition = new ol.control.MousePosition({
      coordinateFormat: ol.coordinate.createStringXY(2),
      projection: 'EPSG:4326',
      target: document.getElementById('myposition'),
      undefinedHTML: '&nbsp;'
    });
  4. Add the control to the map without forgetting that this operation is only available after you create the map:
    map.addControl(mousePosition);
  5. See the result in your browser and hover with your mouse the map.
  6. Go back in your HTML code and add the following code in your CSS file, assets/css/samples.css:
    #myposition > .ol-mouse-position {
      position: relative;
      margin-left: 20px;
      font-size: 30px;
    }
  7. Reload your page and you will see a screenshot like the following:
    Time for action – finding your mouse position

What just happened?

Here, we choose to use most of the options of the controls.

The first one, coordinateFormat, accepts an ol.CoordinateFormatType. It simply means that when you retrieve the coordinates, you may want to change them to Degrees Minutes Seconds notation or you may want to change the precision you display in the HTML file.

The two relevant options to set here are:

  • ol.coordinate.createStringXY(2): This is where 2 is the precision you expect
  • ol.coordinate.toStringHDMS: This is useful only if you use degrees units and the result will look like 51° 30′ 33″ N "8° 49′ 22″ E"

Be careful to not set ol.coordinate.toStringHDMS() instead; it's a bit surprising, but you need to use the class itself.

The projection option helps you choose the coordinates you want. You may have forgotten but each projection has a defined unit system (and subunits). For example, the EPSG:4326 projection returns units in a decimal degree. It's what you see in the bottom of the previous image. The undefinedHTML option just sets what you want to display when you are not hovering over the map. You can confirm &nbsp; availability by searching in the Chrome debugger Elements panel, the string in precedent example.

The target option and the CSS part were only to remind you of previous use cases.

We've never really insist until now, but you can also set the parameters after controls creation. In fact, for each property, you always have a setter and a getter.

Just open your browser with the example, type in the console the following line, and hover over the map to understand:

mousePosition.setProjection(ol.proj.get('EPSG:3857'))

As you see, you can really explore the methods available in the ol.control.MousePosition control. We really encourage you to play in the console using auto completion: you will see that you can really find useful things that may remind you of other examples. You should also focus on the Fires: part of the API documentation at http://openlayers.org/en/v3.0.0/apidoc/ol.control.MousePosition.html because it will help you to apply your events knowledge in the context of controls and here, the mousePosition control.

The ol.control.ScaleLine control

If you remember, we already introduced you to this control in the Chapter 7, Wrapping Our Heads Around Projections at a functional level.

The purpose of ol.control.ScaleLine is to show a scale line bar to give people an overview of scale and distance. Be aware that it is only useful for projection that keeps distances.

You just need to add it to the map with something such as map.addControl(new ol.control.ScaleLine()) if you don't use the options.

ScaleLine options

The following is the options list you can set for the scale line control:

Name

Type

Description

className

string | undefined

This property sets the CSS class name for the control. The default value is ol-scale-line.

minWidth

number | undefined

This option sets the minimum width in pixels.

target

Element | undefined

This option sets the target, to the DOM element where you want to display your control.

units

ol.control.ScaleLineUnits | undefined

This property sets the units you want to use in your scale line. The supported values are degrees, imperial, nautical, metric, and US.

Have a go hero – discovering ol.control.ScaleLine specific parameters

Although this book is about introducing the OpenLayers 3 library, we suppose that if you are reading this, it also means you want to understand what you are doing. So, let's give you some tasks to do by yourself:

  • Reuse the usual chapter code.
  • Set the minWidth property to a value you want in pixels.
  • Zoom in, zoom out, and pan to see the control behavior.
  • Change units in the scale line (the ol.control.ScaleLineUnits description is available in the following content). When you want to display your map legend using others units, you need to know the available units. You can find them using the table that follows, extracted from the ol.control.ScaleLineUnits type definitions in the OpenLayers 3 API:

    Name

    Type

    Default

    degrees

    string

    This property returns the the string degrees required for internal library units changes. Check out Wikipedia to learn more on this unit (decimal degree) at http://en.wikipedia.org/wiki/Decimal_degrees.

    imperial

    string

    This property is similar to previous property but returns the string imperial. Check out Wikipedia to learn more about the unit at http://en.wikipedia.org/wiki/Imperial_units.

    nautical

    string

    This property is similar to the previous property but returns the string nautical. It refers to nautical miles. Check out Wikipedia for more information at http://en.wikipedia.org/wiki/Nautical_mile.

    metric

    string

    This property is similar to the previous property, but returns the string metric that refers to metre or meter. Check out Wikipedia for an history at http://en.wikipedia.org/wiki/Metre.

    us

    string

    This property is similar to the previous property but returns the string US. It refers to US units. Check out Wikipedia for more information at http://en.wikipedia.org/wiki/United_States_customary_units.

  • Inspect the HTML element using the debugger
  • What CSS rules apply to the elements within the DOM element control
  • Tweak the control color
  • Change the scale line position with CSS, the target property or both of them

The ol.control.ZoomSlider control

The ol.control.ZoomSlider control helps you to see your zoom levels using a slider. We advise you to go to the official example at http://openlayers.org/en/v3.0.0/examples/zoomslider.html because it's well illustrated for CSS styling. You can display the slider horizontally or vertically for the example illustrated in the following screenshot:

The ol.control.ZoomSlider control

ZoomSlider options

The following content presents the available options/properties in ol.control.ZoomSlider.

Name

Type

Description

className

string | undefined

This property sets the class name for the control.

maxResolution

number | undefined

This option defines the maximum resolution.

minResolution

number | undefined

This option sets the minimum resolution.

You can manage and try to better understand resolutions by using the getResolution() function at the view level (with map.getView()).

The ol.control.ZoomToExtent control

The ol.control.ZoomToExtent control permits you to create a button to go to a particular extent.

In a real context, it may help you to get a map with a zoom on a country, and using this control, you will be able to zoom directly on a particular city. In another case, you may want to go back to the initial extent of your map, and this control is also a way to address this requirement.