var EosGoogleMaps = new Class({ /* elementId: id of the element for the map to appear in location: array with [0] latitude co-ordinates, [1] longitude co-ordinates or string with address options: object with type: string possible values: normal, satellite, hybrid controls: array possible values: [large, small or smallZoom,] scale, [type or hierarchicalType], overview scrollWheel: boolean markers: array with [x] object with position: array with [0] latitude coordinates, [1] longitude coordinates marker icon: url to icon popupHtml: */ defaultValues: { 'type': 'normal' }, initialize: function(elementId, location, zoomLevel, options){ if (this.checkBrowser() === true && $chk(zoomLevel)) { this.geocoder = new GClientGeocoder(); this.markers = new Hash(); this.mapDomElement = $(elementId); this.map = new GMap2(this.mapDomElement); this.goThroughOptions(options); this.hiddenMarkers = new Array(); this.hideOutOfBoundsMarkers = true; var coordinates = this.convertStringToCoordinates(location); if (coordinates !== false) { this.goToCoordinates(coordinates, zoomLevel); } else { this.goToLocation(location, zoomLevel); } // We wait with adding the markers till the map is loaded GEvent.addListener(this.map, "load", function() { if (this.markers.getLength > 0) { this.addMarkersToMap.delay(500, this); } }.bind(this)); this.addUnloadEvents(); } }, goToCoordinates: function(coordinates, zoomLevel) { this.zoomLevel = zoomLevel; this.setCenter(coordinates); }, goToLocation: function(location, zoomLevel) { this.zoomLevel = zoomLevel; if (this.checkCoordinates(location) === true) { this.setCenter(location); } else if($type(location) == 'string') { // check if location is string and then check address this.goToAddress(location); } }, setCenter: function (center) { if ($type(center) === 'array') { var point = new GLatLng(center[0], center[1]); this.map.setCenter(point, this.zoomLevel); } else if ($type(center) === 'object') { this.map.setCenter(center, this.zoomLevel); } }, goToAddress: function (address) { this.geocoder.getLatLng(address, function(gps) { this.setCenter(gps, this.zoomLevel); }.bind(this) ); }, setMapType: function (type) { var googleMapsTypes = new Array(); googleMapsTypes['normal'] = G_NORMAL_MAP; googleMapsTypes['satellite'] = G_SATELLITE_MAP; googleMapsTypes['hybrid'] = G_HYBRID_MAP; if ($chk(type) === true && $chk(googleMapsTypes[type])) { this.map.setMapType(googleMapsTypes[type]); } }, checkBrowser: function () { return GBrowserIsCompatible(); }, convertStringToCoordinates: function(location) { if ($chk(location) === true && $type(location) === 'string') { var coordinates = location.split(','); if (this.checkCoordinates(coordinates)) { coordinates[0] = coordinates[0].toFloat(); coordinates[1] = coordinates[1].toFloat(); return coordinates; } } return false; }, checkCoordinates: function(coordinates) { if ($chk(coordinates) === true && $type(coordinates) === 'array') { // checken of het er twee zijn, en twee nummertjes if ($chk(coordinates[0]) && $chk(coordinates[1]) && parseInt(coordinates[0]) && parseInt(coordinates[1])) { return true; } } return false; }, goThroughOptions: function(options) { // map type if ($chk(options.type) === true) { this.setMapType(options.type); } else { this.setMapType(this.defaultValues.type); } // controls this.addControls(options.controls); //ScrollWheel if ($chk(options.scrollWheel) === true && options.scrollWheel === true) { this.addScrollWheel(); } }, addControls: function(controls) { var controlTypes = new Array(); controlTypes['large'] = new GLargeMapControl(); controlTypes['large3D'] = new GLargeMapControl3D(); controlTypes['small'] = new GSmallMapControl(); controlTypes['smallZoom'] = new GSmallZoomControl(); controlTypes['smallZoom3D'] = new GSmallZoomControl3D(); controlTypes['scale'] = new GScaleControl(); controlTypes['type'] = new GMapTypeControl(); controlTypes['hierarchicalType'] = new GHierarchicalMapTypeControl(); controlTypes['overview'] = new GOverviewMapControl(); if (this.checkControls(controls) === true) { controls.each(function(control) { this.map.addControl(controlTypes[control]); }, this); }; }, checkControls: function(controls) { var valid = false; if ($chk(controls) === true && $type(controls) === 'array') { valid = true; } else { return false; } // check for conflicts var conflictTypesZoom = ['large','small', 'smallZoom']; var conflictTypesType = ['type','hierarchicalType']; var itemHasConflicts = function(controlsArray, type) { var originalLength = controlsArray.length; type.each(function(conflict) { if ($chk(controlsArray.erase)) { // mootools 1.2 controlsArray.erase(conflict); } else { // mootools 1.1 controlsArray.remove(conflict); } }); return originalLength - controlsArray.length <= 1; }; if (itemHasConflicts($A(controls), conflictTypesZoom) === true) { valid = true; } else { return false; } if (itemHasConflicts($A(controls), conflictTypesType) === true) { valid = true; } else { return false; } return valid; }, addScrollWheel: function() { this.map.enableScrollWheelZoom(); }, addUnloadEvents: function () { window.addEvent('unload', function() { GUnload(); }); }, addMarker: function(id, location, options) { // Marker already known? if ($chk(this.markers['values']) && this.markers.values().contains(id)) { return true; } if (!$chk(options)) { options = {}; } var coordinates = this.convertStringToCoordinates(location); if (coordinates !== false) { var point = new GLatLng(parseFloat(coordinates[0]), parseFloat(coordinates[1])); this.createMarker(id, point, options); } else if (this.checkCoordinates(location) === true) { // Should not be encountered. var point = new GLatLng(parseFloat(location[0]), parseFloat(location[1])); this.createMarker(id, point, options); } else if($type(location) == 'string') { // check if location is string and then check address this.geocoder.getLatLng(location, function(point){ if ($chk(point)) { this.createMarker(id, point, options); }/* else { console.log(location); }*/ }.bind(this)); } }, createMarker: function(id, location, options) { // Marker already known? if ($chk(this.markers['values']) && this.markers.values().contains(id)) { return true; } var markerOptions = {}; if (options.icon && $chk(options.icon)) { var customIcon = new GIcon(G_DEFAULT_ICON); customIcon.image = options.icon; if (options.iconsizeX && options.iconsizeY) { customIcon.iconSize = new GSize(options.iconsizeX, options.iconsizeY); } if (options.shadow && $chk(options.shadow)) { customIcon.shadow = options.shadow; if (options.shadowsizeX && options.shadowsizeY) { customIcon.shadowSize = new GSize(options.shadowsizeX, options.shadowsizeY); } } if (options.iconmap && $chk(options.iconmap)) { customIcon.imageMap = options.iconmap; } if (options.iconAnchor && $chk(options.iconAnchor)) { customIcon.iconAnchor = new GPoint(options.iconAnchor[0], options.iconAnchor[1]); } markerOptions.icon = customIcon; } if (options.draggable && options.draggable === true ) { markerOptions.draggable = true; } var marker = new GMarker(location, markerOptions); if (options.hidden && options.hidden === true) { //marker.hide(); this.hiddenMarkers.push(id); } if (options.html && $chk(options.html)) { GEvent.addListener(marker, "click", function() { if (!$chk(options.htmlOptions)) { var markerOptions = {}; } else { var markerOptions = options.htmlOptions; } marker.openInfoWindowHtml(options.html,markerOptions); }); } if (options.tooltip && $chk(options.tooltip)) { marker.sbtooltip = new Element('div', { styles: { display: 'none' } } ); if (options.tooltipClass && $chk(options.tooltipClass)) { marker.sbtooltip.addClass(options.tooltipClass); } marker.sbtooltip.addClass('gMapsMarkerTooltip'); marker.sbtooltip.setHTML(options.tooltip); GEvent.addListener(marker, "mouseover", function() { var point=this.map.getCurrentMapType().getProjection().fromLatLngToPixel(this.map.getBounds().getSouthWest(),this.map.getZoom()); var offset=this.map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),this.map.getZoom()); var anchor=marker.getIcon().iconAnchor; var width=marker.getIcon().iconSize.width; var tooltipSize = marker.sbtooltip.getSize(); var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); pos.apply(marker.sbtooltip); marker.sbtooltip.setStyle('display', 'inline'); }.bind(this)); var hideTooltip = function() { marker.sbtooltip.setStyle('display', 'none'); }; GEvent.addListener(marker, "mouseout", hideTooltip); GEvent.addListener(marker, "dragstart", hideTooltip); GEvent.addListener(marker, "remove", hideTooltip); marker.sbtooltip.injectInside(this.mapDomElement); } if (options.draggable && options.draggable === true ) { GEvent.addListener(marker, "dragstart", function() { this.map.closeInfoWindow(); }.bind(this)); } if (options.draggable && options.draggable === true && options.onDragged) { GEvent.addListener(marker, "dragend", function() { var point = marker.getPoint(); $(options.onDragged).value = point.lat()+','+point.lng(); }); } if ($chk(options.events)) { options.events.each(function(func, eventType) { GEvent.addListener(marker, eventType, func); }); } // Add to the marker list if ($chk(this.markers.include)) { // mootools 1.2 this.markers.include(id, marker); } else { // mootools 1.1 if (!$chk(this.markers.get(id))) { this.markers.set(id, marker); } } // Loaded? Then add the new markers to the map GEvent.addListener(this.map, "load", this.addMarkersToMap.delay(500, this)); //} }, addOverlay: function(marker) { this.map.addOverlay(marker); }, removeOverlay: function(overlay) { this.map.removeOverlay(overlay); }, getMarkerById: function(markerId) { return this.markers.get(markerId); }, hideMaker: function(markerId) { this.hiddenMarkers.merge([markerId]); this.getMarkerById(markerId).hide(); }, showMarker: function(markerId) { this.hiddenMarkers.remove(markerId); this.getMarkerById(markerId).show(); }, addMarkersToMap: function() { this.markers.each(function(item, id) { // Shown on map? if (this.hideOutOfBoundsMarkers == false || this.map.getBounds().containsLatLng(item.getLatLng())) { if(item.isHidden() && !this.hiddenMarkers.contains(id)) { this.addOverlay(item); } } }.bind(this)); }, removeMarker: function(markerId) { this.removeOverlay(this.getMarkerById(markerId)); this.hiddenMarkers.remove(markerId); this.markers.remove(markerId); }, clearMarkers: function() { this.markers.empty(); this.map.clearOverlays(); } });