It would be a good feature to be able to put the layer switcher outside the map, like this example:

http://openlayers.org/dev/examples/layerswitcher.html

or is there altready a way of doing this?

thanks,

joeysantiago

CommentFileSizeAuthor
#4 layerswitcherdiv-1475316_4.patch1.9 KBevilehk
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

joeysantiago’s picture

Well, a dirty solution has been putting this code in includes/behaviors/js/openlayers_behavior_layerswitcher.js , line 14:

var control = new OpenLayers.Control.LayerSwitcher({
      'ascending': !!data.map.behaviors['openlayers_behavior_layerswitcher'].ascending,
      'roundedCorner' : !!data.map.behaviors['openlayers_behavior_layerswitcher'].roundedCorner,
      'roundedCornerColor' : data.map.behaviors['openlayers_behavior_layerswitcher'].roundedCornerColor,
      'div':OpenLayers.Util.getElement('IdOfMyLayerSwitcherDiv')

    });

probably it would be better to put also some code in the .inc file, in order to enable / disable the feature and add the id of the layer?

zzolo’s picture

Status: Active » Closed (works as designed)

Hey @joeysantiago. Well, what you should do is just create a new layer switcher behavior that allows for that. You can put it in a custom module. This way you can maintain code easier on your project. But yes, this is the best way to go.

michaellenahan’s picture

Thank you for this very useful thread!

Here's a Drupal 7 solution which I am working on.

Once again this is the dirty way to do it, hacking the openlayers module itself.

sites/all/modules/openlayers/plugins/behaviors/openlayers_behavior_layerswitcher.js

/**
 * Layer Switcher Behavior
 */
Drupal.openlayers.addBehavior('openlayers_behavior_layerswitcher', function (data, options) {
  options.ascending = !! options.ascending;

  // Add these lines to move the switcher to an external div
  // (in this case the id of the external div is openlayers-switcher-block)
  options['div'] = OpenLayers.Util.getElement('openlayers-switcher-block');
  Drupal.openlayers.addControl(data.openlayers, 'LayerSwitcher', options);

  // Maximize if needed.
  if (!! options.maximizeDefault == true) {
    data.openlayers.getControlsByClass('OpenLayers.Control.LayerSwitcher')[0].maximizeControl();
  }
});

I'd be grateful for pointers on how to do this properly using a new layerswitcher behavior.

I'm not sure how to implement a new behavior, so my first thought was to override the js file using hook_js_alter().
I ran into this error:
Uncaught TypeError: Cannot call method 'addBehavior' of undefined
... in other words, Drupal.openlayers is not yet defined.
... so that's why for now, I'm unfortunately hacking the openlayers module itself.

For completeness, here is the rest of my custom openlayers_switcher module, it basically provides a block with the div for my layerswitcher to go into.

/**
 * Implementation of hook_block_info().
 *
 * Provides a block which will contain the layer switcher icons.
 *
 * You will need to put this block in the correct region by going to:
 * Structure > Blocks in your browser.
 */
function openlayers_switcher_block_info() {
  $blocks['openlayers-switcher-block'] = array(
    'info' => t('openlayers-switcher-block'),
  );
  return $blocks;
}

/**
 * Implementation of hook_block_view().
 *
 * Renders a block which will contain the layer switcher icons.
 */
function openlayers_switcher_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'openlayers-switcher-block':
      $block = array(
        'content' => array(
          '#markup' => _openlayers_switcher_content(),
        )
      );
      break;
  }
  return $block;
}

/**
 * custom html block
 * @return string
 *
 * Returns an empty div which will contain the layer switcher.
 */
function _openlayers_switcher_content() {
  return "<div id='openlayers-switcher-block'></div>";
}

/**
 * Implements hook_openlayers_map_preprocess_alter().
 *
 * Ensures that the layerswitcher is maximized - this is important as we are 
 * moving it to a div which is outside the map.
 */
function openlayers_switcher_openlayers_map_preprocess_alter(&$map = array()) {
  $map['behaviors']['openlayers_behavior_layerswitcher']['maximizeDefault'] = 1;
}
evilehk’s picture

Status: Fixed » Needs review
FileSize
1.9 KB

I had the same issue of wanting to provide a div id for displaying the layerswitcher outside the map, and thank you michaellenahanu for sharing your module! I solved it in a similar manner, except I added a textfield in the layerswitcher fieldset of the behavior tab in the openlayers map edit screen. This way you can set different div ids for the layerswitcher per map. Here's the patch.

Pol’s picture

Version: 6.x-2.x-dev » 7.x-2.x-dev
Component: OL Styles » OL Behaviors
Status: Closed (works as designed) » Fixed

Great patch ! committed !

You can also check this module, it might help: http://drupal.org/project/olebs

evilehk’s picture

Status: Needs review » Fixed

Thank you, Pol, and thanks for sharing your olebs module. I tried it out and it works great, especially with chosen.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.