Hi,
it seems there is no possibility to zoom map at the display with cck openlayer addon.
I have installed Openlayers and everything works perfect. Just, I have a problem with displaying a map in a node.
The problem is that a map is always at same position an has same zoom.
What I would like is to zoom display map and center it where the polyline starts.
Is this possible, even via changing a code.
I need this for a project
Thanks in advance

Comments

zzolo’s picture

Category: feature » support

neddim, make sure you are using newest dev version.

There is no current way to zoom_to_features on a formatter. The main problem is that cck formatters dont have any options screen.

What you can do, is create a basic map with the zoom to features behavior in a spearate module with hook_openlayers_presets(), then it will be available in the presets, and you can clone and do whatever from there.

that0n3guy’s picture

So... if zoom to features can just be added on a separate module to be shown in a preset. Why can't zoom to features be added in the "controls" section of any preset? (i'm no programmer.. .just wondering)

phayes’s picture

This is because zoom_to_features is a behavior that requires some configuration (specifically, it needs to know *which* layer is should be zooming to), the controls section does not allow for configuration, so we cannot add it there. In 2.0 we will be adding a hook for behaviors to define forms, which we can then load into the presets UI.

For more information on programatically creating custom presets see http://zzolo.org/thoughts/drupal-and-openlayers under the "Better than UI" section

that0n3guy’s picture

Ahh I see... do you have an example of how to do what zzolo is talking about in #1?

Thanks

zzolo’s picture

@that0n3guy

Try looking here to see an example. It's near the bottom:
http://zzolo.org/thoughts/drupal-and-openlayers

BWPanda’s picture

Title: Cannot zoom map in display » Create map preset with zoom-to-features behaviour
Version: 6.x-1.0-alpha4 » 6.x-1.x-dev
Component: OpenLayers CCK » OpenLayers API

I'm trying to do what was suggested above and create a preset via the API that has a zoom-to-features behariour, but I'm not sure of the syntax to add the behaviour to the preset. I think when you have an API, you really need to list all the possible options for a given function... :)

Here's my code so far:

<?php
/**
 * Implementation of hook_openlayers_presets().
 *
 * Create a node preset that automatically zooms to features
 */
function brat_custom_openlayers_presets() {
  $presets = array();

  // Create map array
  $node_map = array(
    'width' => '250px',
    'height' => '175px',
    'center' => array(
      'lat' => '0',
      'lon' => '0',
      'zoom' => '2',
    ),
    'projection' => '900913',
    'default_layer' => 'openlayers_layers_google_street',
  );

  // Create full preset array
  $presets['node'] = array(
    'preset_name' => 'node',
    'preset_title' => t('Node'),
    'preset_description' => t('Maps to be displayed in nodes'),
    'preset_data' => $node_map,
  );

  return $presets;
}
?>
Boobaa’s picture

Subscribing

BWPanda’s picture

Still needing urgent help with this please...

phayes’s picture

something like this:

/**
* Implementation of hook_openlayers_presets().
*
* Create a node preset that automatically zooms to features
*/
function brat_custom_openlayers_presets() {
  $presets = array();

  // Create map array
  $node_map = array(
    'width' => '250px',
    'height' => '175px',
    'center' => array(
      'lat' => '0',
      'lon' => '0',
      'zoom' => '2',
    ),
    'projection' => '900913',
    'default_layer' => 'openlayers_layers_google_street',
    'behaviors' => array (
      'zoomy' => array (
        'id' => 'zoomy',
        'type' => 'openlayers_behaviors_zoom_to_layer',
        'layer' => 'THE_NAME_OF_CCK_LAYER_WHICH_YOU_CAN_FIND_USING_DEVEL_AND_OPENLAYERS_DEBUG_OPTIONS',
      ),
    ),
  );

  // Create full preset array
  $presets['node'] = array(
    'preset_name' => 'node',
    'preset_title' => t('Node'),
    'preset_description' => t('Maps to be displayed in nodes'),
    'preset_data' => $node_map,
  );

  return $presets;
}
phayes’s picture

Note: I updated the above code as I found some bugs.

Also, you may wish to use a hook to alter the map instead of writing out the preset. You could use hook_openlayers_map_preprocess_alter

BWPanda’s picture

I must be doing something wrong...

