You are here

geolocation_googlemaps.module in Geolocation Field 6

Same filename and directory in other branches
  1. 7 modules/geolocation_googlemaps/geolocation_googlemaps.module

File

modules/geolocation_googlemaps/geolocation_googlemaps.module
View source
<?php

//==========================================//

// DEFINING A FORMATTER

//==========================================//

/**
* Implementation of hook_theme().
*/
function geolocation_googlemaps_theme() {
  return array(
    // Themes for the formatters.
    'geolocation_googlemaps_formatter_googlemaps_static' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}

/**
* Implementation of hook_field_formatter_info().
*
* All fields should have a 'default' formatter.
* Any number of other formatters can be defined as well.
* It's nice for there always to be a 'plain' option
* for the raw value, but that is not required.
*
*/
function geolocation_googlemaps_field_formatter_info() {
  return array(
    'googlemaps_static' => array(
      'label' => t('Static Google Map'),
      'field types' => array(
        'geolocation_latlng',
      ),
    ),
  );
}

/**
* Theme function for field formatter.
*
* $element['#item']: the sanitized $delta value for the item,
* $element['#field_name']: the field name,
* $element['#type_name']: the $node->type,
* $element['#formatter']: the $formatter_name,
* $element['#node']: the $node,
* $element['#delta']: the delta of this item, like '0',
*
*/
function theme_geolocation_googlemaps_formatter_googlemaps_static($element) {
  $item = $element['#item'];
  $settings = array(
    'map_dimensions' => '512x512',
    'map_zoomlevel' => '7',
    'map_imageformat' => 'png8',
    'map_maptype' => 'roadmap',
  );
  $variables = array(
    'path' => 'http://maps.google.com/maps/api/staticmap?zoom=' . $settings['map_zoomlevel'] . '&size=' . $settings['map_dimensions'] . '&format=' . $settings['map_imageformat'] . '&maptype=' . $settings['map_maptype'] . '&markers=size:mid|color:red|' . $item['lat'] . ',' . $item['lng'] . '&sensor=false',
    'alt' => 'Geolocation',
    'attributes' => array(
      'class' => 'geolocation-googlemaps-static',
    ),
  );
  $map_img = theme('image', $variables['path'], $variables['alt'], NULL, $variables['attributes'], FALSE);
  return '<div>' . $map_img . '</div>';
}

//==========================================//

// DEFINING A WIDGET

//==========================================//

/**
* Implementation of hook_widget_info().
*
* Here we indicate that the content module will handle
* the default value and multiple values for these widgets.
*
* Callbacks can be omitted if default handing is used.
* They're included here just so this module can be used
* as an geolocation for custom modules that might do things
* differently.
*/
function geolocation_googlemaps_widget_info() {
  return array(
    'geolocation_googlemap' => array(
      'label' => t('Google Map'),
      'field types' => array(
        'geolocation_latlng',
      ),
    ),
  );
}

/**
* Implementation of hook_widget_settings().
*/
function geolocation_googlemaps_widget_settings($op, $widget) {
  switch ($op) {

    // Create the form element to be used on the widget
    // settings form. Widget settings can be different
    // for each shared instance of the same field and
    // should define the way the value is displayed to
    // the user in the edit form for that content type.
    case 'form':
      $element = array();
      return $element;

    // Return an array of the names of the widget settings
    // defined by this module. These are the items that
    // CCK will store in the widget definition and they
    // will be available in the $field['widget'] array.
    // This should match the items defined in 'form' above.
    case 'save':
      return array(
        'map_dimensions',
        'map_zoomlevel',
        'map_imageformat',
        'map_maptype',
      );
  }
}

/**
* Implementation of hook_widget().
*
* Attach a single form element to the form.
*
* CCK core fields only add a stub element and builds
* the complete item in #process so reusable elements
* created by hook_elements can be plugged into any
* module that provides valid $field information.
*
* Custom widgets that don't care about using hook_elements
* can be built out completely at this time.
*
* If there are multiple values for this field and CCK is
* handling multiple values, the content module will call
* this function as many times as needed.
*
* @param $form
*   the entire form array,
*   $form['#node'] holds node information
* @param $form_state
*   the form_state,
*   $form_state['values'][$field['field_name']]
*   holds the field's form values.
* @param $field
*   the field array
* @param $items
*   array of default values for this field
* @param $delta
*   the order of this item in the array of
*   subelements (0, 1, 2, etc)
*
* @return
*   the form item for a single element for this field
*/
function geolocation_googlemaps_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $lat_value = isset($items[$delta]['lat']) ? $items[$delta]['lat'] : '';
  $lng_value = isset($items[$delta]['lng']) ? $items[$delta]['lng'] : '';
  $element['lat'] = array();
  $element['lng'] = array();
  $element['googlemap'] = array();
  $element['lat'] += array(
    '#type' => 'hidden',
    '#prefix' => '<div id="lat-' . $delta . '-field" class="address">',
    '#suffix' => '</div>',
    '#attributes' => array(
      'class' => 'geolocation-lat',
    ),
    '#default_value' => $lat_value,
    '#maxlength' => 30,
  );
  $element['lng'] += array(
    '#type' => 'hidden',
    '#prefix' => '<div id="lng-' . $delta . '-field" class="address">',
    '#suffix' => '</div>',
    '#attributes' => array(
      'class' => 'geolocation-lng',
    ),
    '#default_value' => $lng_value,
    '#maxlength' => 30,
  );
  $element['address'] = array(
    '#type' => 'textfield',
    '#title' => t('Geolocation'),
    '#prefix' => '<div id="address-' . $delta . '" class="address">',
    '#suffix' => '</div>',
    '#attributes' => array(
      'class' => 'geolocation-input address',
    ),
    '#maxlength' => 120,
    '#weight' => -5.1,
    '#description' => t('Enter an address / location in the textfield above. You can also right-click on the map to set a marker'),
  );
  $element['setaddress'] = array(
    '#prefix' => '<div id="map-' . $delta . '-setaddress">',
    '#suffix' => '</div>',
    '#value' => t('Set location'),
    '#weight' => -5,
  );

  // Copied from Google examples:
  // http://demos.projectx.co.nz/gmaps3/reverse_geocoder.html
  $element['googlemap'] = array(
    '#prefix' => '<div id="map-' . $delta . '" class="map" style="width:100%;height:400px;">',
    '#suffix' => '</div>',
    '#value' => ' ',
  );
  $element['remove'] = array(
    '#prefix' => '<div id="map-' . $delta . '-remove">',
    '#suffix' => '</div>',
    '#value' => t('Remove'),
  );

  // Attach CSS and JS files via FAPI '#attached'.
  drupal_add_css(drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.css');
  $element['googlemap']['#attached']['css'][] = drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.css';
  drupal_add_js(drupal_get_path('module', 'geolocation_googlemaps') . '/geolocation_googlemaps.js');

  // Make defaults available as javascript settings. In JS files use:
  // Drupal.settings.map_defaults.lat
  // TODO: better defaults.
  $map_defaults_lat = $lat_value == '' ? 49.9148115245017 : $lat_value;
  $map_defaults_lng = $lng_value == '' ? 10.8783125877381 : $lng_value;
  $map_defaults = array(
    $delta => array(
      'lat' => $map_defaults_lat,
      'lng' => $map_defaults_lng,
    ),
  );
  drupal_add_js(array(
    'map_defaults' => $map_defaults,
  ), 'setting');
  return $element;
}
function geolocation_googlemaps_footer($main = 0) {

  //  return '<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAA7-L8Jwu-zjowWSy41QPENxT2yXp_ZAY8_ufC3CFXhHIE1NvwkxTUWnxJFjlvG-FxdJglz_kY_obTJQ" />';
}

Functions

Namesort descending Description
geolocation_googlemaps_field_formatter_info Implementation of hook_field_formatter_info().
geolocation_googlemaps_footer
geolocation_googlemaps_theme Implementation of hook_theme().
geolocation_googlemaps_widget Implementation of hook_widget().
geolocation_googlemaps_widget_info Implementation of hook_widget_info().
geolocation_googlemaps_widget_settings Implementation of hook_widget_settings().
theme_geolocation_googlemaps_formatter_googlemaps_static Theme function for field formatter.