Table of Contents for
OpenLayers 3.x Cookbook - Second Edition

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition OpenLayers 3.x Cookbook - Second Edition by Antonio Santiago Perez Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. OpenLayers 3.x Cookbook Second Edition
  4. OpenLayers 3.x Cookbook Second Edition
  5. Credits
  6. About the Authors
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Sections
  13. Conventions
  14. Reader feedback
  15. Customer support
  16. 1. Web Mapping Basics
  17. Creating a simple fullscreen map
  18. Playing with the map's options
  19. Managing the map's stack layers
  20. Managing the map's controls
  21. Moving around the map view
  22. Restricting the map's extent
  23. 2. Adding Raster Layers
  24. Using Bing imagery
  25. Using OpenStreetMap imagery
  26. Adding WMS layers
  27. Changing the zoom effect
  28. Changing layer opacity
  29. Buffering the layer data to improve map navigation
  30. Creating an image layer
  31. Setting the tile size in WMS layers
  32. 3. Working with Vector Layers
  33. Adding a GML layer
  34. Adding a KML layer
  35. Creating features programmatically
  36. Exporting features as GeoJSON
  37. Reading and creating features from a WKT
  38. Using point features as markers
  39. Removing or cloning features using overlays
  40. Zooming to the extent of a layer
  41. Adding text labels to geometry points
  42. Adding features from a WFS server
  43. Using the cluster strategy
  44. Reading features directly using AJAX
  45. Creating a heat map
  46. 4. Working with Events
  47. Creating a side-by-side map comparator
  48. Implementing a work-in-progress indicator for map layers
  49. Listening for the vector layer features' events
  50. Listening for mouse or touch events
  51. Using the keyboard to pan or zoom
  52. 5. Adding Controls
  53. Adding and removing controls
  54. Working with geolocation
  55. Placing controls outside the map
  56. Drawing features across multiple vector layers
  57. Modifying features
  58. Measuring distances and areas
  59. Getting feature information from a data source
  60. Getting information from a WMS server
  61. 6. Styling Features
  62. Styling layers
  63. Styling features based on geometry type
  64. Styling based on feature attributes
  65. Styling interaction render intents
  66. Styling clustered features
  67. 7. Beyond the Basics
  68. Working with projections
  69. Creating a custom control
  70. Selecting features by dragging out a selection area
  71. Transitioning between weather forecast imagery
  72. Using the custom OpenLayers library build
  73. Drawing in freehand mode
  74. Modifying layer appearance
  75. Adding features to the vector layer by dragging and dropping them
  76. Making use of map permalinks
  77. Index

Adding a GML layer

The Geography Markup Language (GML) is an XML grammar that is used to express geographic features. It is an OGC standard and is very widely accepted by the GIS community.

In this recipe (the source code is in ch03/ch03-gml-layer/), we will show you how to create a vector layer from a GML file which can be seen in the following screenshot:

Adding a GML layer

Note

You can find the necessary files in the GML format attached to the source code of this book on the Packt Publishing website.

How to do it…

In order to import features in GML format into the map, follow these instructions:

  1. Create an HTML file with the required OpenLayers dependencies and prepare the div element to hold the map:
    <div id="js-map"></div>
  2. Create the JavaScript file to initialize the map with a base layer, then add the vector layer pointing to the GML source, as follows:
    var map = new ol.Map({
      view: new ol.View({
        zoom: 4,
        center: [-7494000, 2240000]
      }),
      target: 'js-map',
      layers: [
        new ol.layer.Tile({
          source: new ol.source.MapQuest({layer: 'osm'})
        }),
        new ol.layer.Vector({
          source: new ol.source.Vector({
            url: 'bermuda-triangle.gml',
            format: new ol.format.GML2()
          })
        })
      ]
    });

How it works…

Let's take a look at the vector layer instantiation in detail:

new ol.layer.Vector({
  source: new ol.source.Vector({
    url: 'bermuda-triangle.gml',
    format: new ol.format.GML2()
  })
})

Just as raster layers require a source, as seen in Chapter 2, Adding Raster Layers, so do vector layers. Vector layers use the ol.source.Vector class in order to present features.

We provide an HTTP endpoint as a string to the url property. OpenLayers will make an AJAX request on our behalf for this URL, which contains the GML file.

We inform OpenLayers what the format of the source is by setting the format property to GML2. The GML source is at version 2.1.2, which ol.format.GML2 can read and process. For GML 3.1.1, use the ol.format.GML or ol.format.GML3 constructors.

In this example, the vector layer source request will load all of the features from the source on to the map and make no further AJAX requests, implicitly using the ol.loadingstrategy.all strategy. However, there is a property of ol.source.Vector called strategy, which can be used to specify an alternative feature loading strategy, such as loading features only for the extent of the view (ol.loadingstrategy.bbox).

On receiving the GML source, OpenLayers reads the GML-formatted features and converts each feature into a format that can be used on the map (ol.Feature).

OpenLayers offers many other formats to read/write data, but in this recipe, we made use of the ol.format.GML2 instance because our data source is a GML version 2 file.

See also

  • The Adding a KML layer recipe
  • The Creating features programmatically recipe