MapBox

Using MapBox tiles in Managing News

The two essential elements of OpenLayers maps are map layers and map presets. Map layers define a certain layer - a basemap of Afhghanistan or points representing news stories. Map presets are collections and configurations of layers into the maps you can browse on a site. In the Drupal features system, map layers are stored in defaults.inc files.

In the ManagingNews install profile, the 'mn_world' feature (in /profiles/managingnews/modules/features/mn_world/) contains the default map layers in the file mn_world.defaults.inc.

A single layer definition within this function looks like this:

$export = array();
$openlayers_layers = new stdClass;
$openlayers_layers->disabled = FALSE;
$openlayers_layers->api_version = 1;

// Your internal name for this layer
$openlayers_layers->name = 'layer-name';
// The layer name which will appear in the layer switcher
$openlayers_layers->title = 'Layer Title (for the layer switcher)';
$openlayers_layers->description = 'A description of the layer.;
$openlayers_layers->data = array(
  'projection' => array(
    '0' => '900913',
  ),
  // Change in order to make this layer into an overlay - otherwise it is a base layer,
  // only one of which can be shown at a time.
  'baselayer' => TRUE,
  'type' => 'MapBox',
  'options' => array(
     // Change this value to correspond to the layername documented on MapBox.com
     // for example, 'layername' => 'world-light'
    'layername' => 'layer-name',
    'spherical_mercator' => TRUE,
    // This contains a subset of the resolutions from serverResolutions
    // Eliminating and adding values to this array restricts the zoom levels permitted
    // on a map
    'resolutions' => array(
      '0' => 2445.98490469,
      '1' => 1222.99245234,
      '2' => 611.496226172,
      '3' => 305.748113086,
      '4' => 152.874056543,
      '5' => 76.4370282715,
      '6' => 38.2185141357,
      '7' => 19.1092570679,
      '8' => 9.55462853394,
    ),
    // Required for layers to function; do not change
    'serverResolutions' => array(
      '0' => 156543.0339,
      '1' => 78271.51695,
      '2' => 39135.758475,
      '3' => 19567.8792375,
      '4' => 9783.93961875,
      '5' => 4891.96980938,
      '6' => 2445.98490469,
      '7' => 1222.99245234,
      '8' => 611.496226172,
      '9' => 305.748113086,
      '10' => 152.874056543,
      '11' => 76.4370282715,
      '12' => 38.2185141357,
      '13' => 19.1092570679,
      '14' => 9.55462853394,
      '15' => 4.77731426697,
      '16' => 2.38865713348,
      '17' => 1.19432856674,
      '18' => 0.597164283371,
    ),
    // Required for layers to function; do not change
    'maxExtent' => array(
      '0' => -20037508.34,
      '1' => -20037508.34,
      '2' => 20037508.34,
      '3' => 20037508.34,
    ),
    'type' => 'png',
  ),
  'events' => array(),
  'callback' => 'featurename_process_layers',
);
// Change layer-name to a unique layer name
$export['layer-name'] = $openlayers_layers;

To add a layer, copy the code above or an existing MapBox layer, and change its settings, remembering to change the last line to differentiate it from other layers. Then add a line to mn_world.info:

features[openlayers_layers][] = "your_layer_name"

After this is done you need to change the layer presets in any of the other features that use a Mapbox map. For instance, to add a layer to fullscreen map preset, edit mn_world.defaults.inc, and find the fullscreen preset, which begins with:

$openlayers_presets->name = 'mn_fullscreen';

Then define the available layers on that map:

    'default_layer' => 'world_light',
    'layers' => array(
      'world_light' => 'world_light',
      'world' => 'world',
      'osmmapnik' => 'osmmapnik',
      'mn_airports' => 'mn_airports',
      'mn_schools' => 'mn_schools',
      'mn_hospitals' => 'mn_hospitals',
      'mn_news_attachment_1' => 'mn_news_attachment_1',
    ),