Mapping With Drupal - Alan Palazzolo [41]
/**
* Geolocation behavior.
*/
class mappingdrupal_ol_extensions_geolocate extends openlayers_behavior {
// ...
}
We define a new class. The class name mappingdrupal_ol_extensions_geolocate was defined in the first step of the example in Define Behavior, and it extends openlayers_behavior, which is a base class defined in the OpenLayers module. The mappingdrupal_ol_extensions_geolocate class contains several methods; the first of these methods is options_init():
/**
* Provide initial values for options.
*/
function options_init() {
return array(
'on_load' => FALSE,
'button' => TRUE,
'zoom' => 11,
);
}
The options_init() method returns an array of default values that we have now chosen for our configuration options:
/**
* Provide form for configurations per map.
*/
function options_form($defaults) {
return array(
'on_load' => array(
'#title' => t('Geolocate on load'),
'#type' => 'checkbox',
'#description' => t('When checked, the map will geolocate the user
and zoom in when the map first loads.'),
'#default_value' => isset($defaults['on_load']) ?
$defaults['on_load'] : FALSE
),
'button' => array(
'#title' => t('Geolocate button'),
'#type' => 'checkbox',
'#description' => t('When checked, provides a button on the map that
will geolocate the user.'),
'#default_value' => isset($defaults['button']) ?
$defaults['button'] : TRUE
),
'zoom' => array(
'#title' => t('Zoom level'),
'#type' => 'textfield',
'#description' => t('Determine the zoom level when geolocating. Higher
is more zoomed in.'),
'#default_value' => isset($defaults['zoom']) ?
$defaults['zoom'] : 11
),
);
}
The options_form() method takes in default values from the map object, and returns a Drupal form array for the configuration options of our Behavior. If you are unsure of how to create this array, please see the reference on the Drupal Form API. When adding or editing maps, you can see these form items if you go to the OpenLayers Map interface at admin/structure/openlayers/maps, and go to the Behaviors section. The form is shown in Figure 5-6:
/**
* Render.
*/
function render(&$map) {
drupal_add_css(drupal_get_path('module', 'mappingdrupal_ol_extensions') .
'/behaviors/mappingdrupal_ol_extensions_geolocate.css');
drupal_add_js(drupal_get_path('module', 'mappingdrupal_ol_extensions') .
'/behaviors/mappingdrupal_ol_extensions_geolocate.js');
return $this->options;
}
Figure 5-6. OpenLayers geolocation configuration
Finally the render() method is called when the map is rendered for display. The map object is passed to the render() method in case there is any last-minute configuration to add to the map. In our example, we are adding a JavaScript file to add interaction to the map and a CSS file to make things look nicer.
JavaScript interactions
In the behaviors subfolder, create a new file named mappingdrupal_ol_extensions_geolocate.js. As with the GMap module, we will be using Drupal Behaviors to interact with elements on the page, including the OpenLayers map. First, set up the basic Drupal Behavior and geolocation method:
/**
* @file
* JS Implementation of OpenLayers behavior for Geolocation.
*/
// Namespace $ to jQuery
(function($) {
/**
* Geolocation Behavior
*/
Drupal.behaviors.mappingdrupal_ol_extensions_geolocate = {
'attach': function(context, settings) {
// ...
},
// Method to geolocate user.
'geolocate': function(map, zoom) {
// First ensure that the HTML5 geolocation controls
// are available. We might use some more helpful
// libraries for this, like Modernizr
//
// We have to make sure we are explicit of the projection
// as latitude and longitude are different from
// spherical mercator (or other possiblilities).
if (typeof navigator != 'undefined' &&
typeof navigator.geolocation != 'undefined') {
navigator.geolocation.getCurrentPosition(function (position) {
var center = new OpenLayers.LonLat(
position.coords.longitude,
position.coords.latitude
).transform(