Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
// $Id$
/**
* @file
* GMap Filters is a module to include Google Map in a module
*
* GMap filter allows the insertion of a googlemap in a module. It has
* a page to creat a macro and then a filter to convet the macro into the
* html and javascript code required to insert a google map.
*/
// @@@ Remove these and generalize lines. @@@
// These are now stored in the map defaults, but not all the code uses it yet.
define('GMAP_LINECOLOR1', '#00cc00');
define('GMAP_LINECOLOR2', '#ff0000');
define('GMAP_LINECOLOR3', '#0000ff');
// @@@ Remove these and generalize lines. @@@
// Current minimum version: 2.113
// Minimum version last changed on: June 9 2008
// Reason: G_SATELLITE_3D_MAP support in gmap_addons. See http://code.google.com/apis/earth/.
//
// See http://groups.google.com/group/Google-Maps-API/web/api-version-changes
// for details on using other version numbers.
define('GMAP_API_VERSION', '2.115');
/**
* Get the defaults for a gmap.
*/
function gmap_defaults() {
$defaults = array(
'width' => '300px',
'height' => '200px',
'zoom' => 3,
'maxzoom' => 14,
'controltype' => 'Small',
'align' => 'None',
'latlong' => '40,0',
'maptype' => 'Map',
'mtc' => 'standard',
'baselayers' => array('Map', 'Satellite', 'Hybrid'),
'styles' => array(
'line_default' => array('0000ff', 5, 45, '', 0, 0),
'poly_default' => array('000000', 3, 25, 'ff0000', 45),
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
),
'line_colors' => array('#00cc00', '#ff0000', '#0000ff'),
);
$defaults['behavior'] = array();
$m = array();
$behaviors = module_invoke_all('gmap', 'behaviors', $m);
foreach ($behaviors as $k => $v) {
$defaults['behavior'][$k] = $v['default'];
}
$defaults = array_merge($defaults, variable_get('gmap_default', array()));
return $defaults;
}
/**
* Implementation of hook_gmap().
*/
function gmap_gmap($op, &$map) {
switch ($op) {
case 'macro':
return array(
'points' => array(
'multiple' => TRUE,
),
'markers' => array(
'multiple' => TRUE,
),
'feed' => array(
'multiple' => TRUE,
),
);
case 'pre_theme_map':
$path = drupal_get_path('module', 'gmap') .'/js/';
// Activate markers if needed
if ($map['behavior']['dynmarkers'] || !empty($map['markers'])) {
// The marker data is not a real file. We need to respect
// clean url capabilities here.
if (variable_get('clean_url', '0')) {
drupal_add_js('map/markerdata.js');
}
else {
drupal_add_js('?q=map/markerdata.js');
}
drupal_add_js($path .'icon.js');
drupal_add_js($path .'marker.js');
drupal_add_js($path . variable_get('gmap_mm_type', 'gmap') .'_marker.js');
}
if ($map['behavior']['locpick']) {
drupal_add_js($path .'locpick.js');
}
if (variable_get('gmap_load_zoom_plugin', TRUE) && !$map['behavior']['nomousezoom']) {
drupal_add_js(drupal_get_path('module', 'gmap') .'/thirdparty/mousewheel.js');
}
if ($map['markers'] || $map['lines']) {
drupal_add_js($path .'markerloader_static.js');
}
if ($map['shapes']) {
drupal_add_js($path .'shapeloader_static.js');
drupal_add_js($path .'gmap_shapes.js');
}
if (is_array($map['feed'])) {
drupal_add_js($path .'markerloader_georss.js');
}
break;
case 'macro_multiple':
return array('points', 'markers', 'feed', 'circle', 'rpolygon', 'polygon', 'line');
case 'behaviors':
return array(
'locpick' => array(
'title' => t('Location chooser'),
'default' => FALSE,
'help' => t('Used to activate location choosing using a gmap.'),
'internal' => TRUE,
),
'nodrag' => array(
'title' => t('Disable dragging'),
'default' => FALSE,
'help' => t('Remove the ability for the user to drag the map. If dragging is disabled, keyboard shortcuts are implicitly disabled.'),
),
'nokeyboard' => array(
'title' => t('Disable keyboard'),
'default' => TRUE,
'help' => t('Disable the keyboard shortcuts.'),
),
'nomousezoom' => array(
'title' => t('Disable mousezoom'),
'default' => FALSE,
'help' => t('Disable using the scroll wheel to zoom the map.'),
),
'autozoom' => array(
'title' => t('Use AutoZoom'),
'default' => FALSE,
'help' => t('Automatically zoom the map to fit all markers when markers are added.'),
),
'dynmarkers' => array(
'title' => t('Unconditionally enable marker interface'),
'default' => FALSE,
'help' => t('Load the marker loader system even if no markers to load are detected. Useful if you are injecting markers from somewhere else.'),
),
'overview' => array(
'title' => t('Enable Overview Map'),
'default' => FALSE,
'help' => t('Enable the "overview map" in the bottom right corner.'),
'previewable' => TRUE,
),
/* 'notype' => array(
'title' => t('Disable map type control'),
'default' => FALSE,
'help' => t('Removes the map type control from the upper right corner. Recommended for very narrow maps.'),
'previewable' => TRUE,
), */
'collapsehack' => array(
'title' => t('Work around bugs when maps appear in collapsible fieldsets'),
'default' => FALSE,
'help' => t('Enabling this will work around some issues that can occur when maps appear inside collapsible fieldsets.'),
),
// Note to myself, who keeps forgetting what a scale control actually IS.:
// |------------ 1mi ------------|
'scale' => array(
'title' => t('Add scale control to map.'),
'default' => FALSE,
'help' => t('Adds a scale control to the map in the default position.'),
'previewable' => TRUE,
),
);
break;
case 'baselayers':
$map['Google']['Map'] = array(
'title' => t('Map: Standard street map.'),
'default' => TRUE,
'help' => t('The standard default street map. Internal name: G_NORMAL_MAP'),
);
$map['Google']['Satellite'] = array(
'title' => t('Satellite: Standard satellite map.'),
'default' => TRUE,
'help' => t('Satellite view without street overlay. Internal name: G_SATELLITE_MAP'),
);
$map['Google']['Hybrid'] = array(
'title' => t('Hybrid: Hybrid satellite map.'),
'default' => TRUE,
'help' => t('Satellite view with street overlay. Internal name: G_HYBRID_MAP'),
);
$map['Google']['Physical'] = array(
'title' => t('Terrain: Physical feature map.'),
'default' => FALSE,
'help' => t('Map with physical data (terrain, vegetation.) Internal name: G_PHYSICAL_MAP'),
);
break;
}
}
/**
* Set up the HTML header for GMap.
*/
function _gmap_doheader() {
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
static $gmap_initialized = FALSE;
if ($gmap_initialized) {
return;
}
$gmap_path = drupal_get_path('module', 'gmap');
drupal_add_css($gmap_path .'/gmap.css');
drupal_add_js($gmap_path .'/js/gmap.js');
$mm = variable_get('gmap_mm_type', 'gmap');
if ($mm=='clusterer') {
drupal_add_js($gmap_path .'/js/icon.js');
drupal_add_js($gmap_path .'/thirdparty/Clusterer2.js');
}
drupal_add_js($gmap_path .'/js/marker.js');
drupal_add_js($gmap_path .'/js/'. $mm .'_marker.js');
$mms = variable_get('gmap_markermanager', array());
if (empty($mms[$mm])) {
$mms[$mm] = array();
}
drupal_add_js(array('gmap_markermanager' => $mms[$mm]), 'setting');
// @@@
drupal_add_js($gmap_path .'/js/poly.js');
$key = variable_get('googlemap_api_key', '');
if (module_exists('keys_api')) {
$key = keys_api_get_key('gmap', $_SERVER['HTTP_HOST']);
}
drupal_set_html_head('<script src="'. check_url('http://maps.google.com/maps?file=api&v='. variable_get('gmap_api_version', GMAP_API_VERSION) .'&key='. $key) .'" type="text/javascript"></script>');
drupal_add_js(array(
'gmap_init' => array(
'querypath' => base_path() .'map/query',
),
), 'setting');
$gmap_initialized = TRUE;
}
/**
* Parse a macro style definition.
* Example: #111111/1/100/#111111/1
*/
function _gmap_parse_style($style) {
$styles = explode('/', $style);
// @@@ Todo: Fix up old xmaps stuff. Possibly detect by looking for array length 7?
// Strip off # signs, they get added by code.
if (isset($styles[0]) && substr($styles[0], 0, 1) == '#') {
$styles[0] = substr($styles[0], 1);
}
if (isset($styles[3]) && substr($styles[3], 0, 1) == '#') {
$styles[3] = substr($styles[3], 1);
}
// Assume anything > 0 and < 1.1 was an old representation.
if ($styles[2] > 0 && $styles[2] < 1.1) {
$styles[2] = $styles[2] * 100;
}
if (isset($styles[4])) {
if ($styles[4] > 0 && $styles[4] < 1.1) {
$styles[4] = $styles[4] * 100;
}
}
return $styles;
}
/**
* Convert a macro string into a GMap array.
*
* @param $instring
* Macro to process.
* @param $ver
* Version to treat macro as.
* Set to 1 when processing very old macros, otherwise leave as is.
* @return
* A GMap array.
*/
function gmap_parse_macro($instring, $ver = 2) {
// Get a list of keys that are "multiple."
$m = array();
$multiple = module_invoke_all('gmap', 'macro_multiple', $m);
// Remove leading and trailing tags
if (substr(trim($instring), -1)==']') {
$instring = substr(trim($instring), 0, -1);
}
if (substr($instring, 0, 5)=='[gmap') {
$instring = substr($instring, 6);
}
// Chop the macro into an array
$temp = explode('|', $instring);
$m = array();
foreach ($temp as $row) {
$offset = strpos($row, '=');
if ($offset !== FALSE) {
$k = trim(substr($row, 0, $offset));
$r = trim(substr($row, $offset+1));
if (in_array($k, $multiple)) {
// Things that can appear multiple times
if (!isset($m[$k])) {
$m[$k] = array();
}
$m[$k][] = $r;
}
else {
$m[$k] = $r;
}
}
}
// Synonyms
if ($m['type']) {
$m['maptype'] = $m['type'];
unset($m['type']);
}
if ($m['control']) {
$m['controltype'] = $m['control'];
unset($m['control']);
}
if (is_array($m['feed'])) {
foreach ($m['feed'] as $k => $v) {
$temp = explode('::', $v);
// Normalize url
if (substr($temp[1], 0, 1) == '/') {
$temp[1] = substr($temp[1], 1);
}
$temp[1] = url($temp[1]);
$m['feed'][$k] = array(
'markername' => $temp[0],
'url' => $temp[1],
);
}
}
// Merge points and markers
if (!is_array($m['points'])) $m['points'] = array();
if (!is_array($m['markers'])) $m['markers'] = array();
$m['markers-temp'] = array_merge($m['points'], $m['markers']);
unset($m['points']);
unset($m['markers']);
// all shapes in 1 array
if ($m['circle']) {
foreach ($m['circle'] as $shape) {
$s = array('type' => 'circle');
$cp = strpos($shape, ':');
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
$stylestr = substr($shape, 0, $cp);
$s['style'] = _gmap_parse_style($stylestr);
$shape = substr($shape, $cp+1);
}
$tmp = explode('+', $shape);
$s['radius'] = $tmp[1] ? $tmp[1] : 100;
if ($tmp[2]) $s['numpoints'] = trim($tmp[2]);
$tmp = _gmap_str2coord($tmp[0]);
$s['center'] = $tmp[0];
$m['shapes'][] = $s;
}
unset($m['circle']);
}
// Fixup legacy lines.
if ($m['line1']) {
if (!isset($m['line'])) $m['line'] = array();
$m['line'][] = GMAP_LINECOLOR1 .':'. $m['line1'];
unset($m['line1']);
}
if ($m['line2']) {
if (!isset($m['line'])) $m['line'] = array();
$m['line'][] = GMAP_LINECOLOR2 .':'. $m['line3'];
unset($m['line2']);
}
if ($m['line3']) {
if (!isset($m['line'])) $m['line'] = array();
$m['line'][] = GMAP_LINECOLOR3 .':'. $m['line3'];
unset($m['line3']);
}
if ($m['line']) {
foreach ($m['line'] as $shape) {
$s = array('type' => 'line');
$cp = strpos($shape, ':');
$stylestr = substr($shape, 0, $cp);
$s['style'] = _gmap_parse_style($stylestr);
$shape = substr($shape, $cp+1);
}
$s['points'] = _gmap_str2coord($shape);
$m['shapes'][] = $s;
}
unset($m['line']);
}
if ($m['rpolygon']) {
foreach ($m['rpolygon'] as $shape) {
$s = array('type' => 'rpolygon');
$cp = strpos($shape, ':');
$stylestr = substr($shape, 0, $cp);
$s['style'] = _gmap_parse_style($stylestr);
$shape = substr($shape, $cp+1);
}
$tmp = explode('+', $shape);
if ($tmp[2]) {
$s['numpoints'] = (int)trim($tmp[2]);
$tmp = array_slice($tmp, 0, 2);
}
$shape = implode('+', $tmp);
$tmp = _gmap_str2coord($shape);
$s['center'] = $tmp[0];
$s['point2'] = $tmp[1];
$m['shapes'][] = $s;
}
unset($m['rpolygon']);
}
if ($m['polygon']) {
foreach ($m['polygon'] as $shape) {
$s = array('type' => 'polygon');
$cp = strpos($shape, ':');
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
$stylestr = substr($shape, 0, $cp);
$s['style'] = _gmap_parse_style($stylestr);
$shape = substr($shape, $cp+1);
}
$s['points'] = _gmap_str2coord($shape);
$m['shapes'][] = $s;
}
unset($m['polygon']);
}
// Version 1 -> 2 conversion
if ($ver == 1) {
// Zoom is flipped
if ($m['zoom']) {
$m['zoom'] = 18 - $m['zoom'];
if ($m['zoom'] < 1) {
$m['zoom'] = 1;
}
}
}
// Center -> latitude and longitude
if ($m['center']) {
list($m['latitude'], $m['longitude']) = explode(',', $m['center']);
unset($m['center']);
}
// Behavior
if ($m['behaviour']) {
$m['behavior'] = $m['behaviour'];
unset($m['behaviour']);
}
if ($m['behavior']) {
$m['behavior-temp'] = explode(' ', $m['behavior']);
$m['behavior'] = array();
foreach ($m['behavior-temp'] as $v) {
$m['behavior'][substr($v, 1)] = (substr($v, 0, 1) == '+') ? TRUE : FALSE;
}
unset($m['behavior-temp']);
}
// tcontrol now is mtc.
if ($m['tcontrol']) {
if (strtolower(trim($m['tcontrol'])) == 'on') {
$m['mtc'] = 'standard';
}
else {
$m['mtc'] = 'none';
}
unset($m['tcontrol']);
}
// notype also controls mtc.
if (isset($m['behavior']['notype'])) {
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
if ($m['behavior']['notype']) {
$m['mtc']= 'none';
}
unset($m['behavior']['notype']);
}
// Stuff that was converted to behavior flags
// Scale control.
if ($m['scontrol']) {
if (strtolower(trim($m['scontrol'])) == 'on') {
$m['behavior']['scale'] = TRUE;
}
else {
$m['behavior']['scale'] = FALSE;
}
unset($m['scontrol']);
}
// Draggability.
if ($m['drag']) {
if (strtolower(trim($m['drag'])) == 'yes') {
$m['behavior']['nodrag'] = FALSE;
}
else {
$m['behavior']['nodrag'] = TRUE;
}
unset($m['drag']);
}
// Markers fixup
foreach ($m['markers-temp'] as $t) {
unset($markername);
// Named?
if (strpos($t, '::')) { // Single : gets handled below.
list($markername, $t) = explode('::', $t, 2);
}
// Break down into points
$points = explode('+', $t);
$offset = -1;
foreach ($points as $point) {
$marker = array();
$offset++;
$marker['options'] = array();
// Labelled?
// @@@ Gmap allows both a tooltip and a popup, how to represent?
if (strpos($point, ':')) {
list($point, $marker['text']) = explode(':', $point, 2);
$marker['text'] = theme('gmap_marker_popup', $marker['text']);
}
if (strpos($point, '%')) {
list($point, $addons) = explode('%', $point, 2);
$motemp = explode('%', $addons);
foreach ($motemp as $option) {
$marker['options'][trim($option)] = TRUE;
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
}
}
list($marker['latitude'], $marker['longitude']) = explode(',', $point, 2);
// Named markers get an offset too.
if (isset($markername)) {
$marker['markername'] = $markername;
$marker['offset'] = $offset;
}
$m['markers'][] = $marker;
}
}
unset($m['markers-temp']);
// Assign an id if one wasn't specified.
if (!$m['id']) {
$m['id'] = gmap_get_auto_mapid();
}
// The macro can now be manipulated by reference.
foreach (module_implements('gmap') as $module) {
$additions = call_user_func_array($module .'_gmap', array('parse_macro', &$m));
if (!empty($additions)) {
foreach ($additions as $k => $v) {
$m[$k] = $v;
}
}
}
return $m;
}
/**
* Parse "x.xxxxx , y.yyyyyy (+ x.xxxxx, y.yyyyy ...)" into an array of points.
*/
function _gmap_str2coord($str) {
// Explode along + axis
$arr = explode('+', $str);
// Explode along , axis
$points = array();
foreach ($arr as $pt) {
list($lat, $lon) = explode(',', $pt);
$points[] = array((float)trim($lat), (float)trim($lon));
}
return $points;
}
/**
* Theme a marker popup.
* This will get called for markers embedded in macros.
*/
function theme_gmap_marker_popup($label) {
return $label;
}
/**
* Location chooser utility function.
*
* Creates a map that can be interactively used to fill a form with a
* location (latitude, longitude and zoom level).
*
* Note: This is a utility function designed for location.module, there is no
* guarantee it will not be removed eventually.
*
* @param $map
* Either a macro to use as the base map for setting a location, or an already set map associative array.
* @param $form
* A formset associative array. Cannot be more than one deep.
* @param $fields
* An associative array for the field names. 'latitude', 'longitude'=>name of respective array, 'address' is optional.
* @return
* A string with the google map code to be inserted onto the page.
*
*/
function gmap_set_location($map, &$form, $fields) {
static $ctr = 0;
$ctr++;
if (!is_array($map)) {
$map = array_merge(gmap_defaults(), gmap_parse_macro($map));
}
$id = 'loc'. $ctr;
$map['id'] = $id;
// This is a locpick map.
$map['behavior']['locpick'] = TRUE;
$element = array(
'#type' => 'gmap',
'#map' => $map['id'],
'#settings' => $map,
);
$form[$fields['latitude']]['#map']=$id;
gmap_widget_setup($form[$fields['latitude']], 'locpick_latitude');
$form[$fields['longitude']]['#map']=$id;
gmap_widget_setup($form[$fields['longitude']], 'locpick_longitude');
if (isset($fields['address'])) {
$form[$fields['address']]['#map'] = $id;
gmap_widget_setup($form[$fields['address']], 'locpick_address');
}
return theme('gmap', $element);
}
/**
* Handle filter preparation.
*/
function _gmap_prepare($intext) {
$out = FALSE;
$matches = array();
preg_match_all('/\[gmap([^\[\]]+ )* \] /x', $intext, $matches);
$i = 0;
while (isset($matches[1][$i])) {
$out[0][$i] = $matches[0][$i];
if ($matches[1][$i][0] == '1') {
$ver = 1;
$matches[1][$i] = substr($matches[0][$i], 1);
}
else {
$ver = 2;
}
$map = array('#settings' => gmap_parse_macro($matches[1][$i], $ver));
$out[1][$i] = theme('gmap', $map);
$i++;
} // endwhile process macro
return $out;
}
/**
* Make sure a string is a valid css dimension.
*/
function gmap_todim($instring) {
$s = strtolower($instring);
$matches = array();
if (preg_match('/([\d.]+)\s*(em|ex|px|in|cm|mm|pt|pc|%)/', $s, $matches)) {
return $matches[1] . $matches[2];
}
else {
return FALSE;
}
}
/**
* Ensure a textfield is a valid css dimension string.
*/
function gmap_dimension_validate(&$elem) {
if (!gmap_todim($elem['#value'])) {
form_error($elem, t('The specified value is not a valid CSS dimension.'));
}
}
/**
* Implementation of hook_filter().
*/
function gmap_filter($op, $delta = 0, $format = -1, $text = '') {
switch ($op) {
case 'list':
return (array(0 => t('GMap filter')));
case 'name':
return t('Google map filter');
case 'description':
return t('converts a google map macro into the html required for inserting a google map.');
case 'process':
$gmaps = _gmap_prepare($text); //returns an array of $tables[0] = table macro $table[1]= table html
if ($gmaps) { // there are table macros in this node
return str_replace($gmaps[0], $gmaps[1], $text);
}
else {
return $text;
}
case 'prepare':
return $text;
case 'no cache':
return TRUE; // @@@ Possibly improve efficiency in the future?
}
}
/**
* Implementation of hook_filter_tips().
*/
function gmap_filter_tips($delta, $format, $long = FALSE) {
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
if (user_access('create macro')) { // only display macro if user can create one
return t('Insert Google Map macro.') .'<a href="'. url('map/macro') .'" target="_blank" >'. t('Create a macro') .'</a>';
}
else {
return t('Insert Google Map macro.');
}
}
/**
* Implementation of hook_menu().
*/
function gmap_menu($may_cache) {
if ($may_cache) {
$items = array();
/* $items[] = array(
'path' => 'map',
'type' => MENU_ITEM_GROUPING,
'title' => t('google maps'),
'access' => user_access('create macro')||user_access('show user map')||user_access('show node map'),
); */
$items[] = array(
'path' => 'admin/settings/gmap',
'title' => t('GMap'),
'description' => t('Configure GMap settings'),
'callback' => 'drupal_get_form',
'callback arguments' => 'gmap_admin_settings',
'access' => user_access('administer site configuration'),
'type' => MENU_NORMAL_ITEM,
);
$items[] = array(
'path' => 'map/query',
'type' => MENU_CALLBACK,
'access' => user_access('access content'),
'callback' => 'gmap_json_query',
);
$items[] = array(
'path' => 'map/markerdata.js',
'type' => MENU_CALLBACK,
'access' => user_access('access content'),
'callback' => 'gmap_markerdata_js',
);
return $items;
}
}
/**
* Generate the markerdata file.
*/
function gmap_markerdata_js() {
drupal_set_header('Content-Type: text/javascript');
echo "// GMap marker image data.\n";
echo "Drupal.gmap.iconpath = ". drupal_to_js(base_path() . drupal_get_path('module', 'gmap') .'/markers') .";\n";
echo "Drupal.gmap.icondata = ". drupal_to_js(gmap_get_icondata()) .";\n";
}
/**
* JSON request interface.
*/
function gmap_json_query() {
if (arg(2)=='markers') {
drupal_set_header('Content-Type: text/javascript');
echo drupal_to_js(array(
'path' => base_path() . drupal_get_path('module', 'gmap') .'/markers',
'markers' => gmap_get_icondata(TRUE),
));
exit();
}
}
/**
* Settings page. (remove for d6)
*/
function gmap_admin_settings() {
require_once(drupal_get_path('module', 'gmap') .'/gmap_settings_ui.inc');
return _gmap_admin_settings();
}
/**
* Implementation of hook_elements().
*/
function gmap_elements() {
return array(
'gmap' => array(
'#input' => FALSE, // This isn't a *form* input!!
'#settings' => array_merge(gmap_defaults(), array(
'points' => array(),
'pointsOverlays' => array(),
'lines' => array(),
)),
'#process' => array('expand_gmap' => array()),
),
'gmap_macrotext' => array(
'#input' => TRUE,
'#cols' => 60,
'#rows' => 5,
'#process' => array(
'process_gmap_control' => array('textarea', 'macrotext'),
),
),
'gmap_overlay_edit' => array('#input' => FALSE, '#process' => array('process_gmap_overlay_edit' => array())),
'gmap_style' => array('#input' => TRUE, '#tree' => TRUE, '#gmap_style_type' => 'poly', '#process' => array('process_gmap_style' => array())),
'gmap_address' => array('#input' => FALSE, '#process' => array('process_gmap_address' => array())),
'gmap_align' => array('#input' => TRUE, '#process' => array('process_gmap_align' => array())),
'gmap_latitude' => array('#input' => TRUE, '#process' => array('process_gmap_control' => array('textfield', 'latitude', 'gmap_coord'))),
'gmap_longitude' => array('#input' => TRUE, '#process' => array('process_gmap_control' => array('textfield', 'longitude', 'gmap_coord'))),
'gmap_latlon' => array('#input' => TRUE, '#process' => array('process_gmap_control' => array('textfield', 'latlon', 'gmap_coord'))),
'gmap_markerchooser' => array('#input' => TRUE, '#process' => array('process_gmap_markerchooser' => array())),
'gmap_dimension' => array('#input' => TRUE, '#theme' => 'textfield', '#validate' => array('gmap_dimension_validate' => array())),
);
}
/**
* Gmap element #process function.
*/
function expand_gmap($element) {
$mapid = 'map';
if ($element['#map']) {
$mapid = $element['#map'];
}
else {
$element['#map'] = $mapid;
}
if (!$element['#settings']) {
$element['#settings'] = array();
}
// @@@
$element['#settings'] = array_merge(gmap_defaults(), array(
'id' => $mapid,
'points' => array(),
'pointsOverlays' => array(),
'lines' => array(),
), $element['#settings']);
gmap_widget_setup($element, 'gmap');
return $element;
}
/**
* Generic gmap control #process function.
*/
function process_gmap_control($element, $edit, $fieldtype, $control, $theme='') {
$element['#type'] = $fieldtype;
gmap_widget_setup($element, $control);
if (!empty($theme)) {
$element['#theme'] = $theme;
}
else {
$element['#theme'] = 'gmap_'. $control;
}
return $element;
}
/**
* Style fieldset #process function.
*/
function process_gmap_style($element) {
$isline = ($element['#gmap_style_type'] == 'line');
// Stroke color
$element[0] = array(
'#type' => 'textfield',
'#size' => 6,
'#maxlength' => 6,
'#field_prefix' => '#',
'#title' => t('Stroke color'),
'#value' => $element['#value'][0],
'#attributes' => array('class' => 'gmap_style'),
);
// Stroke weight
$element[1] = array(
'#type' => 'textfield',
'#title' => t('Stroke weight'),
'#description' => t('Thickness of line, in pixels.'),
'#size' => 3,
'#maxlength' => 3,
'#field_suffix' => t('px'),
'#value' => $element['#value'][1],
'#attributes' => array('class' => 'gmap_style'),
);
// Stroke opacity
$element[2] = array(
'#type' => 'textfield',
'#title' => t('Stroke opacity'),
'#size' => 3,
'#maxlength' => 3,
'#field_suffix' => '%',
'#value' => $element['#value'][2],
'#attributes' => array('class' => 'gmap_style'),
);
// Fill color
$element[3] = array(
'#type' => 'textfield',
'#title' => t('Fill color'),
'#description' => t('Hex color value for fill color. Example: #<em>00AA33</em>'),
'#size' => 6,
'#maxlength' => 6,
'#field_prefix' => '#',
'#value' => $element['#value'][3],
'#disabled' => $isline,
'#attributes' => array('class' => 'gmap_style'),
);
$element[4] = array(
'#type' => 'textfield',
'#title' => t('Fill opacity'),
'#description' => t('Opacity of fill, from 0 to 100%.'),
'#size' => 3,
'#maxlength' => 3,
'#field_suffix' => '%',
'#value' => $element['#value'][4],
'#disabled' => $isline,
'#attributes' => array('class' => 'gmap_style'),
);
return $element;
}
/**
* Theme a gmap_style fieldset.
*/
function theme_gmap_style($element) {
// Fieldsets print #value at the end, so we need to empty it out.
// Otherwise, it puts "Array" at the end of the fieldset.
$element['#value'] = '';
return theme('fieldset', $element, $element['#children']);
}
/**
* Overlay editor #process function.
*/
function process_gmap_overlay_edit($element) {
// Conver the root element into a fieldset.
$element['#type'] = 'fieldset';
if (!$element['#title']) {
$element['#title'] = t('Overlay editor');
}
$element['#tree'] = TRUE;
$element['mapclicktype'] = array(
'#type' => 'select',
'#title' => t('Click map'),
'#map' => $element['#map'],
'#options' => array(
'Points' => t('Points'),
'Lines' => t('Lines'),
'Circles' => t('Circles'),
'GPolygon' => t('Filled Polygons'),
),
);
gmap_widget_setup($element['mapclicktype'], 'overlayedit_mapclicktype');
$element['markerclicktype'] = array(
'#type' => 'select',
'#title' => t('Click marker / segment'),
'#map' => $element['#map'],
'#options' => array(
'Remove' => t('Remove'),
// 'updatestyle' => t('Update Styles'),
// 'removestyle' => t('Remove Styles'),
'Edit Info' => t('Edit Info'),
),
);
gmap_widget_setup($element['markerclicktype'], 'overlayedit_markerclicktype');
$element['marker'] = array(
'#type' => 'select',
'#map' => $element['#map'],
'#options' => gmap_get_marker_titles(),
'#title' => t('Marker'),
'#theme' => 'gmap_overlay_edit',
);
gmap_widget_setup($element['marker'], 'overlayedit');
$element['linestyle'] = array(
'#type' => 'gmap_style',
'#title' => t('Line style'),
'#gmap_style_type' => 'line',
'#default_value' => array('0000ff', 5, 45, '', ''), // @@@
);
gmap_widget_setup($element['linestyle'], 'overlayedit_linestyle', $element['#map']);
$element['linestyle']['linestyle_apply'] = array(
'#tree' => FALSE,
'#type' => 'checkbox',
'#title' => t('Use for new and changed lines'),
'#default_value' => FALSE,
);
gmap_widget_setup($element['linestyle']['linestyle_apply'], 'overlayedit_linestyle_apply', $element['#map']);
$element['polystyle'] = array(
'#type' => 'gmap_style',
'#title' => t('Polygon style'),
'#gmap_style_type' => 'poly',
'#default_value' => array('000000', 3, 25, 'ff0000', 45), // @@@
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
);
gmap_widget_setup($element['polystyle'], 'overlayedit_polystyle', $element['#map']);
$element['polystyle']['polystyle_apply'] = array(
'#tree' => FALSE,
'#type' => 'checkbox',
'#title' => t('Use for new and changed polygons'),
'#default_value' => FALSE,
);
gmap_widget_setup($element['polystyle']['polystyle_apply'], 'overlayedit_polystyle_apply', $element['#map']);
return $element;
}
/**
* Alignment selector #process function.
*/
function process_gmap_align($element) {
$element['#type'] = 'select';
gmap_widget_setup($element, 'align');
$element['#options'] = drupal_map_assoc(array('None', 'Right', 'Left', 'Center'));
$element['#theme'] = 'gmap_align';
return $element;
}
/**
* Address widget #process function.
*/
function process_gmap_address($element) {
$element['#type'] = 'textfield';
gmap_widget_setup($element, 'address');
$element['#theme'] = 'gmap_address';
return $element;
}
/**
* Marker chooser #process function.
*/
function process_gmap_markerchooser($element) {
$element['#type'] = 'select';
$element['#options'] = gmap_get_marker_titles();
return $element;
}
/**
* Overlay editor theme function.
*/
function theme_gmap_overlay_edit($element) {
$path = drupal_get_path('module', 'gmap');
drupal_add_js($path .'/js/gmap.js');
drupal_add_js($path .'/js/gmap_shapes.js');
drupal_add_js($path .'/js/overlay_edit.js');
return theme('select', $element);
}
/**
* Perform some normalization on the map object
* to prevent errors.
*/
function gmap_map_cleanup(&$map) {
// Google is picky about this one.
$map['zoom'] = (int)$map['zoom'];
// Normalize latitude / longitude
if ($map['latlong']) {
$map['latlon'] = $map['latlong'];
unset($map['latlong']);
}
if (isset($map['latlon']) && (!isset($map['latitude']) || !isset($map['longitude']))) {
list($map['latitude'], $map['longitude']) = explode(',', $map['latlon']);
}
unset($map['latlon']);
foreach ($map['baselayers'] as $k => $v) {
if (!$v) {
unset($map['baselayers'][$k]);
}
}
}
function theme_gmap_coord($element) {
//drupal_add_js
return theme('textfield', $element);
}
function theme_gmap_macrotext($element) {
drupal_add_js(drupal_get_path('module', 'gmap') .'/js/macro.js');
// @@@
drupal_add_js(drupal_get_path('module', 'gmap') .'/js/macrobuilder.js');
return theme('textarea', $element);
}
function theme_gmap_address($element) {
drupal_add_js(drupal_get_path('module', 'gmap') .'/js/address.js');
return theme('textfield', $element);
}
function theme_gmap_align($element) {
drupal_add_js(drupal_get_path('module', 'gmap') .'/js/align.js');
return theme('select', $element);
}
/**
* Gmap element theme hook
*/
function theme_gmap($element) {
_gmap_doheader();
// If the theme function is called directly, the map is not properly set up yet.
if (!$element['#id']) {
gmap_widget_setup($element, 'gmap');
}
// Possible if this gmap was created manually.
if (!$element['#settings']) {
$element['#settings'] = array();
}
$map = array_merge(gmap_defaults(), $element['#settings']);
gmap_map_cleanup($map);
switch (strtolower($map['align'])) {
case 'left':
$element['#attributes']['class'] += ' gmap-left';
break;
case 'right':
$element['#attributes']['class'] += ' gmap-right';
break;
case 'center':
case 'centre':
$element['#attributes']['class'] += ' gmap-center';
}
$style = array();
$style[] = 'width: '. $map['width'];
$style[] = 'height: '. $map['height'];
$element['#attributes']['class'] = trim($element['#attributes']['class'] .'gmap gmap-map gmap-'. $map['id'] .'-gmap');
// Some markup parsers (IE) don't handle empty inners well. Use the space to let users know javascript is required.
// @@@ Bevan sez: Google static maps could be useful here.
// @@@ Bdragon sez: Yeah, would be nice, but hard to guarantee functionality. Not everyone uses the static markerloader.
$o = '<div style="'. implode('; ', $style) .';"'. drupal_attributes($element['#attributes']) .'>'. t('Javascript is required to view this map.') .'</div>';
// $map can be manipulated by reference.
foreach (module_implements('gmap') as $module) {
call_user_func_array($module .'_gmap', array('pre_theme_map', &$map));
}
// Inline settings extend.
$o .= '<script type="text/javascript">'."\n";
$o .= "/* <![CDATA[ */\n";
$o .= 'Drupal.extend({ settings: '. drupal_to_js(array('gmap' => array($element['#map'] => $map))) ." });\n";
$o .= "/* ]]> */\n";
$o .= "</script>\n";
return $o;
}
/**
* Set up widget.
* This function will change a form element's ID so it is found
* by the GMap handlers system.
* @param &$element
* The form element to modify.
* @param $type
* The gmap widget type to map to.
* @param $map
* The map id. If not defined, $element['#map'] will be used.
* @return
* None.
*/
function gmap_widget_setup(&$element, $type, $map=NULL) {
if (!$map) {
if (isset($element['#map'])) {
$map = $element['#map'];
}
else {
// Hmm, missing #map. Try to figure it out.
if (isset($element['#settings']['id'])) {
$map = $element['#settings']['id'];
}
}
}
$element['#attributes']['class'] = trim(implode(' ', array(
$element['#attributes']['class'],
'gmap-control',
'gmap-'. $type,
)));
$element['#id'] = gmap_get_id($map, $type);
$element['#attributes']['id'] = $element['#id'];
$element['#map'] = $map;
}
/**
* Get a CSS id for a map and type.
* Since CSS ids have to be unique, GMap related IDs are assigned by
* this function.
*/
function gmap_get_id($map, $type) {
static $serial = array();
if (!isset($serial[$map])) {
$serial[$map] = array();
}
if (!isset($serial[$map][$type])) {
$serial[$map][$type] = -1;
}
$serial[$map][$type]++;
return 'gmap-'. $map .'-'. $type . $serial[$map][$type];
}
/**
* Generate a dynamic map identifier.
*/
function gmap_get_auto_mapid() {
static $auto = 0;
$auto++;
return 'auto'. $auto .'map';
}
/**
* Get the list of marker titles.
*/
function gmap_get_marker_titles($reset = FALSE) {
static $titles;
if (is_array($titles) && !$reset) {
return $titles;
}
$titles = cache_get('gmap_marker_titles');
if ($titles) {
$titles = unserialize($titles->data);
}
if ($reset || !$titles) {
require_once(drupal_get_path('module', 'gmap') .'/gmap_markerinfo.inc');
$titles = _gmap_get_marker_titles();
}
cache_set('gmap_marker_titles', 'cache', serialize($titles));
return $titles;
}
/**
* Get the JSON icon data for all the default markers.
*/
function gmap_get_icondata($reset = FALSE) {
static $icons;
if (is_array($icons) && !$reset) {
return $icons;
}
$icons = cache_get('gmap_icondata');
if ($icons) {
$icons = unserialize($icons->data);
}
if ($reset || !$icons) {
require_once(drupal_get_path('module', 'gmap') .'/gmap_markerinfo.inc');
$icons = _gmap_get_icondata();
}
cache_set('gmap_icondata', 'cache', serialize($icons));
return $icons;
}
/**
* Utility function to allow high-precision decimals to work with the SQL layer.
* Use concatenation. (Apparently unquoted %s is bad.)
*/
function gmap_decimal($num) {
// Paraphrased from postgresql documentation:
//
// Numbers in SQL can be in one of these forms:
// digits
// digits.[digits][e[+-]digits]
// [digits].digits[e[+-]digits]
// digitse[+-]digits
// where "digits" is one or more decimal digits.
// Trim extra whitespace
$num = trim($num);
// Check if we're in an acceptable form.
if (preg_match('/^[+\-]?((\d+)|(\d+\.\d*)|(\d*\.\d+))(e[+\-]?\d+)?$/', $num)===1) {
// Good, we can pass that right along.
return $num;
}
// Otherwise, cast to float, possibly losing precision.
return (float) $num;
}
/**
* Utility function to use the google maps geocoder server side.
* This is an easy, quick way to geocode a single address.
* Note: This is a REMOTE CALL TO GOOGLE. Do NOT use this where performance matters,
* as it could possibly take several seconds for this function to return.
* See http://www.google.com/apis/maps/documentation/reference.html#GGeoStatusCode
* for a description of the possible status codes.
*/
function gmap_geocode($address, $tld = 'com') {
$key = variable_get('googlemap_api_key', '');
if (module_exists('keys_api')) {
$key = keys_api_get_key('gmap', $_SERVER['HTTP_HOST']);
}
$data = drupal_http_request('http://maps.google.'. $tld .'/maps/geo?q='. drupal_urlencode($address) .'&output=csv&key='. $key);
if ($data->code == 200) {
$r = explode(',', $data->data);
return array(
'status' => (int)$r[0],
'accuracy' => (int)$r[1],
'latitude' => $r[2],
'longitude' => $r[3],
);
}
// Non 200 is G_GEO_SERVER_ERROR (500).
return array(
'status' => 500,
);
}
/**
* Simple way to draw a map from inside a theme.
* @param $latitude
* Latitude of marker.
* @param $longitude
* Longitude of marker.
* @param $markername
* Marker to use.
* '' will fall back to google's default marker.
* @param $info
* What to show in the bubble when the marker is clicked.
* Leave blank if you don't want a bubble.
* @param $zoom
* Map zoom.
* 'default' will use the default zoom from the settings page.
* 3 is usually a good value to use.
* @param $width
* Map width.
* 'default' will use the default width from the settings page.
* @param $height
* Map height.
* 'default' will use the default height from the settings page.
* @param $autoshow
* If set to TRUE, automatically show the marker bubble.
* @param $map
* Override parts of the map array.
* If you need to do much with this, you should probabaly be putting together
* the map array manually.
*/
function gmap_simple_map($latitude, $longitude, $markername = '', $info = '', $zoom = 'auto', $width = 'default', $height = 'default', $autoshow = FALSE, $map = array()) {
$settings = array(
'id' => gmap_get_auto_mapid(),
'latitude' => $latitude, // Center the map
'longitude' => $longitude, // on the marker.
);
if ($zoom != 'default') {
$settings['zoom'] = $zoom;
}
if ($width != 'default') {
$settings['width'] = $width;
}
if ($height != 'default') {
$settings['height'] = $height;
}
$settings['markers'] = array(array(
'latitude' => $latitude,
'longitude' => $longitude,
'markername' => $markername,
'offset' => 0,
));
if (!empty($info)) {
$settings['markers'][0]['text'] = $info;
}
if ($autoshow) {
$settings['markers'][0]['autoclick'] = TRUE;
}
if (!empty($map)) {
$settings = array_merge($settings, $map);
}
return theme('gmap', array('#settings' => $settings));
}
/**
* Implementation of hook_keys_service(). (from the keys api)
*/
function gmap_keys_service() {
return array(
'gmap' => array(
'name' => t('Gmap'),
'description' => t('Google Maps API Key'),
),
);
}