Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. function gMap()
  2. {
  3.     this.mMap = null;
  4.     this.mRunAfterCreation = new Array();
  5.     this.mMenuItems = new Array();
  6.     this.mMenuFunctions = new Array();
  7.     this.mMarkers = new Array();
  8.     this.mMarkersLength = 0;
  9.     this.mInfoWindow = null;
  10.     this.mContainerID = "map_canvas";
  11.     this.mLat = 0;
  12.     this.mLng = 0;
  13.     this.mZoom = 14;
  14.     this.mOnRightClick = function() {};
  15. }
  16.  
  17. gMap.prototype.appendMarker = function (aItem)
  18. {
  19.     this.mMarkers[this.mMarkers.length] = aItem;
  20. }
  21.  
  22. // Event handler
  23. gMap.prototype.handleEvent = function(event) {
  24.     if (event.type === "load")
  25.         this.initialize();
  26. }
  27.  
  28. gMap.prototype.initialize = function()
  29. {
  30.     var tLatlng = new google.maps.LatLng(this.mLat, this.mLng);
  31.     var tMapOptions = {
  32.         zoom: this.mZoom,
  33.         center: tLatlng
  34.     };
  35.     this.mMap = new google.maps.Map(document.getElementById(this.mContainerID), tMapOptions);
  36.     google.maps.event.addListener(this.mMap, "rightclick", this.mOnRightClick);
  37.     this.mInfoWindow = new google.maps.InfoWindow();
  38.     for (var i = 0; i < this.mRunAfterCreation.length; i++)
  39.     {
  40.         this.mRunAfterCreation[i](this);
  41.     }
  42. };
  43.  
  44. gMap.prototype.createMap = function(aContainerID, aLat, aLng, aZoom)
  45. {
  46.     if (aZoom === undefined) aZoom = 14;
  47.     this.mContainerID = aContainerID;
  48.     this.mLat = aLat;
  49.     this.mLng = aLng;
  50.     this.mZoom = aZoom;
  51.     google.maps.event.addDomListener(window, 'load', this);
  52. }
  53.  
  54. gMap.prototype.queue = function(aFunction)
  55. {
  56.     this.mRunAfterCreation[this.mRunAfterCreation.length] = aFunction;
  57. }
  58.  
  59. gMap.prototype.addMarker = function(aTitle, aLat, aLng, aInfoText)
  60. {
  61.     if (aInfoText === undefined) aInfoText = '';
  62.     this.queue(function(aRef)
  63.     {
  64.         var tMarker = new google.maps.Marker({
  65.             position: new google.maps.LatLng(aLat, aLng),
  66.             map: aRef.mMap,
  67.             title: aTitle,
  68.         });
  69.         aRef.appendMarker(tMarker);
  70.        
  71.         if (aInfoText != '')
  72.         {
  73.             google.maps.event.addListener(tMarker, 'click', function() {
  74.                 aRef.mInfoWindow.setContent(aInfoText);
  75.                 aRef.mInfoWindow.open(aRef.mMap, tMarker);
  76.             });
  77.         }
  78.     });
  79.    
  80.     this.mMarkersLength++;
  81.     return this.mMarkersLength - 1;
  82. };
  83.  
  84. gMap.prototype.getMarker = function(aIndex)
  85. {
  86.     return this.mMarkers[aIndex];
  87. }
  88.  
  89. gMap.prototype.onClick = function(aFunction)
  90. {
  91.     this.queue(function(aRef)
  92.     {
  93.         aRef.mOnLeftClick = aFunction;
  94.         google.maps.event.clearListeners(aRef.mMap, 'click');
  95.         google.maps.event.addListener(aRef.mMap, "click", aRef.mOnLeftClick(aRef));
  96.     });
  97. };
  98.  
  99. gMap.prototype.onRightClick = function(aFunction)
  100. {
  101.     this.queue(function(aRef)
  102.     {
  103.         aRef.mOnRightClick = aFunction;
  104.         google.maps.event.clearListeners(aRef.mMap, 'rightclick');
  105.         google.maps.event.addListener(aRef.mMap, "rightclick", aRef.mOnRightClick(aRef));
  106.     });
  107. };
  108.  
  109. gMap.prototype.createContextMenu = function()
  110. {
  111.     this.onRightClick( function(aRef) {
  112.         return function(event) {
  113.             var tContextMenuDir;
  114.             $('.map_context_menu').remove();
  115.             tContextMenuDir = document.createElement("div");
  116.             tContextMenuDir.className  = 'map_context_menu';
  117.            
  118.             tMenuContent = '';
  119.             for (var i = 0; i < aRef.mMenuItems.length; i++)
  120.             {
  121.                 // Replacements
  122.                     // Latitude :lat:
  123.                 tItem = aRef.mMenuItems[i].replace(':lat:', event.latLng.lat().toString());
  124.                     // Longitude :lng:
  125.                 tItem = tItem.replace(':lng:', event.latLng.lng().toString());
  126.                 // Create item
  127.                 tMenuContent += '<div class="map_menu_item">' + tItem + '</div>';
  128.             }
  129.             tContextMenuDir.innerHTML = tMenuContent;
  130.  
  131.             $(aRef.mMap.getDiv()).append(tContextMenuDir);
  132.  
  133.             $('.map_context_menu').css('left', event.pixel.x);
  134.             $('.map_context_menu').css('top', event.pixel.y);
  135.  
  136.             tContextMenuDir.style.visibility = "visible";
  137.  
  138.             for (var i = 0; i < aRef.mMenuItems.length; i++)
  139.             {
  140.                 aRef.mMenuFunctions[i](aRef, event);
  141.             }
  142.         };
  143.     });
  144.     this.onClick( function(aRef)
  145.         {
  146.             return function(event) {$('.map_context_menu').remove()};
  147.         });
  148. };
  149.  
  150. gMap.prototype.addMenuItem = function(aHTML, aFunction)
  151. {
  152.     if (aFunction === undefined) aFunction = function(aRef, event){};
  153.     this.mMenuItems[this.mMenuItems.length] = aHTML;
  154.     this.mMenuFunctions[this.mMenuFunctions.length] = aFunction;
  155. };
  156.  
  157. gMap.prototype.centerPos = function(aLat, aLng)
  158. {
  159.     tPos = new google.maps.LatLng(aLat, aLng);
  160.     this.mMap.setCenter(tPos);
  161. }
  162. gMap.prototype.center = function(aPos)
  163. {
  164.     this.mMap.setCenter(aPos);
  165. }
  166.  
  167. gMap.prototype.resize = function()
  168. {
  169.     google.maps.event.trigger(this.mMap, "resize");
  170. }