You are here

function theme_gmap_geo_formatter in GMap Addons 7

Same name and namespace in other branches
  1. 6 gmap_geo/gmap_geo.module \theme_gmap_geo_formatter()

Themes a geo field as a gmap.

Wasn't sure what to use as the base map settings. GMap default settings? GMap picker macro settings? Bdragon and I (Bec) have talked about a "gmap profile" setup, in which case we could provide a field formatter for each gmap profile ("GMap: profile_name"). This would be ideal.

  • Right now this map's settings are based on the gmap_picker widget settings,

if present.

  • Should this be checking to make sure that the WKT is well-formed?

Possibly with geo_wkt_validate()?

  • This field formatter is not useful with geodata from most shapefiles,

because these shapes often have more points than JavaScript-based can handle (generally 1,000 points/vertices is a reasonable maximum).

  • Autozoom doesn't yet zoom for polygons and lines; this field formatter

doesn't provide any autozoom stuff either.

  • Polygons don't get drawn at all by gmap unless at least one of their

vertices is within the visible area. If not all of a polygon's vertices are within the visible area, they may be rendered incorrectly.

1 string reference to 'theme_gmap_geo_formatter'
gmap_geo_theme in gmap_geo/gmap_geo.module
Implementation of hook_theme().

File

gmap_geo/gmap_geo.module, line 181
Provides a Google maps location picker widget for Geo cck fields. Provides a field formatter for Geo fields that displays point, line, or polygon data on a Google map.

Code

function theme_gmap_geo_formatter($element) {
  $i = 0;
  $total_points = 0;
  while (isset($element[$i])) {
    $item = $element[$i]['#item'];
    $i++;
    if ($total_points > 1000) {
      $i = -1;
      drupal_set_message(t('Some data is not displayed on the map because there are too many items.'), 'warning');
      break;
    }

    // get the feature's coordinates
    $geodata = _gmap_geo_from_wkt($item['wkt']);
    switch ($geodata['type']) {
      case 'POINT':
        foreach ($geodata['data'] as $point) {
          $coords = explode(' ', $point);
          $map['markers'][] = array(
            'longitude' => $coords[0],
            'latitude' => $coords[1],
          );
          $total_points++;
        }
        break;
      case 'POLYGON':
      case 'LINESTRING':
        if (isset($item['centroid'])) {
          $centroid_raw = _gmap_geo_from_wkt($item['centroid']);
          $centroid = explode(' ', reset($centroid_raw['data']));

          // autozoom for polygons is coming in GMap 1.1 (2008-12-30)
          $map['latitude'] = $centroid[0];
          $map['longitude'] = $centroid[1];
        }

        // get feature coordinates
        $shape_coords = array();
        foreach ($geodata['data'] as $point) {
          $shape_coords[] = explode(' ', $point);
        }

        // polygon simplification is resource intensive.
        // I am not sure how well this handles line data -- quite possibly it
        // would be fine, if you provide a centroid.
        if ($element['#formatter'] == 'gmap_geo_simplify' && isset($centroid)) {
          module_load_include('inc', 'gmap_geo', 'simplify');
          $shape_coords = gmap_geo_simplify($shape_coords, $centroid);
        }

        // if the poly/line is too complex, let's not crash the browser...
        if (count($shape_coords) < 200) {
          $map['shapes'][] = array(
            'type' => $geodata['type'] == 'POLYGON' ? 'polygon' : 'line',
            'points' => $shape_coords,
          );
          $total_points += count($shape_coords);
        }
        else {
          $map['markers'][] = array(
            'longitude' => $centroid[0],
            'latitude' => $centroid[1],
            'text' => t('This shape has too many points to display on a Google Map'),
          );
          $total_points++;
        }
        break;
    }
  }
  if ($map) {

    // Load field instance info. If the field uses the gmap_picker widget, use
    // the widget's gmap macro to build the map array.
    module_load_include('inc', 'content', 'includes/content.crud');
    $field = content_field_instance_read(array(
      'type_name' => $element['#type_name'],
      'field_name' => $element['#field_name'],
    ));
    if ($field[0]['widget']['type'] == 'gmap_picker') {
      $field_map = gmap_parse_macro($field[0]['widget']['gmap_picker_macro']);
      $map = array_merge($field_map, $map);
    }
    $map['behavior'] = array(
      'autozoom',
    );
    if (isset($field_map['zoom'])) {
      $map['maxzoom'] = $field_map['zoom'];
    }
    return theme('gmap', $map);
  }
}