You are here

LAYER_TYPES.txt in Openlayers 6.2

Same filename and directory in other branches
  1. 7.2 docs/LAYER_TYPES.txt
Current for 6.x-2.x

# Layer Types

Layer types are one of the fundamental building blocks of the OpenLayers module.
They are factories for layers themselves - a layer type is like a wrapper around
an OpenLayers (javascript) object which lets you configure settings through the
UI and also encapsulate other tasks that layers need to do, like loading
information.

## Structure

Typically a layer type is a class that extends `openlayers_layer_type`, although
it can extend another layer type class if necessary. All that's really needed is
that it implements the right methods. Which are...

* `render`: The `render(&$map)` function is called on each layer type when a map
  is rendered. Since the map array is passed by reference, this function can
  modify it in any way it wishes. However, typically render functions just
  include layer javascript and then return their options array.
* `settings_form`: The settings form of a layer type is different from the
  options form in two very important ways. The first, practical reason for their
  separation is that layer type settings are settings that you'll want to set
  for an entire website, like a Google Maps API key or javascript location. So,
  these settings are not attached to individual layers. The other, technical
  difference, is that, while layer *options* end up in the data array, which is
  serialized and exported, etc., layer *settings* are saved as Drupal variables,
  so that they can optionally intercepted by modules like Spaces, which allow
  users to customize domain-specific settings (like API keys) by space.
* `options_form`: The options form of the layer is what the user sees when they
  land on the layer add form. The results of this form submission are
  automatically saved as the contents of the layer's 'data' property, which is
  then sent to javascript and, depending on layer type, pushed into the
  Javascript layer constructor. This means that exported layers can have more
  properties than the OpenLayers Drupal Module knows about, and they will be
  seamlessly pushed into Javascript and serve their function in Javascript-land.
* `options_init`: The options_init function defines the default options of any
  given layer. This is used for options that will not be set in the options
  form, but would be awkward to code as hidden fields. If your layer type class
  has the correct __construct implementation (like those in the OpenLayers
  Module), then these settings will be added whenever you initialize a layer

## Vector

* Map objects can contain an attribute called 'vector', defined in options_init():

  function options_init() {
    return array(
      'vector' => TRUE,
    );
  }

* This is an important attribute - it designates layers that are derived from
  the OpenLayers Vector layer class [1]. Unlike tile or image-based layers -
  like OpenStreetMap or Google Maps, these will typically be from data files
  like KML, GML, or OpenLayers Data layers. And also unlike image-based maps,
  they don't need to be in the projection of the rest of the map, since they are
  easily reprojected by the OpenLayers Javascript library. So, it is possible to
  have a WMS layer in the EPSG:4326 projection with KML data on it, and also put
  that KML data on a Google Maps map in the EPSG:900913 projection, and the data
  will be displayed on both. Thus setting the vector attribute allows certain
  layers to be added to maps of any projection.

[^1]: http://dev.openlayers.org/releases/OpenLayers-2.9.1/doc/apidocs/files/OpenLayers/Layer/Vector-js.html

## Javascript

OpenLayers Layer Types typically have a bit of Javascript accompanying them
which follows a certain form. It will provide a method in the
`Drupal.openlayers.layer` namespace, like `Drupal.openlayers.layer.cloudmade`,
takes arguments `(name, map, options)`, and will return a fully initialized map
object. These are basically factories for OpenLayers Layer Type objects, and are
usually under 50 lines long. Note that if you plan on making a distinctly
different layer type, it's best not to do it within this javascript file, but to
create a new OpenLayers Layer Type (in javascript) - see the MapBox module for
an example of a new OpenLayers Layer Type (`OpenLayers.Layer.MapBox`), which is
entirely independent of the Drupal module.

File

