source: extensions/GMaps/js/gmapsCategory.js @ 7500

Last change on this file since 7500 was 7500, checked in by grum, 14 years ago

add possibility to add maps in descriptions
feature:1937

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