You are here

function geofield_map_plugin_style_map::render in Geofield 7

Same name and namespace in other branches
  1. 7.2 modules/geofield_map/includes/geofield_map_plugin_style_map.inc \geofield_map_plugin_style_map::render()

Renders views (map)

Overrides views_plugin_style::render

File

modules/geofield_map/includes/geofield_map_plugin_style_map.inc, line 101
This file holds style plugin for Geofield Maps

Class

geofield_map_plugin_style_map
@class Extension of the Views Plugin Syle for Geofield Map

Code

function render() {
  geophp_load();
  $style_options = $this->view->style_plugin->options;
  $geo_data = !empty($style_options['data_source']) ? 'field_' . $style_options['data_source'] : NULL;
  $popup_data = !empty($style_options['popup_source']) ? $style_options['popup_source'] : NULL;
  if ($geo_data) {
    $this
      ->render_fields($this->view->result);
    $data = array();
    foreach ($this->view->result as $id => $result) {
      $geofield = !empty($result->{$geo_data}) ? $result->{$geo_data} : NULL;
      if (!empty($geofield)) {
        $description = $popup_data ? $this->rendered_fields[$id][$popup_data] : '';
        $geometry = geoPHP::load($geofield[0]['raw']['wkt'], 'wkt');
        $datum = json_decode($geometry
          ->out('json'));
        $datum->properties = array(
          'description' => $description,
        );
        $data[] = $datum;
      }
    }
    if (count($data) == 1) {
      $data = $data[0];
    }
    elseif (count($data) > 1) {
      $tmp = $data;
      $data = array(
        'type' => 'GeometryCollection',
        'geometries' => $tmp,
      );
    }
    $map_settings = geofield_map_settings_do($style_options);
    $map_id = drupal_html_id('geofield_map_' . $this->view->name . '_' . $this->view->current_display);
    $js_settings = array(
      $map_id => array(
        'map_id' => $map_id,
        'map_settings' => $map_settings,
        'data' => $data,
      ),
    );
    drupal_add_js(array(
      'geofieldMap' => $js_settings,
    ), 'setting');
  }
  drupal_add_js('//maps.googleapis.com/maps/api/js?sensor=false', 'external');
  drupal_add_js(drupal_get_path('module', 'geofield_map') . '/js/GeoJSON.js');
  drupal_add_js(drupal_get_path('module', 'geofield_map') . '/js/geofield_map.js');
  drupal_add_css(drupal_get_path('module', 'geofield_map') . '/css/geofield_map.css');

  // defaults
  $width = '100%';
  $height = '300px';
  if ($style_options['geofield_map_width']) {
    $width = $style_options['geofield_map_width'];
  }
  if ($style_options['geofield_map_height']) {
    $height = $style_options['geofield_map_height'];
  }
  return '<div style="width: ' . $width . '; height: ' . $height . '" id="' . $map_id . '" class="geofieldMap">' . $style_options['alt_text'] . '</div>';
}