1
+ <?php
2
+ /**
3
+ * Google Maps for ExpressionEngine
4
+ * Author: Justin Kimbrell (SaucePan Creative)
5
+ * Build: 2.0 - April 16, 2011
6
+ * Copyright 2011 - All rights reserved
7
+ * http://objectivehtml.com, http://inthesaucepan.com
8
+ */
9
+
10
+ $ plugin_info = array (
11
+ 'pi_name ' => 'Google Map Channel Plugin ' ,
12
+ 'pi_version ' => '2.0 ' ,
13
+ 'pi_author ' => 'Justin Kimbrell ' ,
14
+ 'pi_author_url ' => 'http://objectivehtml.com/plugins/google-map-channel-plugin ' ,
15
+ 'pi_description ' => 'Creates static and dynamic maps from content channels. ' ,
16
+ 'pi_usage ' => Gmap::usage ()
17
+ );
18
+
19
+
20
+ Class Gmap {
21
+
22
+ /* Channel Parameters */
23
+
24
+ var $ params = array (
25
+ 'author_id ' , 'backspace ' , 'cache ' , 'refresh ' , 'cat_limit ' , 'category ' ,
26
+ 'category_group ' , 'isable ' ,'channel ' , 'display_by ' , 'dynamic ' , 'dynamic_start ' ,
27
+ 'entry_id ' , 'entry_id_from ' , 'entry_id_to ' , 'fixed_order ' , 'group_id ' , 'limit ' ,
28
+ 'month_limit ' , 'offset ' , 'orderby ' , 'paginate ' , 'paginate_base ' , 'paginate_type ' , 'related_categories_mode ' , 'relaxed_categories ' , 'require_entry ' , 'show_current_week ' ,
29
+ 'show_expired ' , 'show_future_entries ' , 'show_pages ' , 'sort ' , 'start_day ' , 'start_on ' ,
30
+ 'status ' , 'stop_before ' , 'sticky ' , 'track_views ' , 'uncategorized_entries ' , 'url_title ' ,
31
+ 'username ' , 'week_sort ' , 'year ' , 'month ' , 'day '
32
+ );
33
+
34
+ var $ options = array ('backgroundColor ' , 'disableDefaultUI ' , 'disableDoubleClickZoom ' , 'draggable ' , 'draggableCursor ' , 'heading ' , 'keyboardShortcuts ' , 'mapTypeControl ' , 'mapTypeControlOptions ' , 'mapTypeId ' , 'maxZoom ' , 'minZoom ' , 'noClear ' , 'overviewMapControl ' , 'overviewMapControlOptions ' , 'panControl ' , 'panControlOptions ' , 'rotateControl ' , 'rotateControlOptions ' , 'scaleControl ' , 'scaleControlOptions ' , 'scrollwheel ' , 'streetView ' , 'streetViewControl ' , 'streetViewControlOptions ' , 'tilt ' , 'title ' , 'zoomControl ' , 'zoomControlOptions ' , 'zoom ' );
35
+
36
+ var $ map = array ('center ' , 'address_field ' , 'city_field ' , 'state_field ' , 'zipcode_field ' ,
37
+ 'country_field ' , 'channel ' , 'address ' , 'latitude ' , 'longitude ' , 'map_type ' , 'id ' , 'class ' , 'style ' );
38
+
39
+
40
+ /* Map parameters */
41
+
42
+ var $ address ;
43
+
44
+ private function _fetch_params ()
45
+ {
46
+ foreach ($ this ->map as $ field )
47
+ $ this ->map [$ field ] = $ this ->EE ->TMPL ->fetch_param ($ field );
48
+
49
+ foreach ($ this ->options as $ field )
50
+ $ options [$ field ] = $ this ->EE ->TMPL ->fetch_param ($ field );
51
+
52
+ $ this ->options = $ options ;
53
+
54
+ $ address = $ this ->map ['address_field ' ] ? '{ ' .$ this ->map ['address_field ' ].'} ' : '' ;
55
+ $ address .= $ this ->map ['city_field ' ] ? '{ ' .$ this ->map ['city_field ' ].'} ' : '' ;
56
+ $ address .= $ this ->map ['state_field ' ] ? '{ ' .$ this ->map ['state_field ' ].'} ' : '' ;
57
+ $ address .= $ this ->map ['zipcode_field ' ] ? '{ ' .$ this ->map ['zipcode_field ' ].'} ' : '' ;
58
+ $ address .= $ this ->map ['country_field ' ] ? '{ ' .$ this ->map ['country_field ' ].'} ' : '' ;
59
+
60
+ $ this ->address = $ address ;
61
+
62
+ /* Sets the default if any param is not set */
63
+
64
+ //$this->map['zoom'] = !$this->map['zoom'] ? 12 : $this->map['zoom'];
65
+ $ this ->map ['latitude ' ] = !$ this ->map ['latitude ' ] ? 0 : $ this ->map ['latitude ' ];
66
+ $ this ->map ['longitude ' ] = !$ this ->map ['longitude ' ] ? 0 : $ this ->map ['longitude ' ];
67
+ $ this ->map ['id ' ] = !$ this ->map ['id ' ] ? FALSE : $ this ->map ['id ' ];
68
+ $ this ->map ['class ' ] = !$ this ->map ['class ' ] ? '' : $ this ->map ['class ' ];
69
+ $ this ->map ['style ' ] = !$ this ->map ['style ' ] ? '' : $ this ->map ['style ' ];
70
+
71
+ if ($ this ->map ['map_type ' ])
72
+ $ map_type = $ this ->map ['map_type ' ];
73
+ else
74
+ $ map_type = $ this ->options ['mapTypeId ' ];
75
+
76
+ $ map_type = !$ map_type || !isset ($ map_type ) ? 'google.maps.MapTypeId.ROADMAP ' : 'google.maps.MapTypeId. ' .strtoupper ($ map_type );
77
+
78
+ $ this ->options ['mapTypeId ' ] = $ map_type ;
79
+
80
+ return $ this ->map ;
81
+ }
82
+
83
+ private function _init_gmap ()
84
+ {
85
+
86
+ $ string = '
87
+ <div id=" ' .$ this ->map ['id ' ].'" class=" ' .$ this ->map ['class ' ].'" style=" ' .$ this ->map ['style ' ].'"></div>
88
+
89
+ <script type="text/javascript">
90
+
91
+ var geocoder = new google.maps.Geocoder(); ' ;
92
+
93
+ if ($ this ->map ['address ' ])
94
+ {
95
+ $ this ->address = $ this ->map ['address ' ];
96
+ $ exp_open_tag = '' ;
97
+ $ exp_close_tag = '' ;
98
+ }
99
+ else
100
+ {
101
+ $ address = $ this ->map ['address_field ' ] ? '{if ' .$ this ->map ['address_field ' ].'}{ ' .$ this ->map ['address_field ' ].'}{/if} ' : '' ;
102
+ $ address .= $ this ->map ['city_field ' ] ? '{if ' .$ this ->map ['city_field ' ].'}{ ' .$ this ->map ['city_field ' ].'},{/if} ' : '' ;
103
+ $ address .= $ this ->map ['state_field ' ] ? '{if ' .$ this ->map ['state_field ' ].'}{ ' .$ this ->map ['state_field ' ].'}{/if} ' : '' ;
104
+ $ address .= $ this ->map ['zipcode_field ' ] ? '{if ' .$ this ->map ['zipcode_field ' ].'}{ ' .$ this ->map ['zipcode_field ' ].'}{/if} ' : '' ;
105
+ $ address .= $ this ->map ['country_field ' ] ? '{if ' .$ this ->map ['country_field ' ].'}{ ' .$ this ->map ['country_field ' ].'}{/if} ' : '' ;
106
+
107
+
108
+ $ exp_open_tag = '{exp:channel:entries ' ;
109
+
110
+ foreach ($ this ->params as $ param )
111
+ if ($ this ->EE ->TMPL ->fetch_param ($ param ))
112
+ $ exp_open_tag .= $ param .'=" ' .$ this ->EE ->TMPL ->fetch_param ($ param ).'" ' ;
113
+
114
+ $ exp_open_tag .= '} ' ;
115
+
116
+ $ exp_close_tag = '{/exp:channel:entries} ' ;
117
+ }
118
+
119
+ if ($ this ->map ['center ' ])
120
+ {
121
+ $ string .= '
122
+ geocoder.geocode({ address: " ' .$ this ->map ['center ' ].'"}, function(results, status) {
123
+ if (status == google.maps.GeocoderStatus.OK) {
124
+ var location = results[0].geometry.location;
125
+ ' ;
126
+
127
+ if (!$ this ->options ['zoom ' ])
128
+ $ string .='
129
+ ' .$ this ->map ['id ' ].'.setZoom(10);
130
+ ' ;
131
+
132
+ $ string .= '
133
+ ' .$ this ->map ['id ' ].'.setCenter(location);
134
+ }
135
+ });
136
+ ' ;
137
+ }
138
+ $ string .= '
139
+ var latlng = new google.maps.LatLng( ' .$ this ->map ['latitude ' ].', ' .$ this ->map ['longitude ' ].');
140
+
141
+ var myOptions = {
142
+ center: latlng,
143
+ ' ;
144
+ $ tmp_string = '' ;
145
+
146
+ foreach ($ this ->options as $ index => $ value )
147
+ {
148
+ if ($ value )
149
+ {
150
+ $ tmp_string .= $ index .': ' .$ value .', ' ;
151
+ }
152
+ };
153
+
154
+ $ tmp_string = substr ($ tmp_string , 0 , strlen ($ tmp_string ) - 1 );
155
+
156
+ $ string .= $ tmp_string .'
157
+ }
158
+
159
+ ' .$ this ->map ['id ' ].' = new google.maps.Map(document.getElementById(" ' .$ this ->map ['id ' ].'"), myOptions);
160
+
161
+ var ' .$ this ->map ['id ' ].'_bounds = new google.maps.LatLngBounds();
162
+ ' ;
163
+
164
+ $ string .= $ exp_open_tag .'
165
+ var address = " ' .trim ($ this ->address ).'";
166
+
167
+ geocoder.geocode({ address: address}, function(results, status) {
168
+ if (status == google.maps.GeocoderStatus.OK) {
169
+ var location = results[0].geometry.location;
170
+ ' ;
171
+
172
+ if (!$ this ->map ['center ' ] && !$ this ->options ['zoom ' ])
173
+ {
174
+ $ string .= '
175
+ ' .$ this ->map ['id ' ].'_bounds.extend(location);
176
+ ' .$ this ->map ['id ' ].'.fitBounds( ' .$ this ->map ['id ' ].'_bounds);
177
+ ' ;
178
+ }
179
+
180
+ if ($ this ->options ['zoom ' ] && !$ this ->map ['center ' ]) {
181
+
182
+ $ string .= '
183
+ ' .$ this ->map ['id ' ].'.setCenter(location);
184
+ ' ;
185
+ }
186
+
187
+ $ string .= '
188
+ var marker = new google.maps.Marker({
189
+ map: ' .$ this ->map ['id ' ].',
190
+ position: location,
191
+ title: " ' .$ this ->address .'"
192
+ });
193
+ ' ;
194
+
195
+ if ($ this ->EE ->TMPL ->tagdata != '' )
196
+ {
197
+ $ string .= '
198
+ var html = " ' .preg_replace ("/[ \n\r\t]/ " ,"" ,$ this ->EE ->TMPL ->tagdata ).'";
199
+ ' ;
200
+
201
+ $ string .='
202
+ var infowindow = new google.maps.InfoWindow({
203
+ content: html
204
+ });
205
+
206
+ google.maps.event.addListener(marker, \'click \', function() {
207
+ infowindow.close();
208
+ infowindow.open( ' .$ this ->map ['id ' ].', marker);
209
+ }); ' ;
210
+ }
211
+
212
+ $ string .= '
213
+ }
214
+ });
215
+ ' .$ exp_close_tag .'
216
+ </script> ' ;
217
+
218
+ return $ string ;
219
+
220
+ }
221
+
222
+ function Gmap ()
223
+ {
224
+ $ this ->EE =& get_instance ();
225
+
226
+ $ params = $ this ->_fetch_params ();
227
+
228
+ if (!$ this ->map ['address ' ] && !$ this ->map ['channel ' ])
229
+ show_error ('You must define an address or a channel. ' );
230
+
231
+ if (!$ this ->map ['id ' ])
232
+ show_error ('You must assign a unique <code>id</code> to every map. This <code>id</code> sould only contain alphabetical and numerical characters with the exception of an underscore. ' );
233
+
234
+ $ this ->return_data = $ this ->_init_gmap ();
235
+ }
236
+
237
+ function usage ()
238
+ {
239
+ ob_start ();
240
+ ?>
241
+
242
+ For a complete tutorial, go to http://objectivehtml.com/examples/gmap
243
+
244
+ And the plugin reference, http://objectivehtml.com/plugins/google-maps-for-expressionengine
245
+
246
+
247
+ <?php
248
+ $ buffer = ob_get_contents ();
249
+
250
+ ob_end_clean ();
251
+
252
+ return $ buffer ;
253
+ }
254
+ }
0 commit comments