You are here

function theme_views_view_gmap in GMap Module 5

Display the results of a view in a Google Map.

File

./gmap_views.module, line 62
GMap Views: A Views Style plugin providing a GMap view.

Code

function theme_views_view_gmap($view, $results) {

  // Work when multiple views are displayed at once.
  $mapid = "view_gmap_{$view->name}_{$view->build_type}";

  // Fields are used to render the markers.
  $fields = _views_get_fields();

  // find the ids of the column we want to use
  $point_ids = _gmap_views_find_coords_ids($view);
  if (isset($view->gmap_macro) && $view->gmap_macro) {
    $thismap = array(
      '#map' => $mapid,
      '#settings' => array_merge(gmap_defaults(), gmap_parse_macro($view->gmap_macro)),
    );
    if ($thismap['#settings']['behavior']['views_autocenter']) {

      // Find the first valid location.
      foreach ($results as $entry) {
        $location = _gmap_views_get_lat_long_from_ids($entry, $point_ids);
        if ($location['lat'] && $location['lon']) {

          // Set default location for map
          $thismap['#settings']['latitude'] = $location['lat'];
          $thismap['#settings']['longitude'] = $location['lon'];

          // Break loop because we have what we want.
          break;
        }
      }
    }
  }
  else {
    if (!empty($view->gmap_map)) {
      $thismap = $view->gmap_map;
    }
    else {
      $thismap = array(
        '#map' => $mapid,
        '#settings' => gmap_defaults(),
      );
    }
  }
  $fatmarkers = isset($thismap['#settings']['behavior']['fatmarkers']) && $thismap['#settings']['behavior']['fatmarkers'];
  $markers = array();
  if ($fatmarkers) {
    $thismap['#settings']['viewfields'] = $view->field;
    $datafields = array();
    foreach ($view->field as $field) {
      if ($fields[$field['id']]['visible'] !== FALSE) {
        $datafields[] = $field['queryname'];
      }
    }
  }
  $markermode = $thismap['#settings']['markermode'];
  $markertypes = variable_get('gmap_node_markers', array());
  foreach ($results as $entry) {
    $type = $entry->gmap_node_type;
    $location = _gmap_views_get_lat_long_from_ids($entry, $point_ids);
    if ($location['lat'] && $location['lon']) {
      if ($fatmarkers) {
        $data = array();
        foreach ($view->field as $field) {
          if ($fields[$field['id']]['visible'] !== FALSE) {
            $data[] = views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $entry, $view);
          }
        }
        $themarker = array(
          'markername' => isset($markertypes[$type]) ? $markertypes[$type] : 'drupal',
          'latitude' => $location['lat'],
          'longitude' => $location['lon'],
          'view' => array_values($data),
        );
        if (isset($entry->gmap_taxonomy_marker) && !empty($entry->gmap_taxonomy_marker)) {
          $themarker['markername'] = $entry->gmap_taxonomy_marker;
        }
      }
      else {

        // Common
        $themarker = array(
          'markername' => isset($markertypes[$type]) ? $markertypes[$type] : 'drupal',
          'latitude' => $location['lat'],
          'longitude' => $location['lon'],
        );
        if (isset($entry->gmap_taxonomy_marker) && !empty($entry->gmap_taxonomy_marker)) {
          $themarker['markername'] = $entry->gmap_taxonomy_marker;
        }

        // Popup
        if ($markermode == 1) {

          // @@@ TODO: Switch to using views_theme sometime. Unfortunately, it changes the function prototype.. :-/

          //$marker_popup = views_theme('gmap_views_marker_label', $view->name, $view, $fields, $entry);
          $marker_popup = theme('gmap_views_marker_label', $view, $fields, $entry);

          // add themed HTML to either text or tabs depending on whether or not
          // it was an array.
          if (is_array($marker_popup)) {

            // Tabbed.
            $themarker['tabs'] = $marker_popup;
          }
          else {
            $themarker['text'] = $marker_popup;
          }
        }
        else {
          if ($markermode == 2) {
            $themarker['link'] = url('node/' . $entry->nid);
          }
        }
      }
      if (isset($entry->node_title)) {
        $themarker['opts']['title'] = $entry->node_title;
      }
      $markers[] = $themarker;
    }
  }
  $thismap['#settings']['markers'] = $markers;
  $output .= theme('gmap', $thismap);
  return $output;
}