source: extensions/GMaps/js/gmapsMarkup.js @ 15311

Last change on this file since 15311 was 12204, checked in by grum, 13 years ago

fix bugs
bug:2346 - Preview / next arrow do not work with [gmaps] tag in description
bug:2063 - Incompatibility with slideshow mode

  • Property svn:executable set to *
File size: 12.7 KB
RevLine 
[7500]1/**
2 * -----------------------------------------------------------------------------
3 * file: gmapsMarkup.js
[12204]4 * file version: 1.1.2
5 * date: 2011-09-23
[7500]6 */
7
[12204]8
9 var gmapsCM=null,
10     gmapsMM=null;
11
12function markupMaps()
13{
14  var markerImgProp = [
[7616]15      null,
16      { w:32, h:32, x:15, y:31 }, // s01
17      { w:32, h:32, x:15, y:31 }, // s02
18      { w:32, h:32, x:10, y:31 }, // s03
[10551]19      { w:30, h:40, x:4, y:39 } // s04
[7616]20    ];
[7500]21
[7616]22  /**
23   * create the map
24   *
25   * @param Object properties : map properties
26   * @param Integer gmapsIndex : index in the map list
27   */
28  this.createMap = function (properties, gmapsIndex)
29  {
30    var bounds = new google.maps.LatLngBounds(
31          new google.maps.LatLng(properties.mapBounds.south, properties.mapBounds.west),
32          new google.maps.LatLng(properties.mapBounds.north, properties.mapBounds.east)
33        ),
34        map = new google.maps.Map($("#"+properties.id).get(0),
35          {
36            backgroundColor:'#ffffff',
37            mapTypeId: properties.mapType,
38            zoom: properties.zoomLevel,
39            center: bounds.getCenter(),
40            navigationControl: (properties.navigationControl==-1)?false:true,
41            scrollwheel: (properties.navigationControl==-1)?false:true,
42            scaleControl: (properties.scaleControl=='n')?false:true,
43            streetViewControl: (properties.streetViewControl=='n')?false:true,
44            mapTypeControl:(properties.mapTypeControl==-1)?false:true,
45            mapTypeControlOptions:
46              {
47                style:properties.mapTypeControl
48              },
[10551]49            markerTitle:''
[7616]50          }
51        );
52
53    if(properties.kmlFileUrl!='')
54    {
55      kmlFile = new google.maps.KmlLayer(properties.kmlFileUrl,
[7500]56        {
[7616]57          preserveViewport:true
[7500]58        }
59      );
[7616]60      kmlFile.setMap(map);
61    }
62    else
63    {
64      kmlFile=null;
65    }
[7500]66
[7616]67    re=/^mS(\d\d)_.*/i;
68    iM=re.exec(properties.markerImg);
69    if(iM!=null) iM=new Number(iM[1]);
[7500]70
[7616]71    if(iM!=null)
72    {
73      map.markerImg = new google.maps.MarkerImage('plugins/GMaps/img/'+properties.markerImg,
74          new google.maps.Size(markerImgProp[iM].w, markerImgProp[iM].h),
75          new google.maps.Point(0,0),
76          new google.maps.Point(markerImgProp[iM].x, markerImgProp[iM].y));
77    }
78    else
79    {
80      map.markerImg = null;
81    }
[7500]82
[7616]83    map.bounds=bounds;
84    map.kmlFile=kmlFile;
85    map.markers=new Array();
86    map.gmapsIndex=gmapsIndex;
87    map.width=properties.width;
88    map.height=properties.height;
89    map.callId=0;
90    map.allowBubble=properties.allowBubble;
91    map.initialized=false;
92    properties.gMap=map;
[7500]93
94
[7616]95    if(properties.geolocated)
96    {
97      if(properties.fitToBounds)
98      {
[12204]99        this.fitToBounds(map.bounds, gmapsIndex);
[7616]100      }
101      else
102      {
103        map.setCenter(map.bounds.getCenter());
104      }
105    }
[7500]106
[7616]107    if(properties.markerVisible)
[7500]108    {
[12204]109      this.loadMarkers(map);
[7500]110    }
111    else
112    {
[12204]113      this.initializeMapViewport(gmapsIndex);
[7500]114    }
[7616]115  };
[7500]116
[7616]117  /**
118   * load markers from the server
119   *
120   * @param Map map : the map
121   */
[12204]122  this.loadMarkers = function(map)
[7500]123  {
[12204]124    var __this=this;
125
[7616]126    if(map.getBounds()==null) return(false);
127    map.callId++;
[7500]128
[7616]129    datas={
130      requestId:gmapsMarkup.requestId,
131      callId:map.callId,
132      bounds:{
133          north:map.getBounds().getNorthEast().lat(),
134          east:map.getBounds().getNorthEast().lng(),
135          south:map.getBounds().getSouthWest().lat(),
136          west:map.getBounds().getSouthWest().lng()
137        },
138      width:map.width,
139      height:map.height,
140      distanceTreshold:20,
[10551]141      loadIndex:map.gmapsIndex
[7616]142    };
[7500]143
[7616]144    $.ajax(
145      {
146        type: "POST",
147        url: "plugins/GMaps/gmaps_ajax.php",
148        async: true,
149        data: { ajaxfct:"public.maps.getMarkers", datas:datas },
150        success:
151          function(msg)
[7500]152          {
[7616]153            tmp=$.parseJSON(msg);
154            if(gmapsMarkup.maps[tmp.loadIndex].gMap.callId==tmp.callId)
155            {
156              tmp.markers.sort(compareMarkers);
[12204]157              __this.applyMarkers(gmapsMarkup.maps[tmp.loadIndex].gMap, tmp.markers);
158              __this.initializeMapViewport(tmp.loadIndex);
[7616]159            }
[7500]160          }
[7616]161      }
162    );
163  };
[7500]164
[7616]165  /**
166   * apply markers to map
167   *
168   * @param Map map : the google map object
169   * @param Array markers : array of markers properties
170   *                        each marker is an object with properties :
171   *                          latitude, longitude, nbImg
[7500]172   */
[12204]173  this.applyMarkers = function(map, markers)
[7500]174  {
[7616]175    if(map==null) return(false);
176
177    /*
178     * deleting markers from the map only if they are not in the new list
179     */
180    if(map.markers.length>0)
[7500]181    {
[7616]182      var i=0;
183      while(i<map.markers.length)
[7500]184      {
[12204]185        newListIndex=this.markerInList(map.markers[i].uId, markers);
[7616]186        if(newListIndex==-1)
187        {
188          map.markers[i].marker.setMap(null);
189          map.markers.splice(i, 1);
190        }
191        else
192        {
193          markers.splice(newListIndex,1);
194          i++;
195        }
[7500]196      }
197    }
198
[7616]199    /*
200     * add new markers on the map
201     */
202    for(var i=0;i<markers.length;i++)
203    {
[12204]204      var __this=this,
205          marker = new google.maps.Marker(
206            {
207              position:new google.maps.LatLng(markers[i].lat, markers[i].lng),
208              map: map,
209              title:markers[i].nbImgTxt
210            }
211          );
[7500]212
[7616]213      if(map.markerImg!=null) marker.setIcon(map.markerImg);
[7500]214
[7616]215      marker.info=markers[i];
216      marker.info.displayed=0;
[7500]217
[7616]218      map.markers.push(
219        {
220          marker:marker,
221          uId:markers[i].uId
222        }
223      );
224
225      if(map.allowBubble)
[7500]226      {
[7616]227        google.maps.event.addListener(
228          marker,
229          'click',
230          function ()
231          {
[12204]232            __this.displayWindowInfo(this);
[7616]233          }
234        );
[7500]235      }
236
[7616]237      for(var index=0;index<marker.info.imgTn.length;index++)
238      {
239        switch(marker.info.imgTn[index].charAt(0))
[7500]240        {
[7616]241          case 'G':
242            marker.info.imgTn[index]='./galleries/'+marker.info.imgTn[index].substr(1);
243            break;
244          case 'U':
245            marker.info.imgTn[index]='./upload/'+marker.info.imgTn[index].substr(1);
246            break;
[7500]247        }
[7616]248      }
[7500]249    }
[7616]250  };
[7500]251
[7616]252  var compareMarkers=function(m1,m2)
253  {
254    if(m1.uId<m2.uId)
[7500]255    {
[7616]256      return(-1);
[7500]257    }
[7616]258    else if(m1.uId<m2.uId)
259    {
260      return(1);
261    }
262    return(0);
263  };
[7500]264
[12204]265  this.markerInList=function(uniqueId, markerList)
[7500]266  {
[7616]267    for(var i=0;i<markerList.length;i++)
268    {
269      if(markerList[i].uId==uniqueId) return(i)
270    }
[7500]271    return(-1);
[7616]272  };
[7500]273
[7616]274  /**
275   * display the infowindow
276   *
277   * @param Marker marker : the marker
278   */
[12204]279  this.displayWindowInfo=function(marker)
[7500]280  {
[12204]281    if(gmapsCM && gmapsCM instanceof categoryMaps) gmapsCM.closePictureInfo();
282
[7616]283    gmapsMarkup.currentInfo=marker.info;
284    gmapsMarkup.infoWindow.close();
285    gmapsMarkup.infoWindow.setContent($('#iGMapsInfoWindowContent').clone().each(renameId).get(0));
286    gmapsMarkup.infoWindow.open(marker.map, marker);
[12204]287    $('#ciWALeft, #ciWARight').unbind('click');
288    $('#ciWARight').bind('click', function(event) { __this.displayPictureNext(); } );
289    $('#ciWALeft').bind('click', function(event) { __this.displayPicturePrev(); } );
290    this.displayPictureInfo(gmapsMarkup.currentInfo.displayed);
[7616]291  };
292  var renameId=function(i, e)
293  {
294    if(e.id!='') e.id='c'+e.id;
295    $(e).children().each(renameId);
296  };
[7500]297
298
[12204]299  this.closePictureInfo = function ()
300  {
301    gmapsMarkup.currentInfo=null;
302    gmapsMarkup.infoWindow.setContent('');
303    gmapsMarkup.infoWindow.close();
304    $('#ciWALeft, #ciWARight').unbind('click');
305  }
306
[7616]307  /**
308   *
309   */
[12204]310  this.displayPictureInfo=function(index)
[7500]311  {
[12204]312    var __this=this;
313
314    $('#ciWALeft,#ciWARight').unbind('click');
315    $('#ciWARight').bind('click',
316      function(event)
317      {
318        __this.displayPictureNext();
319      }
320    );
321    $('#ciWALeft').bind('click',
322      function(event)
323      {
324        __this.displayPicturePrev();
325      }
326    );
327
[7616]328    gmapsMarkup.currentInfo.displayed=index;
329    if(gmapsMarkup.currentInfo.imgName[index]=='')
330    {
331      $('#ciGMIWC_title').html('&nbsp;');
332    }
333    else
334    {
335      $('#ciGMIWC_title').html(gmapsMarkup.currentInfo.imgName[index]);
336    }
337    $('#ciGMIWC_img').attr('src', gmapsMarkup.currentInfo.imgTn[index]);
[7500]338
[7616]339    $('#ciGMIWC_img').unbind();
340    if(gmapsMarkup.currentInfo.imgCatsUrl[index].length==1)
341    {
342      $('#ciGMIWC_img').bind('click',
343        function ()
344        {
345          window.location=gmapsMarkup.currentInfo.imgCatsUrl[gmapsMarkup.currentInfo.displayed][0];
346        }
347      );
348    }
349    else
350    {
351      $('#ciGMIWC_showcatList').html('');
352      for(var i=0;i<gmapsMarkup.currentInfo.imgCatsUrl[index].length;i++)
[7500]353      {
[7616]354        $('#ciGMIWC_showcatList')
355          .append('<li><a href="'+gmapsMarkup.currentInfo.imgCatsUrl[index][i]+'">'+gmapsMarkup.currentInfo.imgCatsNames[index][i]+'</a></li>');
[7500]356      }
[7616]357      $('#ciGMIWC_img, #ciGMIWC_showcat')
358        .bind('mouseenter', function () { $('#ciGMIWC_showcat').css('display', 'block'); } )
359        .bind('mouseleave', function () { $('#ciGMIWC_showcat').css('display', 'none'); } );
360    }
361
362    if(gmapsMarkup.currentInfo.nbImg>1)
[7500]363    {
[7616]364      $('#ciGMIWC_picnum').html((index+1)+'/'+gmapsMarkup.currentInfo.nbImgTxt);
365      $('#ciWALeft, #ciWARight').css('display', 'inline-block');
[7500]366    }
[7616]367    else
368    {
369      $('#ciGMIWC_picnum').html(gmapsMarkup.currentInfo.nbImgTxt);
370      $('#ciWALeft, #ciWARight').css('display', 'none');
371    }
372  };
[7500]373
[7616]374  /**
375   *
376   */
377  this.displayPicturePrev=function()
[7500]378  {
[7616]379    gmapsMarkup.currentInfo.displayed--;
380    if(gmapsMarkup.currentInfo.displayed<0) gmapsMarkup.currentInfo.displayed=gmapsMarkup.currentInfo.nbImg-1;
[12204]381    this.displayPictureInfo(gmapsMarkup.currentInfo.displayed);
[7616]382  };
383
384  /**
385   *
386   */
387  this.displayPictureNext=function()
[7500]388  {
[7616]389    gmapsMarkup.currentInfo.displayed++;
390    if(gmapsMarkup.currentInfo.displayed>=gmapsMarkup.currentInfo.nbImg) gmapsMarkup.currentInfo.displayed=0;
[12204]391    this.displayPictureInfo(gmapsMarkup.currentInfo.displayed);
[7616]392  };
[7500]393
394
[7616]395  /**
396   * check if zoomLevel
397   */
[12204]398  this.fitToBounds=function(bounds, mapIndex)
[7616]399  {
400    gmapsMarkup.maps[mapIndex].gMap.fitBounds(bounds);
[7500]401
[7616]402    if(gmapsMarkup.maps[mapIndex].zoomLevelMaxActivated &&
403       gmapsMarkup.maps[mapIndex].gMap.getZoom() > gmapsMarkup.maps[mapIndex].zoomLevel)
404    {
405      gmapsMarkup.maps[mapIndex].gMap.setZoom(gmapsMarkup.maps[mapIndex].zoomLevel);
406    }
407  };
[7500]408
[12204]409  this.initializeMapViewport=function(mapIndex)
[7500]410  {
[12204]411    var __this=this;
412
[7616]413    if(mapIndex>-1 && !gmapsMarkup.maps[mapIndex].gMap.initialized)
414    {
415      // reduce copyright size... ^_^
416      $('#'+gmapsMarkup.maps[mapIndex].id+' span, #'+gmapsMarkup.maps[mapIndex].id+' a').css('font-size', '55.0%');
[7500]417
[7616]418      if(gmapsMarkup.maps[mapIndex].markerVisible)
419      {
420        google.maps.event.addListener(
421          gmapsMarkup.maps[mapIndex].gMap,
422          'dragend',
423          function()
424          {
[12204]425            __this.loadMarkers(this);
[7616]426          }
427        );
[7500]428
[7616]429        google.maps.event.addListener(
430          gmapsMarkup.maps[mapIndex].gMap,
431          'zoom_changed',
432          function()
433          {
[12204]434            __this.loadMarkers(this);
[7616]435            gmapsMarkup.infoWindow.close();
436          }
437        );
438      }
[7500]439
440
[7616]441      if(gmapsMarkup.maps[mapIndex].kmlZoom && gmapsMarkup.maps[mapIndex].gMap.kmlFile!=null)
442      {
443        google.maps.event.addListenerOnce(
444          gmapsMarkup.maps[mapIndex].gMap.kmlFile,
445          'defaultviewport_changed',
446          function()
447          {
[12204]448            __this.fitToBounds(gmapsMarkup.maps[mapIndex].gMap.kmlFile.getDefaultViewport(), mapIndex);
[7616]449          }
450        );
[7500]451
[7616]452      }
[7500]453
[7616]454      gmapsMarkup.maps[mapIndex].gMap.initialized=true;
[7500]455    }
[7616]456  };
[7500]457
458}
459
460
[12204]461$(window).load(
462  function ()
[7500]463  {
[12204]464    gmapsMM=new markupMaps();
[7616]465
[7500]466    // all maps have the same initials bounds
467    gmapsMarkup.currentInfo=null;
468
469    gmapsMarkup.infoWindow = new google.maps.InfoWindow();
470    google.maps.event.addListener(
471     gmapsMarkup.infoWindow,
472     'closeclick',
473     function () {
474       gmapsMarkup.infoWindow.setContent('');
475       $('#ciGMIWC_img').unbind();
476     }
477    );
478
479    /*
480     * initialize map on the server side (call the 'public.maps.init'
481     * function)
482     */
483    $.ajax(
484      {
485        type: "POST",
486        url: "plugins/GMaps/gmaps_ajax.php",
487        async: true,
488        data: { ajaxfct:"public.maps.init", category:gmapsMarkup.categoryId, mapId:'y' },
489        success:
490          function(msg)
491          {
492            gmapsMarkup.requestId=msg;
493
494            for(var i=0;i<gmapsMarkup.maps.length;i++)
495            {
496              // prepare google map maps
497              $('#'+gmapsMarkup.maps[i].id).css(
498                {
499                  width: gmapsMarkup.maps[i].width+'px',
500                  height:gmapsMarkup.maps[i].height+'px'
501                }
502              );
503
[12204]504              gmapsMM.createMap(gmapsMarkup.maps[i], i);
[7500]505            }
506          }
507      }
508    );
509  }
510);
511
512
Note: See TracBrowser for help on using the repository browser.