Index: API.txt =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/gmap/Attic/API.txt,v retrieving revision 1.6 diff -u -p -r1.6 API.txt --- API.txt 5 Nov 2006 09:34:34 -0000 1.6 +++ API.txt 24 May 2007 21:13:40 -0000 @@ -196,5 +196,35 @@ $mymap=array('id' => 'mymap', gmap_draw_map($mymap); +GMAP VIEWS API + +The gmap_views module also provides a small API for providing additional +overlay data to the produced map. By defining: + +function hook_gmap_views_handle_field($phases, $data) + +You can tell gmap which column contains the geographic information it +needs to plot the nodes on the map. + +There are two phases to the hook, 'discovery' and 'process'. During the +discovery phase, your hook will be called once for each field in the +view. If your module can transform this field into latitude and +longitude coordinates, you should return a value. This value will be +store and returned to you during the 'process' phase. This can be use- +ful in caching information, such has how to process the data, etc. + +If your module cannot process this field, return NULL (the norm in most +cases). + +In the 'process' phase, the $data arugment will contain two keys: + +- 'module' => this will hold the name of the field, the module that is +being invoked (your module) and an 'extra' field, containing whatever +you returned during the 'discovery' phase. +- 'entry' => the views "entry" containing all the fields returned from +the database/ + +From this data you should return an array with keys "lat" and "lon". +Gmap_views will use this array to plot the node on the map. Index: gmap_views.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/gmap/Attic/gmap_views.module,v retrieving revision 1.1.2.9 diff -u -p -r1.1.2.9 gmap_views.module --- gmap_views.module 28 Apr 2007 15:58:24 -0000 1.1.2.9 +++ gmap_views.module 24 May 2007 21:13:40 -0000 @@ -40,22 +40,16 @@ function gmap_views_gmap($op) { /** * Validate a GMap View. - * GMap Views requires Location: Latitude and Location: Longitude - * to generate markers. + * GMap Views requires one of the following: + * - Location: Lat and Location: Lon + * - Two columns, one titled t('Latitude'), one titled t('Longitude') + * - A module that can transform a field into lat, long coordinates */ function gmap_views_validate($type, $view, $form) { - $lat_set = FALSE; - $lon_set = FALSE; - for ($n = 0 ; $n < $view['field']['count'] ; $n++) { - if ($view['field'][$n]['id'] == 'location.latitude') { - $lat_set = TRUE; - } - if ($view['field'][$n]['id'] == 'location.longitude') { - $lon_set = TRUE; - } - } - if (!($lat_set && $lon_set)) { - form_error($form["$type-info"][$type . '_type'], t('GMap View requires Location: Latitude and Location: Longitude!')); + $ids = _gmap_views_find_coords_ids($view); + if (!($ids['lat'] && $ids['lon']) && !(isset($ids['module']))) { + form_error($form["$type-info"][$type . '_type'], + t('GMap View requires: either "Location: Latitude" and "Location: Longitude" or a field titled "Latitude" and a field titled "Longitude"')); } return views_ui_plugin_validate_list($type, $view, $form); } @@ -64,9 +58,13 @@ function gmap_views_validate($type, $vie * Display the results of a view in a Google Map. */ function theme_views_view_gmap($view, $results) { + // 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' => 'view_gmap', @@ -75,10 +73,7 @@ function theme_views_view_gmap($view, $r if ($thismap['#settings']['behavior']['views_autocenter']) { // Find the first valid location. foreach ($results as $entry) { - $location = array( - 'lat' => $entry->location_latitude, - 'lon' => $entry->location_longitude, - ); + $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']; @@ -114,10 +109,8 @@ function theme_views_view_gmap($view, $r foreach ($results as $entry) { // @@@ $type = isset($entry->node_type) ? $entry->node_type : ''; - $location = array( - 'lat' => $entry->location_latitude, - 'lon' => $entry->location_longitude, - ); + $location = _gmap_views_get_lat_long_from_ids($entry, $point_ids); + if (($location['lat']) && ($location['lon'])) { if ($fatmarkers) { $data = array(); @@ -162,4 +155,52 @@ function theme_gmap_views_marker_label($ $marker_label .= '
'. views_theme_field('views_handle_field', $field['queryname'], $fields, $field, $entry, $view) .'
'; } return $marker_label; -} \ No newline at end of file +} + +/** + * Helper function to find a valid latitude and longitude in this field + */ +function _gmap_views_find_coords_ids($view) { + $ids = array(); + $copy = (array) $view; + foreach ($copy['field'] as $key => $field) { + if (!is_numeric($key)) { + continue; // skip the 'count', etc. + } + + // we check to see if the field is a location:lat field or titled Latitude + if ($field['id'] == 'location.latitude' || $field['label'] == t('Latitude')) { + $ids['lat'] = $field['queryname']; + } + + if ($field['id'] == 'location.longitude' || $field['label'] == t('Longitude')) { + $ids['lon'] = $field['queryname']; + } + + // see if any module will take on the task of adding lat-lon to the view + foreach(module_implements('gmap_views_handle_field') as $module) { + if($res = module_invoke($module, 'gmap_views_handle_field', 'discover', $field)) { + $ids['module'] = array( + 'module' => $module, + 'field' => $field['queryname'], + 'extra' => $res, + ); + } + } + } + + return $ids; +} + +/** + * Helper function to find actual lat and lon values from the work done in _gmap_views_find_coords_ids + */ +function _gmap_views_get_lat_long_from_ids($entry, $ids) { + // during the discovery phase, a module registered that it could turn this entry into a lat-lon array + if($ids['module']) { + $ids['entry'] = $entry; + return module_invoke($ids['module']['module'], 'gmap_views_handle_field', 'process', $ids); + } + // standard stuff, we can handle it + return array('lat' => $entry->{$ids['lat']}, 'lon' => $entry->{$ids['lon']}); +}