You are here

leaflet_mapbox.module in Leaflet MapBox 8

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

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

File

leaflet_mapbox.module
View source
<?php

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

/**
 * Implements hook_leaflet_map_info().
 */
function leaflet_mapbox_leaflet_map_info() {
  $config = \Drupal::config('leaflet_mapbox.settings');
  $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' => $config
      ->get('zoomlevel'),
    'zoomAnimation' => TRUE,
    'zoomControl' => TRUE,
  );

  // Get access token.
  $token = $config
    ->get('token');
  $options = array(
    'attribution' => '',
  );
  $url_template = '';
  switch ($config
    ->get('api_version')) {
    case '3':
      $code = $config
        ->get('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 = $config
        ->get('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' => $config
      ->get('label'),
    'description' => $config
      ->get('description'),
    'settings' => $settings,
    'layers' => array(
      'earth' => array(
        'urlTemplate' => $url_template,
        'options' => $options,
      ),
    ),
  );
  return $map_info;
}

Functions