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

Last change on this file since 11342 was 10551, checked in by grum, 13 years ago

bug:2148 - compatibility with piwigo 2.2
bug:2062 - compatibility with IE7

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