docs/LAYER_TYPES.txt
View source
  1. Current for 6.x-2.x
  2. # Layer Types
  3. Layer types are one of the fundamental building blocks of the OpenLayers module.
  4. They are factories for layers themselves - a layer type is like a wrapper around
  5. an OpenLayers (javascript) object which lets you configure settings through the
  6. UI and also encapsulate other tasks that layers need to do, like loading
  7. information.
  8. ## Structure
  9. Typically a layer type is a class that extends `openlayers_layer_type`, although
  10. it can extend another layer type class if necessary. All that's really needed is
  11. that it implements the right methods. Which are...
  12. * `render`: The `render(&$map)` function is called on each layer type when a map
  13. is rendered. Since the map array is passed by reference, this function can
  14. modify it in any way it wishes. However, typically render functions just
  15. include layer javascript and then return their options array.
  16. * `settings_form`: The settings form of a layer type is different from the
  17. options form in two very important ways. The first, practical reason for their
  18. separation is that layer type settings are settings that you'll want to set
  19. for an entire website, like a Google Maps API key or javascript location. So,
  20. these settings are not attached to individual layers. The other, technical
  21. difference, is that, while layer *options* end up in the data array, which is
  22. serialized and exported, etc., layer *settings* are saved as Drupal variables,
  23. so that they can optionally intercepted by modules like Spaces, which allow
  24. users to customize domain-specific settings (like API keys) by space.
  25. * `options_form`: The options form of the layer is what the user sees when they
  26. land on the layer add form. The results of this form submission are
  27. automatically saved as the contents of the layer's 'data' property, which is
  28. then sent to javascript and, depending on layer type, pushed into the
  29. Javascript layer constructor. This means that exported layers can have more
  30. properties than the OpenLayers Drupal Module knows about, and they will be
  31. seamlessly pushed into Javascript and serve their function in Javascript-land.
  32. * `options_init`: The options_init function defines the default options of any
  33. given layer. This is used for options that will not be set in the options
  34. form, but would be awkward to code as hidden fields. If your layer type class
  35. has the correct __construct implementation (like those in the OpenLayers
  36. Module), then these settings will be added whenever you initialize a layer
  37. ## Vector
  38. * Map objects can contain an attribute called 'vector', defined in options_init():
  39. function options_init() {
  40. return array(
  41. 'vector' => TRUE,
  42. );
  43. }
  44. * This is an important attribute - it designates layers that are derived from
  45. the OpenLayers Vector layer class [1]. Unlike tile or image-based layers -
  46. like OpenStreetMap or Google Maps, these will typically be from data files
  47. like KML, GML, or OpenLayers Data layers. And also unlike image-based maps,
  48. they don't need to be in the projection of the rest of the map, since they are
  49. easily reprojected by the OpenLayers Javascript library. So, it is possible to
  50. have a WMS layer in the EPSG:4326 projection with KML data on it, and also put
  51. that KML data on a Google Maps map in the EPSG:900913 projection, and the data
  52. will be displayed on both. Thus setting the vector attribute allows certain
  53. layers to be added to maps of any projection.
  54. [^1]: http://dev.openlayers.org/releases/OpenLayers-2.9.1/doc/apidocs/files/OpenLayers/Layer/Vector-js.html
  55. ## Javascript
  56. OpenLayers Layer Types typically have a bit of Javascript accompanying them
  57. which follows a certain form. It will provide a method in the
  58. `Drupal.openlayers.layer` namespace, like `Drupal.openlayers.layer.cloudmade`,
  59. takes arguments `(name, map, options)`, and will return a fully initialized map
  60. object. These are basically factories for OpenLayers Layer Type objects, and are
  61. usually under 50 lines long. Note that if you plan on making a distinctly
  62. different layer type, it's best not to do it within this javascript file, but to
  63. create a new OpenLayers Layer Type (in javascript) - see the MapBox module for
  64. an example of a new OpenLayers Layer Type (`OpenLayers.Layer.MapBox`), which is
  65. entirely independent of the Drupal module.