You are here

function _geofield_openlayers_formatter in Geofield 7.2

Same name and namespace in other branches
  1. 7 geofield.formatters.inc \_geofield_openlayers_formatter()
1 call to _geofield_openlayers_formatter()
geofield_field_formatter_view in ./geofield.formatters.inc
Implements hook_field_formatter_view().

File

./geofield.formatters.inc, line 386
Drupal field formatter hooks and helper functions.

Code

function _geofield_openlayers_formatter($map_name, $items) {
  $features = array();
  geophp_load();

  // Create array of $features
  foreach ($items as $delta) {
    if (array_key_exists('geom', $delta)) {
      $geometry = geoPHP::load($delta['geom']);
    }
    else {
      $geometry = geoPHP::load($delta);
    }
    $features[] = array(
      'wkt' => $geometry
        ->out('wkt'),
      'projection' => 'EPSG:4326',
    );
  }

  // Get map preset
  $preset = openlayers_preset_load($map_name);
  $map = openlayers_build_map($preset->data);
  if (!isset($map['layers']['geofield_formatter'])) {
    drupal_set_message(t('Trying to render a geofield formatter on a map without the placeholder layer'), 'error');
  }

  // Add the features to the placeholder layer
  $map['layers']['geofield_formatter']['features'] = $features;

  // Return themed map if no errors found
  if (empty($map['errors'])) {
    $js = array(
      'openlayers' => array(
        'maps' => array(
          $map['id'] => $map,
        ),
      ),
    );
    drupal_add_js($js, 'setting');

    // Push map through theme function and return
    $output = theme('openlayers_map', array(
      'map' => $map,
      'map_name' => $map_name,
    ));
  }
  return $output;
}