PluginProbe
CodePeople Post Map for Google Maps / trunk
CodePeople Post Map for Google Maps vtrunk
1.3.0 1.2.9 1.2.8 1.2.7 1.2.6 trunk 1.0.44 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5
codepeople-post-map / js / cpm.js

cpm.js in CodePeople Post Map for Google Maps trunk, at js/cpm.js

439 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * CodePeople Post Map
3 * Version: 1.0.1
4 * Author: CodePeople
5 * Plugin URI: http://wordpress.dwbooster.com
6 */
7
8 var CodePeoplePostMapPublicCounter = 0;
9 function CodePeoplePostMapPublic()
10 {
11 CodePeoplePostMapPublicCounter++;
12 if( typeof jQuery == 'undefined' )
13 {
14 if( CodePeoplePostMapPublicCounter <= 6 ) setTimeout( function(){ CodePeoplePostMapPublic() }, 1000 );
15 return;
16 }
17 jQuery(function( $ ){
18 // Create a class with CodePeople Post Map functionalities and attributes
19 $.CPM = function(id, config, cloud_id){
20 this.data = $.extend(true, {}, this.defaults, config);
21 this.id = id;
22 this.cloud_id = cloud_id;
23 this.markers = [];
24 this.uniqueMarkers = [];
25 if( typeof this.data[ 'center' ] == 'object' && typeof this.data.center.length != 'undefined' && this.data.center.length == 2 )
26 {
27 this.data.center = new google.maps.LatLng( this.data.center[ 0 ], this.data.center[ 1 ] );
28 }
29 else
30 {
31 this.data.center = null;
32 }
33 };
34
35 $.CPM.prototype = {
36 defaults : {
37 markers : [],
38 marker_title : 'title',
39 zoom : 10,
40 dynamic_zoom : false,
41 drag_map : true,
42 type : 'ROADMAP',
43 mousewheel : true,
44 scalecontrol : true,
45 zoompancontrol : true,
46 fullscreencontrol: true,
47 typecontrol : true,
48 streetviewcontrol : true,
49 trafficlayer : false,
50 show_window : true,
51 show_default : true,
52 display : 'map',
53 highlight : true,
54 highlight_class : 'cpm_highlight'
55 },
56
57 // private methods to complete every steps in map generation
58 _correct : function( v ){
59 v = (new String(v)).replace(/[^\d\+\-\.]/g, '');
60 return v*1;
61 },
62
63 _empty : function (v){
64 return (!v || /^\s*$/.test(v));
65 },
66
67 _get_latlng : function(i){
68 var me = this,
69 g = new google.maps.Geocoder(),
70 m = me.data.markers,
71 a = m[i]['address'];
72
73 g.geocode({address:a}, function(result, status){
74 me.counter--;
75 if(status && status == "OK"){
76 m[i]['latlng'] = new google.maps.LatLng(me._correct(result[0]['geometry']['location'].lat()), me._correct(result[0]['geometry']['location'].lng()));
77 }else{
78 m[i]['invalid'] = true;
79 }
80
81 // All points have been checked now is possible to load the map
82 if(me.counter == 0){
83 me._load_map();
84 }
85 });
86 },
87 _str_transform : function( t ){
88 return t.replace( /&lt;/g, '<')
89 .replace( /&gt;/g, '>')
90 .replace( /&amp;/g, '&')
91 .replace( /&quot;/g, '"')
92 .replace(/\\'/g, "'")
93 .replace(/\\"/g, '"' );
94 },
95 _unique : function( l ){
96 var rtn = [];
97 this.uniqueMarkers = [];
98 for( var i = 0, h = l.length; i < h; i++ )
99 {
100 let index = JSON.stringify(l[ i ].position);
101 if( typeof this.uniqueMarkers[ index ] == 'undefined' )
102 {
103 this.uniqueMarkers[ index ] = [];
104 rtn.push( l[ i ] );
105 }
106 else
107 {
108 l[ i ].visible = false;
109 }
110 this.uniqueMarkers[ index ].push( l[ i ] );
111 }
112
113 return rtn;
114 },
115 _load_map : function(){
116
117 var me = this,
118 m = me.data.markers,
119 h = m.length,
120 c = 0,
121 v = 0; // Number of valid points
122
123 while(c < h && m[c]['invalid']) c++;
124
125 if(c < h){
126 me.map = new google.maps.Map($('#'+me.id)[0], {
127 zoom: me.data.zoom,
128 center: ( typeof me.data.center != 'undefined' && me.data.center != null ) ? me.data.center : m[c].latlng,
129 mapTypeId: google.maps.MapTypeId[me.data.type],
130 gestureHandling: ((me.data.mousewheel) ? 'cooperative' : 'none'),
131 draggable: me.data.drag_map,
132
133 // Show / Hide controls
134 scaleControl: me.data.scalecontrol,
135 zoomControl: me.data.zoompancontrol,
136 mapTypeControl: me.data.typecontrol,
137 streetViewControl: me.data.streetviewcontrol,
138 fullscreenControl: me.data.fullscreencontrol,
139 backgroundColor: 'none',
140 mapId: me.cloud_id
141 });
142
143 var map = me.map,
144 bounds = new google.maps.LatLngBounds(),
145 default_point = -1;
146
147 if(me.data.trafficlayer)
148 {
149 var trafficLayer = new google.maps.TrafficLayer();
150 trafficLayer.setMap(me.map);
151 }
152
153 if( me.data.show_default ){
154 google.maps.event.addListenerOnce(map, 'idle', function(){
155 setTimeout(function(){
156 if( me.markers.length ) google.maps.event.trigger( ( ( default_point < 0 ) ? me.markers[ 0 ] : me.markers[ default_point ] ), 'click' );
157 }, 1000);
158 });
159 }
160 me.infowindow = new google.maps.InfoWindow();
161 var title = null;
162 for (var i = c; i < h; i++){
163 if(!m[i]['invalid']){
164 if( typeof m[ i ][ 'default' ] != 'undefined' && m[ i ][ 'default' ] )
165 {
166 default_point = me.markers.length;
167 }
168
169 bounds.extend(m[i].latlng);
170
171 if(/title/i.test(me['data']['marker_title']))
172 title = me._str_transform($( m[i].info ).find('.title').text());
173 else if(/address/i.test(me['data']['marker_title']))
174 title = (m[i].address) ? me._str_transform(m[i].address) : '';
175
176 var icon_img = document.createElement("img");
177 icon_img.src = m[i].icon.replace(/^http:/i, '');
178 var marker = new google.maps.marker.AdvancedMarkerElement({
179 position: m[i].latlng,
180 map: map,
181 content: icon_img,
182 title:title
183 });
184
185 marker.id = i;
186 me.markers.push(marker);
187 google.maps.event.addListener(marker, 'click', function(){ me.open_infowindow(this); });
188 google.maps.event.addListener(marker, 'mouseover', function(){ me.set_highlight(this); });
189 google.maps.event.addListener(marker, 'mouseout', function(){ me.unset_highlight(this); });
190 }
191 }
192 me._unique( me.markers );
193 if (h > 1 && me.data.dynamic_zoom) {
194 setTimeout( ( function( m, b ){ return function(){ m.fitBounds( b ); }; } )( map, bounds ), 500 );
195 }
196 else if (h == 1 || !me.data.dynamic_zoom) {
197 if( typeof me.data.center != 'undefined' && me.data.center != null )
198 {
199 map.setCenter( me.data.center );
200 }
201 else
202 {
203 if( default_point != -1 )
204 {
205 map.setCenter( me.markers[ default_point ].getPosition() );
206 }
207 else
208 {
209 map.setCenter(bounds.getCenter());
210 }
211 }
212 map.setZoom(me.data.zoom);
213 }
214 }
215 else
216 {
217 $('#'+me.id).hide();
218 }
219 },
220
221 // public methods
222 set_map: function(){
223 var me = this;
224 if(me.data.markers.length){
225 var m = me.data.markers,
226 h = m.length,
227 z = 0;
228
229 me.counter = h; // Counter is used to know the momment where all latitudes or longitudes were calculated
230
231 function _get_latlng()
232 {
233 var tmp = 0;
234 for(var i=z; i < h; i++){
235 z++;
236 if(typeof m[i] == 'undefined')
237 {
238 m[i] = {'invalid':true};
239 me.counter--;
240 continue;
241 }else if((me._empty(m[i].lat) || me._empty(m[i].lng)) && !me._empty(m[i].address)){
242 me._get_latlng(i);
243 tmp++;
244 if(tmp == 20)
245 {
246 setTimeout(_get_latlng, 1500);
247 }
248 }else if(me._empty(m[i].lat) && me._empty(m[i].lng)){
249 // The address is not present so the point may be removed from the list
250 m[i]['invalid'] = true;
251 me.counter--;
252 }else{
253 m[i]['latlng'] = new google.maps.LatLng(me._correct(m[i].lat), me._correct(m[i].lng));
254 me.counter--;
255 }
256 }
257 };
258
259 _get_latlng();
260
261 // All points have been checked now is possible to load the map
262 if(me.counter == 0){
263 me._load_map();
264 }
265 }
266 },
267
268 // Open the marker bubble
269 open_infowindow : function(m){
270 var me = this,
271 info = '',
272 unique = me.uniqueMarkers[ JSON.stringify(m.position) ];
273
274 if ( !me.data.show_window ) return;
275
276 // Get the information of all concident points
277 for( var i = 0, h = unique.length; i < h; i++ )
278 {
279 info += ( i < h-1 ) ? $( '<div></div>').html( me.data.markers[ unique[ i ].id ].info ).find( '.cpm-infowindow-additional' ).remove().end().html() : me.data.markers[ unique[ i ].id ].info;
280 }
281
282 var c = me._str_transform( info ),
283 img = $( c ).find( 'img' );
284 c = c.replace( '%additional%', '' );
285 if( img.length )
286 {
287 var count = img.length;
288 img.each( function(){
289 $( '<img src="'+$(this).attr( 'src' ) +'">' ).on('load', (function( c, m ){
290 return function(){
291 count--;
292 if( count == 0 )
293 {
294 me.infowindow.setContent( c );
295 me.infowindow.open( me.map, m );
296 }
297 };
298 } )( c, m ) );
299 } );
300 }
301 else
302 {
303 c += '<style>.cpm-infowindow{ min-height:auto !important; } </style>';
304 me.infowindow.setContent( c );
305 me.infowindow.open( me.map, m );
306 }
307 },
308
309 // Set the highlight class to the post with ID m['post']
310 set_highlight : function(m){
311 if(this.data.highlight){
312 var id = this.data.markers[m.id]['post'];
313 $('.post-'+id).addClass(this.data.highlight_class);
314 }
315 },
316
317 // Remove the highlight class from the post with ID m['post_id']
318 unset_highlight : function(m){
319 if(this.data.highlight){
320 var id = this.data.markers[m.id]['post'];
321 $('.post-'+id).removeClass(this.data.highlight_class);
322 }
323 }
324 };
325 // End CPM class definition
326
327 // Callback function to be called after loading the maps api
328 function initialize( e )
329 {
330 var map_container = $( e ),
331 map_id = map_container.attr('id'),
332 cloud_id = map_container.attr('data-mapid');
333
334 if ( typeof cloud_id == 'undefined' || '' == cloud_id ) cloud_id = map_id;
335
336 if( map_container.parent().is( ':hidden' ) )
337 {
338 setTimeout( function(){ initialize( e ); }, 500 );
339 return;
340 }
341
342 if(cpm_global && cpm_global[map_id] && cpm_global[map_id]['markers'].length){
343 // The maps data are defined
344 var cpm = new $.CPM(map_id, cpm_global[map_id], cloud_id );
345
346 // Display map
347 if(cpm_global[map_id]['display'] == 'map'){
348 map_container.show();
349 cpm.set_map();
350 }else{
351 // Insert a icon to display map
352 var map_icon = $('<div class="cpm-mapicon"></div>');
353 map_icon.on('click',function(){
354 if(map_container.is( ':visible' ))
355 {
356 map_container.hide();
357 }
358 else
359 {
360 map_container.show();
361 cpm.set_map();
362 }
363 });
364 map_icon.insertBefore(map_container);
365 }
366 }
367 };
368
369 window['cpm_init'] = function(){
370 $('.cpm-map').each(function(){
371 if( $( this ).parent().is( ':hidden' ) )
372 {
373 setTimeout(
374 ( function ( e )
375 {
376 return function(){ initialize( e ); };
377 } )( this ),
378 500
379 );
380 }
381 else
382 {
383 initialize( this );
384 }
385 });
386 };
387
388 var map = $('.cpm-map');
389 if(map.length){
390 if(typeof google == 'undefined' || google['maps'] == null){
391 // Create the script tag and load the maps api
392 var script=document.createElement('script');
393 script.type = "text/javascript";
394 script.src=(( typeof window.location.protocol != 'undefined' ) ? window.location.protocol : 'http:' )+'//maps.google.com/maps/api/js?loading=async&'+((typeof cpm_api_key != 'undefined' && cpm_api_key != '')? 'key='+cpm_api_key+'&' :'')+'callback=cpm_init'+((typeof cpm_language != 'undefined' && cpm_language.lng) ? '&language='+cpm_language.lng: '')+'&libraries=marker';
395 document.body.appendChild(script);
396 }else{
397 cpm_init();
398 }
399 }
400 });
401 }
402
403 window.addEventListener("load", function(event){
404 function CodePeoplePostMapRun()
405 {
406 try {
407 if(
408 ! ( 'BorlabsCookie' in window ) ||
409 (
410 'checkCookieConsent' in window.BorlabsCookie &&
411 (
412 window.BorlabsCookie.checkCookieConsent( 'googlemaps' ) ||
413 window.BorlabsCookie.checkCookieConsent( 'maps' )
414 )
415 ) ||
416 (
417 'Consents' in window.BorlabsCookie &&
418 (
419 window.BorlabsCookie.Consents.hasConsent( 'googlemaps' ) ||
420 window.BorlabsCookie.Consents.hasConsent( 'maps' )
421 )
422 )
423 ) setTimeout(CodePeoplePostMapPublic,300);
424 } catch ( err ) {
425 setTimeout(CodePeoplePostMapPublic,300);
426 }
427 }
428
429 CodePeoplePostMapRun();
430 document.addEventListener('borlabs-cookie-code-unblocked-after-consent', function(){CodePeoplePostMapRun();});
431 document.addEventListener('borlabs-cookie-consent-saved', function(){CodePeoplePostMapRun();});
432
433 });
434
435 window.addEventListener('message', (e) => {
436 if (e.data && e.data.type === 'cpm:reload') {
437 window.location.reload();
438 }
439 });