|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Clubdeuce\WPGoogleMaps; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Map |
|
7
|
|
|
* @package Clubdeuce\WPGoogleMaps |
|
8
|
|
|
* |
|
9
|
|
|
* @method bool fit_bounds() |
|
10
|
|
|
* @method array styles() |
|
11
|
|
|
* @method bool use_clusters() |
|
12
|
|
|
*/ |
|
13
|
|
|
class Map_Model extends Model_Base { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var null|string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $_background_color = null; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $_center; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* If set to true, the map's zoom level will be altered to ensure all markers are |
|
27
|
|
|
* in the viewport. |
|
28
|
|
|
* |
|
29
|
|
|
* @var bool |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $_fit_bounds; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* The Map element height (default: 400px). |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $_height = '400px'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The string to use for the map HTML element id property |
|
41
|
|
|
* |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $_html_id; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var Marker[] |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $_markers = array(); |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $_styles = array(); |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var bool |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $_use_clusters; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* The Map element width (default: 100%). |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $_width = '100%'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var int |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $_zoom = 5; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Marker $marker |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function add_marker( $marker ) { |
|
76
|
|
|
|
|
77
|
1 |
|
$this->_markers[] = $marker; |
|
78
|
|
|
|
|
79
|
1 |
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param Marker[] $markers |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function add_markers( $markers ) { |
|
85
|
|
|
|
|
86
|
1 |
|
$this->_markers = array_merge( $this->_markers, $markers ); |
|
87
|
|
|
|
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return array |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function center() { |
|
94
|
|
|
|
|
95
|
2 |
|
$value = null; |
|
96
|
|
|
|
|
97
|
|
|
do { |
|
98
|
2 |
|
if ( isset( $this->_center ) ) { |
|
99
|
2 |
|
$value = $this->_center; |
|
100
|
2 |
|
break; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
if ( 0 == count( $this->markers() ) ) { |
|
104
|
|
|
break; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
$marker = $this->markers()[0]; |
|
108
|
|
|
|
|
109
|
|
|
if ( $marker instanceof Marker ) { |
|
110
|
|
|
|
|
111
|
|
|
$value = $marker->position(); |
|
112
|
|
|
|
|
113
|
|
|
} |
|
114
|
|
|
} while ( false ); |
|
115
|
|
|
|
|
116
|
2 |
|
return $value; |
|
117
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
2 |
|
public function height() { |
|
124
|
|
|
|
|
125
|
2 |
|
return $this->_height; |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
2 |
|
public function html_id() { |
|
133
|
|
|
|
|
134
|
2 |
|
if ( ! isset( $this->_html_id ) ) { |
|
135
|
1 |
|
$this->_html_id = sprintf( 'map-%1$s', md5( serialize( array( $this->center(), $this->markers() ) ) ) ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
2 |
|
return $this->_html_id; |
|
139
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return Marker[] |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function markers() { |
|
146
|
|
|
|
|
147
|
1 |
|
return $this->_markers; |
|
148
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
1 |
|
public function width() { |
|
155
|
|
|
|
|
156
|
1 |
|
return $this->_width; |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return int |
|
162
|
|
|
*/ |
|
163
|
2 |
|
public function zoom() { |
|
164
|
|
|
|
|
165
|
2 |
|
return $this->_zoom; |
|
166
|
|
|
|
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param array $center |
|
171
|
|
|
*/ |
|
172
|
1 |
|
public function set_center( $center ) { |
|
173
|
|
|
|
|
174
|
1 |
|
$center = wp_parse_args( $center, array( |
|
175
|
1 |
|
'lat' => null, |
|
176
|
|
|
'lng' => null, |
|
177
|
|
|
) ); |
|
178
|
|
|
|
|
179
|
1 |
|
$this->_center = $center; |
|
180
|
|
|
|
|
181
|
1 |
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @param int $zoom |
|
185
|
|
|
*/ |
|
186
|
1 |
|
public function set_zoom( $zoom ) { |
|
187
|
|
|
|
|
188
|
1 |
|
$this->_zoom = (int)$zoom; |
|
189
|
|
|
|
|
190
|
1 |
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param string $height |
|
194
|
|
|
*/ |
|
195
|
1 |
|
public function set_height( $height ) { |
|
196
|
|
|
|
|
197
|
1 |
|
$this->_height = $height; |
|
198
|
|
|
|
|
199
|
1 |
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $html_id |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function set_html_id( $html_id ) { |
|
205
|
|
|
|
|
206
|
1 |
|
$this->_html_id = $html_id; |
|
207
|
|
|
|
|
208
|
1 |
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @param array|string $styles |
|
212
|
|
|
*/ |
|
213
|
|
|
public function set_styles( $styles ) { |
|
214
|
|
|
|
|
215
|
|
|
do { |
|
216
|
|
|
if ( is_string( $styles ) ) { |
|
217
|
|
|
$styles = json_decode( $styles, true ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if ( ! is_array( $styles ) ) { |
|
221
|
|
|
trigger_error( __( 'The style property must be an array' ) ); |
|
222
|
|
|
break; |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
$this->_styles = $styles; |
|
226
|
|
|
} while ( false ); |
|
227
|
|
|
|
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @return array |
|
232
|
|
|
* |
|
233
|
|
|
* @todo Refactor to make_params |
|
234
|
|
|
*/ |
|
235
|
1 |
|
public function make_args() { |
|
236
|
|
|
|
|
237
|
|
|
return array( |
|
238
|
1 |
|
'center' => $this->center(), |
|
239
|
1 |
|
'styles' => $this->styles(), |
|
240
|
1 |
|
'zoom' => (int)$this->zoom(), |
|
241
|
|
|
); |
|
242
|
|
|
|
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
} |
|
246
|
|
|
|