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.map = new GMap2($(elementId)); this.goThroughOptions(options); 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['small'] = new GSmallMapControl(); controlTypes['smallZoom'] = new GSmallZoomControl(); 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) { controlsArray.erase(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 (this.markers.has(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){ this.createMarker(id, point, options); }.bind(this)); } }, createMarker: function(id, location, options) { // Marker already known? if (this.markers.has(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); } } markerOptions.icon = customIcon; } if (options.draggable && options.draggable === true ) { markerOptions.draggable = true; } var marker = new GMarker(location, markerOptions); if (options.html && $chk(options.html)) { GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(options.html); }); } 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(); }); } // Add to the marker list this.markers.include(id, marker); // Loaded? Then add the new markers to the map if (this.map.isLoaded()) { this.addMarkersToMap.delay(500, this); } }, addOverlay: function(marker) { this.map.addOverlay(marker); }, addMarkersToMap: function() { this.markers.each(function(item, id) { // Shown on map? if (this.map.getBounds().containsLatLng(item.getLatLng())) { if(item.isHidden()) { this.addOverlay(item); } } }.bind(this)); }, clearMarkers: function() { this.map.clearOverlays(); } });