function gMap()
{
this.mMap = null;
this.mRunAfterCreation = new Array();
this.mMenuItems = new Array();
this.mMenuFunctions = new Array();
this.mMarkers = new Array();
this.mMarkersLength = 0;
this.mInfoWindow = null;
this.mContainerID = "map_canvas";
this.mLat = 0;
this.mLng = 0;
this.mZoom = 14;
this.mOnRightClick = function() {};
}
gMap.prototype.appendMarker = function (aItem)
{
this.mMarkers[this.mMarkers.length] = aItem;
}
// Event handler
gMap.prototype.handleEvent = function(event) {
if (event.type === "load")
this.initialize();
}
gMap.prototype.initialize = function()
{
var tLatlng = new google.maps.LatLng(this.mLat, this.mLng);
var tMapOptions = {
zoom: this.mZoom,
center: tLatlng
};
this.mMap = new google.maps.Map(document.getElementById(this.mContainerID), tMapOptions);
google.maps.event.addListener(this.mMap, "rightclick", this.mOnRightClick);
this.mInfoWindow = new google.maps.InfoWindow();
for (var i = 0; i < this.mRunAfterCreation.length; i++)
{
this.mRunAfterCreation[i](this);
}
};
gMap.prototype.createMap = function(aContainerID, aLat, aLng, aZoom)
{
if (aZoom === undefined) aZoom = 14;
this.mContainerID = aContainerID;
this.mLat = aLat;
this.mLng = aLng;
this.mZoom = aZoom;
google.maps.event.addDomListener(window, 'load', this);
}
gMap.prototype.queue = function(aFunction)
{
this.mRunAfterCreation[this.mRunAfterCreation.length] = aFunction;
}
gMap.prototype.addMarker = function(aTitle, aLat, aLng, aInfoText)
{
if (aInfoText === undefined) aInfoText = '';
this.queue(function(aRef)
{
var tMarker = new google.maps.Marker({
position: new google.maps.LatLng(aLat, aLng),
map: aRef.mMap,
title: aTitle,
});
aRef.appendMarker(tMarker);
if (aInfoText != '')
{
google.maps.event.addListener(tMarker, 'click', function() {
aRef.mInfoWindow.setContent(aInfoText);
aRef.mInfoWindow.open(aRef.mMap, tMarker);
});
}
});
this.mMarkersLength++;
return this.mMarkersLength - 1;
};
gMap.prototype.getMarker = function(aIndex)
{
return this.mMarkers[aIndex];
}
gMap.prototype.onClick = function(aFunction)
{
this.queue(function(aRef)
{
aRef.mOnLeftClick = aFunction;
google.maps.event.clearListeners(aRef.mMap, 'click');
google.maps.event.addListener(aRef.mMap, "click", aRef.mOnLeftClick(aRef));
});
};
gMap.prototype.onRightClick = function(aFunction)
{
this.queue(function(aRef)
{
aRef.mOnRightClick = aFunction;
google.maps.event.clearListeners(aRef.mMap, 'rightclick');
google.maps.event.addListener(aRef.mMap, "rightclick", aRef.mOnRightClick(aRef));
});
};
gMap.prototype.createContextMenu = function()
{
this.onRightClick( function(aRef) {
return function(event) {
var tContextMenuDir;
$('.map_context_menu').remove();
tContextMenuDir = document.createElement("div");
tContextMenuDir.className = 'map_context_menu';
tMenuContent = '';
for (var i = 0; i < aRef.mMenuItems.length; i++)
{
// Replacements
// Latitude :lat:
tItem = aRef.mMenuItems[i].replace(':lat:', event.latLng.lat().toString());
// Longitude :lng:
tItem = tItem.replace(':lng:', event.latLng.lng().toString());
// Create item
tMenuContent += '<div class="map_menu_item">' + tItem + '</div>';
}
tContextMenuDir.innerHTML = tMenuContent;
$(aRef.mMap.getDiv()).append(tContextMenuDir);
$('.map_context_menu').css('left', event.pixel.x);
$('.map_context_menu').css('top', event.pixel.y);
tContextMenuDir.style.visibility = "visible";
for (var i = 0; i < aRef.mMenuItems.length; i++)
{
aRef.mMenuFunctions[i](aRef, event);
}
};
});
this.onClick( function(aRef)
{
return function(event) {$('.map_context_menu').remove()};
});
};
gMap.prototype.addMenuItem = function(aHTML, aFunction)
{
if (aFunction === undefined) aFunction = function(aRef, event){};
this.mMenuItems[this.mMenuItems.length] = aHTML;
this.mMenuFunctions[this.mMenuFunctions.length] = aFunction;
};
gMap.prototype.centerPos = function(aLat, aLng)
{
tPos = new google.maps.LatLng(aLat, aLng);
this.mMap.setCenter(tPos);
}
gMap.prototype.center = function(aPos)
{
this.mMap.setCenter(aPos);
}
gMap.prototype.resize = function()
{
google.maps.event.trigger(this.mMap, "resize");
}