You are here

function geolocation_views_plugin_style_google_map::render in Geolocation Views 7

Render the display in this style.

Overrides views_plugin_style::render

File

./geolocation_views_plugin_style_google_map.inc, line 192

Class

geolocation_views_plugin_style_google_map

Code

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'],
    ),
  ));
}