I've got the code as per #9 above and I changed the value of 'Layer' to 'Location' (the Name I found under Layers > openlayers_cck_vector). However, my map's not displaying.

When I change the display type of my map (under Display Fields in CCK) to a preset I made using the GUI, it works. But when I change it to the 'node' preset (as above), it doesn't work...

What am I doing wrong?

BWPanda’s picture

I just had another look at this, and realised from looking at other maps that the 'layer' should be 'openlayers_cck_vector', but after changing it, I still can't get it to work...

Here's my function:

<?php
function brat_custom_openlayers_presets() {
  $presets = array();

  // Create map array
  $node_map = array(
    'width' => '250px',
    'height' => '175px',
    'center' => array(
      'lat' => '0',
      'lon' => '0',
      'zoom' => '2',
    ),
    'projection' => '900913',
    'default_layer' => 'openlayers_layers_google_street',
    'behaviors' => array(
      'zoomy' => array(
        'id' => 'zoomy',
        'type' => 'openlayers_behaviors_zoom_to_layer',
        'layer' => 'openlayers_cck_vector',
      ),
    ),
  );

  // Create full preset array
  $presets['node'] = array(
    'preset_name' => 'node',
    'preset_title' => t('Node'),
    'preset_description' => t('Maps to be displayed in nodes'),
    'preset_data' => $node_map,
  );

  return $presets;
}
?>

I'm then just inserting the maps into the node via the .tpl.php file (from the $node object) and setting the display type of my location field to 'OpenLayers Map: Node', but still no luck.

jerdavis’s picture

The following implementation should work, I used this on a site today. Place a version of this command in a custom site module:

/**
 * Implementation of hook_openlayers_map_preprocess_alter();
 */    
function mymodule_openlayers_map_preprocess_alter(&$map = array()) {
  $map['behaviors'] = array (
    'center_map' => array (
      'id' => 'center_map',
      'type' => 'openlayers_behaviors_zoom_to_layer',
      'layer' => 'openlayers_cck_vector',
    ),    
  );    
}
phayes’s picture

Have you modified and saved the preset via the UI? This might be a good idea to do - it *should* still save your custom behaviour there. One thing to watch out for when using the google projection is you need to have the resolution and MaxExtent set just right. The UI will do this for you if it detects you are using a 900913 projection.

This is actually a reason why I prefer to use a hook_openlayers_map_preprocess_alter instead of a custom preset - although zzolo is right about the hook having a small performance hit as compared to the preset.

BWPanda’s picture

I can't do anything to my preset via the UI as it only says 'Export' or 'Clone'...

I like the sound of the preprocess_alter hook, I might give that a try instead.

phayes’s picture

I guess you would need to clone it...

The way I do things is I create my preset in the UI then modify it using the preprocess_alter hook. When it comes to rolling from dev to production you want speed, so then I export my preset and combine it with my preprocess_alter hook in code as a module preset. That way you don't need to hit the database to load the preset, and you don't need to invoke a hook_alter either. But during dev I use the UI and the alter all over the place. I don't know what other's do - I would be curious!

BWPanda’s picture

Status: Active » Fixed

Implementing hook_openlayers_map_preprocess_alter() worked a treat, thanks guys!

velosol’s picture

@BWPanda: Could you post your final code?

BWPanda’s picture

Here you go:

<?php
/**
 * Implementation of hook_openlayers_map_preprocess_alter().
 *
 * Add zoom to layer behaviour
 */
function brat_custom_openlayers_map_preprocess_alter(&$map = array()) {
  if ($map['preset_name'] == 'small') {
    $map['behaviors'] = array(
      'brat_zoom_to_layer' => array(
        'id' => 'brat_zoom_to_layer',
        'type' => 'openlayers_behaviors_zoom_to_layer',
        'layer' => 'openlayers_cck_vector',
      ),
    );
  }
}
?>

Things to note:
- My module is called 'brat_custom' so you'll need to change this to whatever your module's called
- I've setup a preset (via the UI) called 'small' which is the one I want to apply this behaviour to, therefore the above code is only applied to maps that use the 'small' preset
- I've called this behaviour implementation 'brat_zoom_to_layer', but from what I understand you can call it whatever you like...
- Depending on your setup, you may need to change the value of 'layer' (mine's set to 'openlayers_cck_vector')

Enjoy!

Status: Fixed » Closed (fixed)

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