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

Debugging on Android

If you have an Android device, you can debug your web applications using the ADB Chrome extension and the Chrome DevTools introduced in Chapter 2, Key Concepts in OpenLayers. There are quite a few steps to get it all working, but it's worth it when you need it! So, here goes:

  1. If Chrome is not already installed by default, install Chrome for Android version 28 or later from Google Play, and connect your device to your computer with a USB. Here's how your screen looks when you perform these actions:
    Debugging on Android
  2. Install Chrome version 28 or later on your development machine. The following screenshot depicts the screen as it should look while performing this action:
    Debugging on Android
  3. Install ADB Chrome Extension available in the Chrome web store by opening a new tab in Chrome, clicking the App Store icon, and then searching for ADB, as shown in the following screenshot:
    Debugging on Android
  4. Enable USB debugging on your device by opening the Developer options settings panel and clicking the checkbox for USB debugging, as follows:
    • For Android 3.2 and older, go to Settings | Applications | Development.
    • For Android 4.0 and newer, go to Settings | Developer.
    • For Android 4.2 and newer, the Developer options panel is hidden by default. To make it available, go to Settings | About phone and tap Build number seven times. Return to the previous screen to find Developer options. Here's how the Developer options screens look during this process:
    Debugging on Android
  5. Connect your Android device to your computer with a USB cable.
  6. Launch Chrome for Android on your device, open Settings | Advanced | Developer Tools, and check off the Enable USB Web debugging option, as shown in the following screenshots:
    Debugging on Android
  7. When you first connect your Android device to your computer, you might see an alert requesting permission to Allow USB debugging. To avoid seeing this again, check Always allow from this computer, and click on OK.
  8. Start debugging in Chrome on your computer with ADB Chrome Extension by clicking on the ADB icon in the Chrome toolbar and selecting Start ADB, as follows:
    Debugging on Android
  9. Click on View Inspection Targets to open the about:inspect page that displays each connected device and its tabs.
  10. Find the mobile device tab you want to debug, and click on the inspect link next to its URL.

What just happened?

Phew! First, we made sure that we installed Chrome for Android on the device and the latest version of Chrome on our computer. Next, we installed ADB Chrome Extension for Chrome. Then, we allowed USB debugging on the device. Finally, we launched ADB Chrome Extension to see it in action. You can now debug your mobile web application running on your device with the Chrome Developer Tools.

Note

It is also possible to debug using Firefox on Android; see https://developer.mozilla.org/en-US/docs/Tools/Remote_Debugging/Firefox_for_Android for more information.

Debug anywhere – WEb INspector REmote (WEINRE)

Native debugging on mobile is great once you get it working, but what do you do when you don't have a USB cable with you, you have an iPhone but not a Mac, or you want to debug on some other device? There's a solution, of course, and the solution is to use WEINRE (WEb INspector REmote), an open source package that gives you an almost native debugging capability for mobile devices.

WEINRE is part of the Apache Cordova project (http://cordova.apache.org/) and was pioneered by Patrick Mueller. We will talk about Apache Cordova in the next section.

Using WEINRE involves combining three separate components, as follows:

  • The debug server: This is an HTTP server provided by WEINRE; it's used by the debug client and the debug target.
  • The debug client: This is the Web Inspector user interface we will use to debug your application. It displays the Elements and Console panels, among other things.
  • The debug target: This refers to both the machine running the browser that we want to debug and the web page being debugged.

The debug client and the debug server run on our development computer and the debug target runs on the mobile device.

To activate the debug target, we will need to add some JavaScript code, provided by the debug server, into the web page.

Getting started with WEINRE

WEINRE is a Node.js application and is published as an NPM package. We've already talked about using Node.js and NPM to run a small server for editing features in Chapter 8, Interacting with Your Map. To install WEINRE then, all we need to do is open a command prompt and run the following command:

npm install –g weinre
Getting started with WEINRE

This is how the command prompt would look when we run the preceding command. This command instructs NPM to install the WEINRE package globally, which will make the weinre command available to you.

To start WEINRE, we need to use the IP address of the development machine—see the beginning of this chapter if you don't remember it. Open a command prompt and run the following command:

weinre –boundHost <your ip address>
Getting started with WEINRE

This is how the command prompt would look when we run the preceding command. The boundHost option allows the debug target's JavaScript to be loaded in a page on another machine, such as our mobile device. There are several other command-line options that you can supply to the weinre command; for the most part, you don't need them, but you can read about them in the WEINRE documentation at http://people.apache.org/~pmuellr/weinre/docs/latest/Running.html.

WEINRE starts a debug server and reports the URL at which the debug server page can be accessed. Copy this URL and open it in a web browser. The following is how the page will look when these actions have been performed:

Getting started with WEINRE

This page has the information and links we need to get started with debugging our mobile web application. The first section, Access Points, contains links to the debug client user interface and documentation. The second section, Target Demos, gives you some quick links to try out WEINRE debugging right away. Try them if you like. The third section contains the link to the JavaScript we will need to add to our mobile web page to activate the debug target code. Let's go ahead with this now.

Open the mobile example page in a code editor. Copy the <script> tag from the following example in the target script section, and paste it into the example page just after the <title> tag, as follows:

<title>Mobile Examples</title>
<script src="http://192.168.209.8:8081/target/target-script-min.js#anonymous"></script>

Now, for the big finale—load the mobile example on your mobile device, and then click on the link to open the debug client on your desktop machine. After that, this is how your screen will look:

Getting started with WEINRE

The Remote tab shows the debug target connected and some other information. You can now click on the Elements, Resources, Network, Timeline, and Console tab. Click on the Elements tab to show the Elements panel. This is how your screen will look after performing these actions:

Getting started with WEINRE

You can use the Elements panel just like Chrome Developer Tools and explore the DOM. Note that it can take a few moments for WEINRE to respond with information, so be patient and wait for it to appear.

The Resources panel shows WebSQL databases and data stored in the local storage and session storage. The Network panel shows XML HTTP requests issued after the page has loaded. Unlike Chrome Developer Tools, it can't capture the assets loaded by the page itself.

The Timeline panel can be used to display timing information about events and to track user-triggered events. Using this panel is beyond the scope of this book, but if you are interested, then check out the WEINRE documentation.

The last tab, Console, displays the JavaScript log and provides a command line for executing arbitrary JavaScript in your web page. Try typing the following command into the console and hit Enter:

alert('test');
Getting started with WEINRE

Your screen will look like this when the preceding command has been entered.

You should see an alert pop up on your mobile device, as shown in the following screenshot:

Getting started with WEINRE

The Console tab also shows log messages, and we can filter by message types (Errors, Warnings, and Logs). Some log messages are generated by the browser itself, typically when an error happens, and we can use WEINRE to see if errors are happening. We can also programmatically send messages to log in our code using methods of the global console object that are available in all web browsers.

WEINRE isn't as complete as native debugging with iOS and Android, but it can certainly help out at a pinch.