You are here

class geolocation_views_plugin_style_google_map in Geolocation Views 7

Hierarchy

Expanded class hierarchy of geolocation_views_plugin_style_google_map

1 string reference to 'geolocation_views_plugin_style_google_map'
geolocation_views_views_plugins in ./geolocation_views.views.inc
Implementation of hook_views_plugins().

File

./geolocation_views_plugin_style_google_map.inc, line 3

View source
class geolocation_views_plugin_style_google_map extends views_plugin_style {

  /**
   * Set default options
   */
  function option_definition() {
    $options = parent::option_definition();
    $options['map_width'] = array(
      'default' => '100%',
    );
    $options['map_height'] = array(
      'default' => '400px',
    );
    $options['map_center'] = array(
      'default' => '0,0',
    );
    $options['map_zoom'] = array(
      'default' => '4',
    );
    $options['map_min_zoom'] = array(
      'default' => 1,
    );
    $options['map_max_zoom'] = array(
      'default' => 19,
    );
    $options['map_type'] = array(
      'default' => 'ROADMAP',
    );
    $options['marker_icon_field'] = array(
      'default' => '',
    );
    $options['marker_url_field'] = array(
      'default' => '',
    );
    $options['marker_title_field'] = array(
      'default' => '',
    );
    $options['use_marker_clusterer'] = array(
      'default' => FALSE,
    );
    $options['marker_clusterer']['grid_size'] = array(
      'default' => 60,
    );
    $options['marker_clusterer']['max_zoom'] = array(
      'default' => '',
    );
    $options['marker_clusterer']['icon_url'] = array(
      'default' => '',
    );
    $options['marker_clusterer']['icon_size'] = array(
      'default' => '',
    );
    $options['map_auto_center_and_zoom'] = array(
      'default' => TRUE,
    );
    $options['map_disable_scroll_wheel'] = array(
      'default' => FALSE,
    );
    $options['map_disable_double_click_zoom'] = array(
      'default' => FALSE,
    );
    return $options;
  }

