-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgoogleMap.php
More file actions
81 lines (72 loc) · 2.39 KB
/
googleMap.php
File metadata and controls
81 lines (72 loc) · 2.39 KB
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
44
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
<?php
/**
* googleMap Output Modifier
*
* DESCRIPTION
*
* This output modifier accepts an address and returns a url for the static map image
*
* PARAMETERS: w, h, z, key - Google Maps API v3 Key, sensor (1 or 0), marker (1 or 0)
*
* OUTPUT MODIFIER USAGE:
* <img [[*tvAddress:googleMap=`w=425,h=300,z=16,key=xxxxxxxxxxx,sensor=0,marker=1`]] />
*
* key is the only REQUIRED variable, unless you set up a system setting named "google_api_key"
* then the snippet will use that value whenever 'key' is not passed
*
* SNIPPET USAGE:
*
* <img [[!googleMap? &address=`[[*tvAddress]]` &w=`425` &h=`300` &z=`16` &marker=`1` &sensor=`0`]] />
*
*
* AUTHOR: Jason Carney, DashMedia.com.au
*/
if(isset($options) && !is_null($options)){
//options included, execute as output modifier
$options = explode(",", $options);
foreach ($options as $key => $value) {
$line = explode("=", $value);
$settings[$line[0]] = $line[1];
}
if (isset($input) && !is_null($input)) {
$settings['address'] = $input;
}
} else {
//options not included, execute as snippet call
$settingsArray = array('w', 'h', 'z', 'address', 'key', 'sensor', 'marker');
foreach ($settingsArray as $key => $value) {
if(isset(${$value})){
$settings[$value] = ${$value};
}
}
}
if(!isset($settings['key']) || $settings['key'] == '' || is_null($settings['key'])){
//key not passed, check system settings for google api key
$settings['key'] = $modx->getOption('google_api_key');
}
//we now have a setings array populated
$required = array('key', 'address');
foreach ($required as $key => $value) {
if(!isset($settings[$value]) || is_null($settings[$value]) || $settings[$value] == ''){
return "googleMap Error: Missing Required Option: ".$value;
}
}
//we have all settings required, set defaults for any missing
$defaults = array(
'w' => 435,
'h' => 300,
'z' => 16,
'sensor' => 0,
'marker' => 1
);
$settings = array_merge($defaults, $settings);
//settings array ready with any non-set values set to defaults
$address = urlencode($settings['address']);
$center = '¢er='.$settings['address'];
$key = '&key='.$settings['key'];
$api = 'http://maps.googleapis.com/maps/api/staticmap?';
$size = '&size='.$settings['w'].'x'.$settings['h'];
$zoom = '&zoom='.$settings['z'];
$markers = $settings['marker']?'&markers='.$address:'';
$sensor = $settings['sensor']?'&sensor=true':'&sensor=false';
return $api.$center.$size.$zoom.$markers.$sensor.$key;