You are here

function leaflet_mapbox_leaflet_map_info in Leaflet MapBox 8

Same name and namespace in other branches
  1. 7 leaflet_mapbox.module \leaflet_mapbox_leaflet_map_info()

Implements hook_leaflet_map_info().

File

./leaflet_mapbox.module, line 10
"Leaflet Mapbox" adds integration for Leaflet module and Mapbox tiles.

Code

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;
}