You are here

RAW_LAYERS.txt in Openlayers 6.2

Same filename and directory in other branches
  1. 7.2 docs/RAW_LAYERS.txt
Raw layers are an addition to OpenLayers post-alpha7 which allow users to 
manually add points to a layer type. In comparison to the layer_type 
method of pulling in custom data, this allows you to 'push' data into the 
layer data array itself. In any case where reusability is a priority, 
layer_types should be utilized (as documented in LAYER_TYPES.txt). However, 
this is a quick method for success that may be more accessible to more 
developers.

A brief, example-only implementation of an arbitrary layer is below.

/**
 * Implementation of hook_ctools_plugin_api().
 * Required to provide layers
 */
function geolocator_ctools_plugin_api($module, $api) {
  if ($module == "openlayers") {
    switch ($api) {
      case 'openlayers_layers':
        return array('version' => 1);
    }
  }
}

/**
 * One can have the 'features' => element point to a function 
 * or be built on the fly within the _layers method. However, 
 * close attention must be paid to ctools caching in order to 
 * insure that dynamic data stays dynamic
 */
function geolocator_openlayers_layers() {
  $layers = array();
  $layer = new stdClass();
  $layer->api_version = 1;
  $layer->name = 'afghanistan';
  $layer->title = 'One Point on Afghanistan';
  $layer->description = '';
  $layer->data = array(
    'layer_type' => 'openlayers_layer_type_raw',
    'projection' => array('900913'),
    'features' => array(
      array(
      "wkt"=> "POINT(65 33)",
      "projection"=> "4326",
      "attributes"=> 
        array(
          "name"=> "Afghanistan",
          "description"=> "248"
        )
      )
    )
  );
  $layers[$layer->name] = $layer;
  return $layers;
}

/**
 * map_preprocess_alter allows one to add a new layer to a map
 * before layers are rendered and data is pulled from them.
 */
function geolocator_openlayers_map_preprocess_alter(&$map) {
  $map['layers']['afghanistan'] = 'afghanistan';
  $map['layer_activated']['afghanistan'] = 'afghanistan';
  $map['layer_switcher']['afghanistan'] = 'afghanistan';
}

File

docs/RAW_LAYERS.txt
View source
  1. Raw layers are an addition to OpenLayers post-alpha7 which allow users to
  2. manually add points to a layer type. In comparison to the layer_type
  3. method of pulling in custom data, this allows you to 'push' data into the
  4. layer data array itself. In any case where reusability is a priority,
  5. layer_types should be utilized (as documented in LAYER_TYPES.txt). However,
  6. this is a quick method for success that may be more accessible to more
  7. developers.
  8. A brief, example-only implementation of an arbitrary layer is below.
  9. /**
  10. * Implementation of hook_ctools_plugin_api().
  11. * Required to provide layers
  12. */
  13. function geolocator_ctools_plugin_api($module, $api) {
  14. if ($module == "openlayers") {
  15. switch ($api) {
  16. case 'openlayers_layers':
  17. return array('version' => 1);
  18. }
  19. }
  20. }
  21. /**
  22. * One can have the 'features' => element point to a function
  23. * or be built on the fly within the _layers method. However,
  24. * close attention must be paid to ctools caching in order to
  25. * insure that dynamic data stays dynamic
  26. */
  27. function geolocator_openlayers_layers() {
  28. $layers = array();
  29. $layer = new stdClass();
  30. $layer->api_version = 1;
  31. $layer->name = 'afghanistan';
  32. $layer->title = 'One Point on Afghanistan';
  33. $layer->description = '';
  34. $layer->data = array(
  35. 'layer_type' => 'openlayers_layer_type_raw',
  36. 'projection' => array('900913'),
  37. 'features' => array(
  38. array(
  39. "wkt"=> "POINT(65 33)",
  40. "projection"=> "4326",
  41. "attributes"=>
  42. array(
  43. "name"=> "Afghanistan",
  44. "description"=> "248"
  45. )
  46. )
  47. )
  48. );
  49. $layers[$layer->name] = $layer;
  50. return $layers;
  51. }
  52. /**
  53. * map_preprocess_alter allows one to add a new layer to a map
  54. * before layers are rendered and data is pulled from them.
  55. */
  56. function geolocator_openlayers_map_preprocess_alter(&$map) {
  57. $map['layers']['afghanistan'] = 'afghanistan';
  58. $map['layer_activated']['afghanistan'] = 'afghanistan';
  59. $map['layer_switcher']['afghanistan'] = 'afghanistan';
  60. }