/[drupal]/contributions/modules/geolocation/geolocation.module
ViewVC logotype

Contents of /contributions/modules/geolocation/geolocation.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Wed May 21 06:54:55 2008 UTC (18 months, 1 week ago) by dopry
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +0 -9 lines
File MIME type: text/x-php
add google maps intgration for geo coding and generating output.
1 <?php //$Id
2
3 /**
4 * @file
5 *
6 * a geolocation field.
7 *
8 * @todo: views integration - proximity filter, expose latitude and longitude independently to views.
9 * @todo: add mysql spatial extension support and distance mapping.
10 *
11 * High Performance Proximity Detection Methodology?
12
13 * 1) limit result set using mysql spatial extensions for rough bounding boxes.
14 * and limit the result set that we use to compute greater circle...
15 * ex) WHERE MBRwithin()
16 * 2) Store long in radians
17 * 3) precompute sin/cos for lats.
18 *
19 * table {
20 longitude varchar
21 latitude varchar
22 longrads float
23 latsin float
24 latcos float
25 latlong_pt POINT(longitude, latitude)
26 }
27
28 given $max_distance FROM (ptlat, ptlong)
29 $radius = 0.5 * $max_distance;
30 $poly = ($ptlong + $radius) .' '. ($ptlat + $radius).','. ($ptlong + $radius) .' '. ($ptlat - $radius).','.
31 ($ptlong - $radius) .' '. ($ptlat - $radius).','. ($ptlong - $radius).' '. ($ptlat + $radius).','.
32 ($ptlong + $radius).' '. ($ptlat + $radius)
33
34 $ptlatsin = sin(deg2rad($ptlat));
35 $ptlatcos = cos(deg2rad($ptlat));
36 $ptlongrads = deg2rad($ptlong);
37 SELECT nid, delta, acos( ($ptlatsin * latsin) + (ptlatcos*latcos * cos(longrads - ptlongrads) )) as distance
38 from geolocation WHERE MRBContains(POLYGON($poly), latlong_pt) AND distance < $max_distance.
39 *
40 */
41
42 /**
43 * @defgoup geolocation_core_hooks
44 * @{
45 *
46 * Core Drupal hooks for geolocation.
47 */
48
49 function geolocation_help($section) {
50 }
51
52 function geolocation_perms() {
53 }
54
55 /**
56 * @} End "defgoup geolocation_core_hooks"
57 */
58
59
60 /**
61 * @defgoup geolocation_field_hooks
62 * @{
63 *
64 * CCK field hooks for geolocation.
65 */
66
67 function geolocation_field_info() {
68 return array(
69 'geolocation' => array('label' => 'Geolocation'),
70 );
71 }
72
73 function geolocation_field_settings($op, $field) {
74 switch($op) {
75 case 'database columns':
76 return array(
77 'longitude' => array(
78 'type' => 'float',
79 'not null' => TRUE,
80 'default' => 0,
81 ),
82 'latitude' => array(
83 'type' => 'float',
84 'not null' => TRUE,
85 'default' => 0,
86 ),
87 'longrads' => array(
88 'type' => 'float',
89 'not null' => TRUE,
90 'default' => 0,
91 ),
92 'latsin' => array(
93 'type' => 'float',
94 'not null' => TRUE,
95 'default' => 0,
96 ),
97 'latcos' => array(
98 'type' => 'float',
99 'not null' => TRUE,
100 'default' => 0,
101 ),
102 'source' => array(
103 'type' => 'text',
104 'default' => '',
105 ),
106 );
107 }
108 }
109
110 function geolocation_field($op, $node, $field, &$items, $teaser, $page) {
111 switch($op) {
112 case 'validate':
113 foreach($items as $delta => $item) {
114 if ( (-90 >= $item['latitude']) && ($item['latitude'] <= 90) ) {
115 form_set_error($field['field_name'], t('Latitude is out of Range.'));
116 }
117 if ( (-180 >= $item['longitude']) && ($item['latitude'] <= 180) ) {
118 form_set_error($field['field_name'], t('Latitude is out of Range'));
119 }
120 }
121 break;
122
123 case 'insert':
124 case 'update':
125 foreach ($items as $delta => $item) {
126 // precalculate some goodness.
127 $item['latsin'] = sin(deg2rad($item['latitude']));
128 $item['latcos'] = cos(deg2rad($item['latitude']));
129 $item['longrads'] = deg2rad($item['longitude']);
130 $items[$delta] = $item;
131 }
132 $items = array_values($items); // compact deltas
133 }
134 }
135
136 /**
137 * @}
138 */
139
140
141 /**
142 * @defgoup geolocation_widget_hooks
143 * @{
144 *
145 * Plain widget for geolocation.
146 */
147
148 function geolocation_widget_info() {
149 return array(
150 'geolocation_textfields' => array(
151 'label' => 'Long./Lat. text fields',
152 'field types' => array('geolocation'),
153 ),
154 );
155 }
156
157 function geolocation_widget_settings() {
158 // This widget really has no settings...
159 // the google widget will probably require a gmap api key here.
160 }
161
162 function geolocation_widget($op, $node, $field, &$items) {
163 $fieldname = $field['field_name'];
164 switch($op) {
165 case 'form':
166 $form[$fieldname] = array(
167 '#type' => 'fieldset',
168 '#title' => $field['widget']['label'],
169 '#weight' => $field['widget']['weight'],
170 '#collapsible' => TRUE,
171 '#collapsed' => FALSE,
172 '#tree' => TRUE,
173 );
174
175 // if we got items, add em.
176 if (count($items)) {
177 foreach($items as $delta => $item) {
178 $form[$fieldname][$delta] = geolocation_widget_geolocation_textfields_form($item);
179 }
180 }
181 // if we're a multiple field add a few extra elements, if we're a single value add a new form.
182 if ($field['multiple'] || count($items) == 0) {
183 $form[$fieldname][0] = geolocation_widget_geolocation_textfields_form();
184 }
185 return $form;
186
187 case 'process form values':
188 // Don't save empty values, beyond the first value.
189 foreach ($items as $delta => $item) {
190 if ($item['value'] == '' && $delta > 0) {
191 unset($items[$delta]);
192 }
193 }
194 break;
195 }
196 }
197
198 function geolocation_widget_geolocation_textfields_form($item = array()) {
199 $form = array();
200 $form['latitude'] = array(
201 '#type' => 'textfield',
202 '#title' => t('Latitude'),
203 '#default_value' => $item['latitude'],
204 '#required' => TRUE,
205 );
206 $form['longitude'] = array(
207 '#type' => 'textfield',
208 '#title' => t('Longitude'),
209 '#default_value' => $item['longitude'],
210 '#required' => TRUE,
211 );
212 return $form;
213 }
214
215 /**
216 * @}
217 */
218
219
220 /**
221 * @defgoup geolocation_formatter_hooks
222 * @{
223 *
224 * Plain formatter for geolocation.
225 */
226
227 function geolocation_field_formatter_info() {
228 $formatters = array(
229 'geolocation_plain' => array(
230 'label' => 'Plain',
231 'field types' => array('geolocation'),
232 ),
233 );
234 return $formatters;
235 }
236
237 function geolocation_field_formatter($field, $item, $formatter) {
238 switch($formatter) {
239 case 'geolocation_plain':
240 return theme('geolocation_plain', $item);
241 }
242 }
243
244 function theme_geolocation_plain($item) {
245 $output = '<span class="latitude"><span class="label">'. t('Latitude') .':&nbsp;</span><span class="value">'. check_plain($item['latitude']) .'</span>&nbsp;';
246 $output .= '<span class="longitude"><span class="label">'. t('Longitude').':&nbsp;</span><span class="value">'. check_plain($item['longitude']) .'</span>';
247 return '<div class="geolocation">'. $output .'<div>';
248 }
249
250 /**
251 * @}
252 */

  ViewVC Help
Powered by ViewVC 1.1.2