Mapping With Drupal - Alan Palazzolo [39]
The other part of the CTools module that is used by OpenLayers is exportables. Layers, Styles, and Maps are all CTools exportables. A CTools exportable is a data record that can be stored in the database or exported in code. Exportables are covered in Chapter 7.
NOTE
The CTools module provides most of its documentation within the module itself, and this documentation will be available on your site if you enable the Advanced Help module.
One straightforward example of an OpenLayers map configuration object is just the default map taken from the module itself. The map configuration object is an example of a CTools exportable. To see this exportable code, go to the OpenLayers maps page on your site at admin/structure/openlayers/maps and click the Export link next to one of the maps that you created in an earlier tutorial. Here is the code that you would export:
$default = new stdClass();
$default->api_version = 1;
$default->name = 'default';
$default->title = t('Default Map');
$default->description = t('This is the default map that comes
with the OpenLayers module.');
$default->data = array(
'projection' => '900913',
'width' => 'auto',
'height' => '400px',
'default_layer' => 'osm_mapnik',
'center' => array(
'initial' => array(
'centerpoint' => '0,0',
'zoom' => '2'
)
),
'displayProjection' => '4326',
'maxExtent' => openlayers_get_extent('4326'),
'behaviors' => array(
'openlayers_behavior_panzoombar' => array(),
'openlayers_behavior_layerswitcher' => array(),
'openlayers_behavior_attribution' => array(),
'openlayers_behavior_keyboarddefaults' => array(),
'openlayers_behavior_navigation' => array(),
),
'layers' => array(
'osm_mapnik' => 'osm_mapnik',
)
);
This map is shown in Figure 5-4.
Figure 5-4. OpenLayers default map
Geolocation Example
We are going to build a Geolocation extension to the OpenLayers module, much like we did with the GMap example in Geolocation Example. This extension will do two things: first, it will geolocate the user and center the map on that position; second, it will provide a mechanism for the user to update the map with their position. Both of these will be configurable by administrators of the site.
OpenLayers already has a geolocation control that comes with the library itself, but only for the 2.11 version of the OpenLayers JavaScript library. The geolocation control that we will create will not depend on a specific version of the OpenLayers library. All the code for this example can be found in the custom module mappingdrupal_ol_extensions. The steps we will go through are the following:
Tell OpenLayers about our new Behavior.
Define the new Behavior plug-in object.
Geolocate the user with JavaScript.
Use CSS to clean up the appearance.
This will produce the map shown in Figure 5-5, although the map will show your own current location, rather than that of the author!
Figure 5-5. OpenLayers geolocation example
Define Behavior
OpenLayers Behaviors are defined and explained in Exploring OpenLayers Behaviors. Most of the Behaviors that come with the module are implementations of the OpenLayers library Controls. The first step of our example is to tell OpenLayers about our new Behavior plug-in. This is done with hook_openlayers_behaviors(). The following is the complete .module file (we are assuming you have already made your .info file):
/**
* @file
* Main module file