You are here

leaflet_mapbox.module in Leaflet MapBox 7

Same filename and directory in other branches
  1. 8 leaflet_mapbox.module

"Leaflet Mapbox" adds integration for Leftlet module and Mapbox tiles.

File

leaflet_mapbox.module
View source
<?php

/**
 * @file
 * "Leaflet Mapbox" adds integration for Leftlet module and Mapbox tiles.
 */

/**
 * Implements hook_menu().
 */
function leaflet_mapbox_menu() {
  $items = array();
  $items['admin/config/services/leaflet-mapbox'] = array(
    'title' => 'Leaflet Mapbox',
    'description' => 'Leaflet Mapbox configuration panel.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'leaflet_mapbox_configuration_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'leaflet_mapbox.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_leaflet_map_info().
 */
function leaflet_mapbox_leaflet_map_info() {
  $settings = array(
    'attributionControl' => TRUE,
    'closePopupOnClick' => TRUE,
    'doubleClickZoom' => TRUE,
    'dragging' => TRUE,
    'fadeAnimation' => TRUE,
    'layerControl' => FALSE,
    'maxZoom' => 19,
    'minZoom' => 0,
    'scrollWheelZoom' => TRUE,
    'touchZoom' => TRUE,
    'trackResize' => TRUE,
    'zoom' => variable_get('leaflet_mapbox_zoomlevel', 2),
    'zoomAnimation' => TRUE,
    'zoomControl' => TRUE,
  );

  // Get access token.
  $token = variable_get('leaflet_mapbox_token', '');
  $options = array(
    'attribution' => '',
  );
  switch (variable_get('leaflet_mapbox_api_version', '3')) {
    case '3':
      $code = variable_get('leaflet_mapbox_code', '');

      // Build urlTemplate.
      $url_template = "//{s}.tiles.mapbox.com/v4/{$code}/{z}/{x}/{y}.png?access_token={$token}";
      break;
    case '4':

      // Extract username and styleid from style url.
      $style_url = variable_get('leaflet_mapbox_style_url', '');
      preg_match('/^mapbox:\\/\\/styles\\/(\\S*)\\/(\\S*)$/', $style_url, $matches);
      if (count($matches)) {
        $username = $matches[1];
        $styleid = $matches[2];
      }

      // Build urlTemplate.
      $url_template = "//api.mapbox.com/styles/v1/{$username}/{$styleid}/tiles/{z}/{x}/{y}?access_token={$token}";

      // Mapbox v4 tiles are 512px by 512px and offset by 1 zoom level.
      $options += array(
        'tileSize' => 512,
        'zoomOffset' => -1,
      );
      break;
  }
  $map_info['leaflet-mapbox'] = array(
    'label' => variable_get('leaflet_mapbox_label', ''),
    'description' => variable_get('leaflet_mapbox_description', ''),
    'settings' => $settings,
    'layers' => array(
      'earth' => array(
        'urlTemplate' => $url_template,
        'options' => $options,
      ),
    ),
  );
  return $map_info;
}

Functions