  /**
   * Options form.
   */
  function options_form(&$form, &$form_state) {
    parent::options_form($form, $form_state);
    $zoom_options = drupal_map_assoc(array(
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      11,
      12,
      13,
      14,
      15,
      16,
      17,
      18,
      19,
    ));
    $field_options = array(
      '' => t('< none >'),
    );
    $fields = $this->display->handler
      ->get_handlers('field');
    foreach ($fields as $id => $handler) {
      $field_options[$id] = $handler
        ->ui_name(FALSE);
    }
    $form['map_width'] = array(
      '#title' => t('Map width'),
      '#type' => 'textfield',
      '#default_value' => $this->options['map_width'],
      '#size' => 5,
    );
    $form['map_height'] = array(
      '#title' => t('Map height'),
      '#type' => 'textfield',
      '#default_value' => $this->options['map_height'],
      '#size' => 5,
    );
    $form['map_center'] = array(
      '#title' => t('Map center'),
      '#type' => 'textfield',
      '#default_value' => $this->options['map_center'],
      '#size' => 40,
    );
    $form['map_zoom'] = array(
      '#title' => t('Map zoom'),
      '#type' => 'select',
      '#options' => $zoom_options,
      '#default_value' => $this->options['map_zoom'],
    );
    $form['map_min_zoom'] = array(
      '#title' => t('Minimum map zoom'),
      '#type' => 'select',
      '#options' => $zoom_options,
      '#default_value' => $this->options['map_min_zoom'],
    );
    $form['map_max_zoom'] = array(
      '#title' => t('Maximum map zoom'),
      '#type' => 'select',
      '#options' => $zoom_options,
      '#default_value' => $this->options['map_max_zoom'],
    );
    $form['map_type'] = array(
      '#title' => t('Map type'),
      '#type' => 'select',
      '#options' => drupal_map_assoc(array(
        'ROADMAP',
        'SATELLITE',
        'HYBRID',
        'TERRAIN',
      )),
      '#default_value' => $this->options['map_type'],
    );
    $form['marker_icon_field'] = array(
      '#title' => t('Marker icon field'),
      '#type' => 'select',
      '#options' => $field_options,
      '#default_value' => $this->options['marker_icon_field'],
    );
    $form['marker_url_field'] = array(
      '#title' => t('Marker url field'),
      '#type' => 'select',
      '#options' => $field_options,
      '#default_value' => $this->options['marker_url_field'],
    );
    $form['marker_title_field'] = array(
      '#title' => t('Marker title field'),
      '#type' => 'select',
      '#options' => $field_options,
      '#default_value' => $this->options['marker_title_field'],
    );
    $form['use_marker_clusterer'] = array(
      '#title' => t('Use MarkerClusterer'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['use_marker_clusterer'],
    );
    $form['marker_clusterer'] = array(
      '#title' => t('MarkerClusterer options'),
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#states' => array(
        'visible' => array(
          '#edit-style-options-use-marker-clusterer' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['marker_clusterer']['grid_size'] = array(
      '#title' => t('Grid size'),
      '#description' => t('The grid size of a cluster in pixel. Each cluster will be a square. If you want the algorithm to run faster, you can set this value larger. The default value is 60.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['marker_clusterer']['grid_size'],
      '#size' => 5,
    );
    $form['marker_clusterer']['max_zoom'] = array(
      '#title' => t('Max zoom'),
      '#description' => t('The max zoom level monitored by a marker cluster. If not given, the marker cluster assumes the maximum map zoom level. When maxZoom is reached or exceeded all markers will be shown without cluster.'),
      '#type' => 'select',
      '#options' => array(
        '' => t('- none -'),
      ) + $zoom_options,
      '#default_value' => $this->options['marker_clusterer']['max_zoom'],
    );
    $form['marker_clusterer']['icon_url'] = array(
      '#title' => t('Custom icon url'),
      '#description' => t('The image url.'),
      '#type' => 'textfield',
      '#default_value' => $this->options['marker_clusterer']['icon_url'],
    );
    $form['marker_clusterer']['icon_size'] = array(
      '#title' => t('Custom icon size'),
      '#description' => t('The image width and height. Example: 32x32'),
      '#type' => 'textfield',
      '#default_value' => $this->options['marker_clusterer']['icon_size'],
      '#size' => 5,
      '#states' => array(
        'visible' => array(
          '#edit-style-options-use-marker-clusterer' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    $form['map_auto_center_and_zoom'] = array(
      '#title' => t('Automatically center and zoom map'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['map_auto_center_and_zoom'],
    );
    $form['map_disable_scroll_wheel'] = array(
      '#title' => t('Disable scroll wheel'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['map_disable_scroll_wheel'],
    );
    $form['map_disable_double_click_zoom'] = array(
      '#title' => t('Disable double click zoom'),
      '#type' => 'checkbox',
      '#default_value' => $this->options['map_disable_double_click_zoom'],
    );
  }

  /**
   * Render the display in this style.
   */
  function render() {
    if (isset($this->view->live_preview) && $this->view->live_preview) {
      return t('Selected style are not compatible with live preview.');
    }
    $geolocation_field = '';
    foreach ($this->view->field as $field_name => $field_handler) {
      if (isset($field_handler->field_info) && $field_handler->field_info['type'] == 'geolocation_latlng') {
        $geolocation_field = $field_name;
        break;
      }
    }
    if (!$geolocation_field) {
      return t('Add Geolocation field to fields list.');
    }
    $this->view->field[$geolocation_field]->options['exclude'] = TRUE;
    $this
      ->render_fields($this->view->result);
    $markers = array();
    foreach ($this->view->result as $row_index => $row) {
      if (!$row->{'field_' . $geolocation_field}) {
        continue;
      }
      $marker_icon = '';
      if ($this->options['marker_icon_field']) {
        $marker_icon = $this
          ->get_field($row_index, $this->options['marker_icon_field']);
      }
      $marker_url = '';
      if ($this->options['marker_url_field']) {
        $marker_url = $this
          ->get_field($row_index, $this->options['marker_url_field']);
      }
      $marker_title = '';
      if ($this->options['marker_title_field']) {
        $marker_title = $this
          ->get_field($row_index, $this->options['marker_title_field']);
        $marker_title = strip_tags($marker_title);
        $marker_title = decode_entities($marker_title);
      }
      $this->view->row_index = $row_index;
      $marker_content = $this->row_plugin
        ->render($row);
      foreach ($row->{'field_' . $geolocation_field} as $field_value) {
        $markers[] = array(
          'lat' => (double) $field_value['raw']['lat'],
          'lng' => (double) $field_value['raw']['lng'],
          'icon' => $marker_icon,
          'url' => $marker_url,
          'content' => $marker_content,
          'title' => $marker_title,
        );
      }
    }
    unset($this->view->row_index);
    $map_id = drupal_html_id('geolocation-views-' . $this->view->name . '-' . $this->view->current_display);
    drupal_alter('geolocation_views_markers', $markers, $this->view);
    drupal_add_js(array(
      'geolocationViewsMarkers' => array(
        $map_id => $markers,
      ),
    ), 'setting');
    $query = array();
    if ($googlemaps_api_key = variable_get('geolocation_googlemaps_api_key')) {
      $query['key'] = $googlemaps_api_key;
    }
    drupal_add_js('//maps.google.com/maps/api/js?' . drupal_http_build_query($query), array(
      'type' => 'external',
    ));
    $module_path = drupal_get_path('module', 'geolocation_views');
    if ($this->options['use_marker_clusterer']) {
      drupal_add_js(array(
        'geolocationViews' => array(
          'modulePath' => $module_path,
        ),
      ), array(
        'type' => 'setting',
      ));
      drupal_add_js($module_path . '/markerclusterer/markerclusterer.js');
    }
    drupal_add_js($module_path . '/geolocation_views.js');
    return theme($this
      ->theme_functions(), array(
      'view' => $this->view,
      'options' => $this->options,
      'attributes_array' => array(
        'id' => $map_id,
        'class' => array(
          'geolocation-views-map',
        ),
        'style' => 'width:' . $this->options['map_width'] . '; height:' . $this->options['map_height'],
        'data-map-center' => $this->options['map_center'],
        'data-map-zoom' => $this->options['map_zoom'],
        'data-map-max-zoom' => $this->options['map_max_zoom'],
        'data-map-min-zoom' => $this->options['map_min_zoom'],
        'data-map-type' => $this->options['map_type'],
        'data-use-marker-clusterer' => $this->options['use_marker_clusterer'],
        'data-marker-clusterer-grid-size' => $this->options['marker_clusterer']['grid_size'],
        'data-marker-clusterer-max-zoom' => $this->options['marker_clusterer']['max_zoom'],
        'data-marker-clusterer-icon-url' => $this->options['marker_clusterer']['icon_url'],
        'data-marker-clusterer-icon-size' => $this->options['marker_clusterer']['icon_size'],
        'data-auto-center' => $this->options['map_auto_center_and_zoom'],
        'data-scroll-wheel' => (int) (!$this->options['map_disable_scroll_wheel']),
        'data-disable-double-click-zoom' => (int) $this->options['map_disable_double_click_zoom'],
      ),
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
geolocation_views_plugin_style_google_map::options_form function Options form. Overrides views_plugin_style::options_form
geolocation_views_plugin_style_google_map::option_definition function Set default options Overrides views_plugin_style::option_definition
geolocation_views_plugin_style_google_map::render function Render the display in this style. Overrides views_plugin_style::render
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::options_submit public function Handle any special handling on the validate form. 9
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin_style::$row_plugin public property The row plugin, if it's initialized and the style itself supports it.
views_plugin_style::$row_tokens public property Store all available tokens row rows.
views_plugin_style::build_sort public function Called by the view builder to see if this style handler wants to interfere with the sorts. If so it should build; if it returns any non-TRUE value, normal sorting will NOT be added to the query. 1
views_plugin_style::build_sort_post public function Called by the view builder to let the style build a second set of sorts that will come after any other sorts in the view. 1
views_plugin_style::destroy public function Destructor. Overrides views_object::destroy
views_plugin_style::even_empty public function Should the output of the style plugin be rendered even if it's empty. 1
views_plugin_style::get_field public function Get a rendered field.
views_plugin_style::get_field_value public function Get the raw field value.
views_plugin_style::get_row_class public function Return the token replaced row class for the specified row.
views_plugin_style::init public function Initialize a style plugin.
views_plugin_style::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_style::pre_render public function Allow the style to do stuff before each row is rendered.
views_plugin_style::query public function Add anything to the query that we might need to. Overrides views_plugin::query 2
views_plugin_style::render_fields public function Render all of the fields for a given style and store them on the object.
views_plugin_style::render_grouping public function Group records as needed for rendering.
views_plugin_style::render_grouping_sets public function Render the grouping sets.
views_plugin_style::tokenize_value public function Take a value and apply token replacement logic to it.
views_plugin_style::uses_fields public function Return TRUE if this style also uses fields.
views_plugin_style::uses_row_class public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_row_plugin public function Return TRUE if this style also uses a row plugin.
views_plugin_style::uses_tokens public function Return TRUE if this style uses tokens.
views_plugin_style::validate public function Validate that the plugin is correct and can be saved. Overrides views_plugin::validate