Last updated June 15, 2012. Created by tmcw on November 11, 2009.
Edited by ifrik, tom_o_t, zzolo. Log in to edit this page.

The OpenLayers module supports layer types, which are ctools plugins. Layer types are related to layers: layers are exportables which contain per-layer configuration, like map server URLs and parameters. Layer types are plugins which provide the forms necessary to create new layers and the functionality of those layers on a map. For instance, the openlayers_views layer type contains the method required to pull data from views and insert it into its own features array.

For instance, the code of the MapBox layer type is

<?php
class openlayers_layer_type_mapbox extends openlayers_layer_type {
 
/**
   * Provide initial values for options.
   */
 
function options_init() {
    return array(
     
'maxExtent' => array(-20037508.34,-20037508.34,20037508.34,20037508.34),
     
'baselayer' => TRUE,
     
'serverResolutions' => openlayers_get_resolutions('900913'),
     
'resolutions' => openlayers_get_resolutions('900913'),
     
'maxExtent' => openlayers_get_extent('900913'),
     
'projection' => array('900913'),
     
'baselayer' => TRUE,
     
'layer_handler' => 'MapBox',
    );
  }
 
/**
   * Options form which generates layers
   */
 
function options_form() {
    return array(
     
'layername' => array(
       
'#type' => 'textfield',
       
'#title' => t('Layer Name'),
       
'#default_value' => $this->data['layername']
      ),
     
'layer_type' => array(
       
'#type' => 'hidden',
       
'#value' => 'openlayers_layer_type_mapbox'
     
),
     
'projection' =>
        array(
        
'0' => array(
         
'#type' => 'hidden',
         
'#value' => '900913',
        ),
      ),
    );
  }
 
/**
   * Render.
   */
 
function render(&$map) {
   
drupal_add_js(drupal_get_path('module', 'mapbox') .'/includes/layer_types/mapbox.js');
    return
$this->options;
  }
}
?>

Details

render() is called when the map is built + displayed, and it can set details of the layer by referencing $this

options_form() is called when the user chooses to add a new layer of this type.

settings_form() can be defined to allow users to set per-layer-type settings, like API keys, which will be automatically stored as variables.

options_init() stores properties of the layer which are not configurable by default.

Layer Type Javascript

Layer types typically include javascript files that are factories for OpenLayers.Layer.* objects. For example, the javascript file for TMS layers is

Drupal.openlayers.layer.tms = function (name, map, options) {
  var styleMap = Drupal.openlayers.getStyleMap(map, options.name);
    if (options.maxExtent !== undefined) {
      options.maxExtent = new OpenLayers.Bounds.fromArray(options.maxExtent);
    }
    if (options.type === undefined){
      options.type = "png";
    }
    options.projection = new OpenLayers.Projection('EPSG:'+options.projection);
    var layer = new OpenLayers.Layer.TMS(name, options.base_url, options);
    layer.styleMap = styleMap;
    return layer;
};

and the TMS layer type specifies that its data should be run through this function by specifying 'layer_handler' => 'tms' in its options_init() function. For each layer, if the layer handler is defined, then it is run.

      if (options.layer_handler !== undefined && Drupal.openlayers.layer[options.layer_handler] !== undefined) {
        var layer = Drupal.openlayers.layer[options.layer_handler](name, map, options);

Implementing New Layer Types

See openlayers.module, where openlayers_openlayers_layer_types defines arrays of ctools plugin info specifying the classes and files associated with layer plugins.

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Please post on the OpenLayers Issue queue with any bug reports and support requests. We cannot provide support on Book Pages.