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

Last change on this file since 16844 was 16011, checked in by grum, 12 years ago

feature:2638- compatibility with Piwigo 2.4

  • Property svn:executable set to *
File size: 12.9 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,
[16011]149        data: { ajaxfct:"public.maps.getMarkers", token:gmaps.token, datas:datas },
[7616]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      {
[15345]239        marker.info.imgTn[index]=replaceRoot(marker.info.imgTn[index]);
[7616]240      }
[7500]241    }
[7616]242  };
[7500]243
[15345]244  var replaceRoot=function(value)
245  {
246    var returned=value;
247    switch(value.charAt(0))
248    {
249      case 'G':
250        returned='./_data/i/galleries/'+value.substr(1);
251        break;
252      case 'I':
253        returned='./i.php?/galleries/'+value.substr(1);
254        break;
255      case 'P':
256        returned='./picture.php?/'+value.substr(1);
257        break;
258    }
259    return(returned);
260  }
261
[7616]262  var compareMarkers=function(m1,m2)
263  {
264    if(m1.uId<m2.uId)
[7500]265    {
[7616]266      return(-1);
[7500]267    }
[7616]268    else if(m1.uId<m2.uId)
269    {
270      return(1);
271    }
272    return(0);
273  };
[7500]274
[12204]275  this.markerInList=function(uniqueId, markerList)
[7500]276  {
[7616]277    for(var i=0;i<markerList.length;i++)
278    {
279      if(markerList[i].uId==uniqueId) return(i)
280    }
[7500]281    return(-1);
[7616]282  };
[7500]283
[7616]284  /**
285   * display the infowindow
286   *
287   * @param Marker marker : the marker
288   */
[12204]289  this.displayWindowInfo=function(marker)
[7500]290  {
[12204]291    if(gmapsCM && gmapsCM instanceof categoryMaps) gmapsCM.closePictureInfo();
292
[7616]293    gmapsMarkup.currentInfo=marker.info;
294    gmapsMarkup.infoWindow.close();
295    gmapsMarkup.infoWindow.setContent($('#iGMapsInfoWindowContent').clone().each(renameId).get(0));
296    gmapsMarkup.infoWindow.open(marker.map, marker);
[12204]297    $('#ciWALeft, #ciWARight').unbind('click');
298    $('#ciWARight').bind('click', function(event) { __this.displayPictureNext(); } );
299    $('#ciWALeft').bind('click', function(event) { __this.displayPicturePrev(); } );
300    this.displayPictureInfo(gmapsMarkup.currentInfo.displayed);
[7616]301  };
302  var renameId=function(i, e)
303  {
304    if(e.id!='') e.id='c'+e.id;
305    $(e).children().each(renameId);
306  };
[7500]307
308
[12204]309  this.closePictureInfo = function ()
310  {
311    gmapsMarkup.currentInfo=null;
312    gmapsMarkup.infoWindow.setContent('');
313    gmapsMarkup.infoWindow.close();
314    $('#ciWALeft, #ciWARight').unbind('click');
315  }
316
[7616]317  /**
318   *
319   */
[12204]320  this.displayPictureInfo=function(index)
[7500]321  {
[12204]322    var __this=this;
323
324    $('#ciWALeft,#ciWARight').unbind('click');
325    $('#ciWARight').bind('click',
326      function(event)
327      {
328        __this.displayPictureNext();
329      }
330    );
331    $('#ciWALeft').bind('click',
332      function(event)
333      {
334        __this.displayPicturePrev();
335      }
336    );
337
[7616]338    gmapsMarkup.currentInfo.displayed=index;
339    if(gmapsMarkup.currentInfo.imgName[index]=='')
340    {
341      $('#ciGMIWC_title').html('&nbsp;');
342    }
343    else
344    {
345      $('#ciGMIWC_title').html(gmapsMarkup.currentInfo.imgName[index]);
346    }
347    $('#ciGMIWC_img').attr('src', gmapsMarkup.currentInfo.imgTn[index]);
[7500]348
[7616]349    $('#ciGMIWC_img').unbind();
350    if(gmapsMarkup.currentInfo.imgCatsUrl[index].length==1)
351    {
352      $('#ciGMIWC_img').bind('click',
353        function ()
354        {
[15345]355          window.location=replaceRoot(gmapsMarkup.currentInfo.imgCatsUrl[gmapsMarkup.currentInfo.displayed][0]);
[7616]356        }
357      );
358    }
359    else
360    {
361      $('#ciGMIWC_showcatList').html('');
362      for(var i=0;i<gmapsMarkup.currentInfo.imgCatsUrl[index].length;i++)
[7500]363      {
[7616]364        $('#ciGMIWC_showcatList')
[15345]365          .append('<li><a href="'+replaceRoot(gmapsMarkup.currentInfo.imgCatsUrl[index][i])+'">'+gmapsMarkup.currentInfo.imgCatsNames[index][i]+'</a></li>');
[7500]366      }
[7616]367      $('#ciGMIWC_img, #ciGMIWC_showcat')
368        .bind('mouseenter', function () { $('#ciGMIWC_showcat').css('display', 'block'); } )
369        .bind('mouseleave', function () { $('#ciGMIWC_showcat').css('display', 'none'); } );
370    }
371
372    if(gmapsMarkup.currentInfo.nbImg>1)
[7500]373    {
[7616]374      $('#ciGMIWC_picnum').html((index+1)+'/'+gmapsMarkup.currentInfo.nbImgTxt);
375      $('#ciWALeft, #ciWARight').css('display', 'inline-block');
[7500]376    }
[7616]377    else
378    {
379      $('#ciGMIWC_picnum').html(gmapsMarkup.currentInfo.nbImgTxt);
380      $('#ciWALeft, #ciWARight').css('display', 'none');
381    }
382  };
[7500]383
[7616]384  /**
385   *
386   */
387  this.displayPicturePrev=function()
[7500]388  {
[7616]389    gmapsMarkup.currentInfo.displayed--;
390    if(gmapsMarkup.currentInfo.displayed<0) gmapsMarkup.currentInfo.displayed=gmapsMarkup.currentInfo.nbImg-1;
[12204]391    this.displayPictureInfo(gmapsMarkup.currentInfo.displayed);
[7616]392  };
393
394  /**
395   *
396   */
397  this.displayPictureNext=function()
[7500]398  {
[7616]399    gmapsMarkup.currentInfo.displayed++;
400    if(gmapsMarkup.currentInfo.displayed>=gmapsMarkup.currentInfo.nbImg) gmapsMarkup.currentInfo.displayed=0;
[12204]401    this.displayPictureInfo(gmapsMarkup.currentInfo.displayed);
[7616]402  };
[7500]403
404
[7616]405  /**
406   * check if zoomLevel
407   */
[12204]408  this.fitToBounds=function(bounds, mapIndex)
[7616]409  {
410    gmapsMarkup.maps[mapIndex].gMap.fitBounds(bounds);
[7500]411
[7616]412    if(gmapsMarkup.maps[mapIndex].zoomLevelMaxActivated &&
413       gmapsMarkup.maps[mapIndex].gMap.getZoom() > gmapsMarkup.maps[mapIndex].zoomLevel)
414    {
415      gmapsMarkup.maps[mapIndex].gMap.setZoom(gmapsMarkup.maps[mapIndex].zoomLevel);
416    }
417  };
[7500]418
[12204]419  this.initializeMapViewport=function(mapIndex)
[7500]420  {
[12204]421    var __this=this;
422
[7616]423    if(mapIndex>-1 && !gmapsMarkup.maps[mapIndex].gMap.initialized)
424    {
425      // reduce copyright size... ^_^
426      $('#'+gmapsMarkup.maps[mapIndex].id+' span, #'+gmapsMarkup.maps[mapIndex].id+' a').css('font-size', '55.0%');
[7500]427
[7616]428      if(gmapsMarkup.maps[mapIndex].markerVisible)
429      {
430        google.maps.event.addListener(
431          gmapsMarkup.maps[mapIndex].gMap,
432          'dragend',
433          function()
434          {
[12204]435            __this.loadMarkers(this);
[7616]436          }
437        );
[7500]438
[7616]439        google.maps.event.addListener(
440          gmapsMarkup.maps[mapIndex].gMap,
441          'zoom_changed',
442          function()
443          {
[12204]444            __this.loadMarkers(this);
[7616]445            gmapsMarkup.infoWindow.close();
446          }
447        );
448      }
[7500]449
450
[7616]451      if(gmapsMarkup.maps[mapIndex].kmlZoom && gmapsMarkup.maps[mapIndex].gMap.kmlFile!=null)
452      {
453        google.maps.event.addListenerOnce(
454          gmapsMarkup.maps[mapIndex].gMap.kmlFile,
455          'defaultviewport_changed',
456          function()
457          {
[12204]458            __this.fitToBounds(gmapsMarkup.maps[mapIndex].gMap.kmlFile.getDefaultViewport(), mapIndex);
[7616]459          }
460        );
[7500]461
[7616]462      }
[7500]463
[7616]464      gmapsMarkup.maps[mapIndex].gMap.initialized=true;
[7500]465    }
[7616]466  };
[7500]467
468}
469
470
[12204]471$(window).load(
472  function ()
[7500]473  {
[15345]474    initInfoWindow();
475
[12204]476    gmapsMM=new markupMaps();
[7616]477
[7500]478    // all maps have the same initials bounds
479    gmapsMarkup.currentInfo=null;
480
481    gmapsMarkup.infoWindow = new google.maps.InfoWindow();
482    google.maps.event.addListener(
483     gmapsMarkup.infoWindow,
484     'closeclick',
485     function () {
486       gmapsMarkup.infoWindow.setContent('');
487       $('#ciGMIWC_img').unbind();
488     }
489    );
490
491    /*
492     * initialize map on the server side (call the 'public.maps.init'
493     * function)
494     */
495    $.ajax(
496      {
497        type: "POST",
498        url: "plugins/GMaps/gmaps_ajax.php",
499        async: true,
[16011]500        data: { ajaxfct:"public.maps.init", token:gmaps.token, category:gmapsMarkup.categoryId, mapId:'y' },
[7500]501        success:
502          function(msg)
503          {
504            gmapsMarkup.requestId=msg;
505
506            for(var i=0;i<gmapsMarkup.maps.length;i++)
507            {
508              // prepare google map maps
509              $('#'+gmapsMarkup.maps[i].id).css(
510                {
511                  width: gmapsMarkup.maps[i].width+'px',
512                  height:gmapsMarkup.maps[i].height+'px'
513                }
514              );
515
[12204]516              gmapsMM.createMap(gmapsMarkup.maps[i], i);
[7500]517            }
518          }
519      }
520    );
521  }
522);
523
524
Note: See TracBrowser for help on using the repository browser.