var OLM = {}; (function(OLM) { OLM.initialized = false; OLM.minZoom = 3; OLM.maxZoom = 17; OLM.maxResolution = 156543.0339; OLM.resolutions = []; OLM.zoomOffset = 0; OLM.LAYER_RELIEF_ZINDEX = 100; OLM.LAYER_PATH_ZINDEX = 400; OLM.LAYER_MARKERS_ZINDEX = 500; OLM.LAYER_MARKERSHIGHLIGHTED_ZINDEX = 600; OLM.Markers = new function() { this.nextMarkerId = 1; this.createMarker = function(latLng, map, style) { var point = map.fromLatLng(latLng); var marker = new OpenLayers.Feature.Vector( new OpenLayers.Geometry.Point(point.lon, point.lat), {style: style}, { externalGraphic: style.externalGraphic, graphicHeight: style.graphicHeight, graphicWidth: style.graphicWidth, graphicXOffset: style.graphicXOffset, graphicYOffset: style.graphicYOffset, cursor: style.cursor } ); marker.redraw = function(style) { if (this.olmLayerAdded) { this.olmLayerAdded.drawFeature(this); } } marker.olmMarkerId = this.getNewMarkerId(); marker.olmMap = map; marker.olmLayerAdded = null; return marker; } this.getNewMarkerId = function() { return this.nextMarkerId++; } }(); OLM.OLMap = function(elMap, settings) { this.elMap = elMap; this.map; this.ctrlScaleLine; this.ctrlNavigation; this.elSHOCartLogo; this.isOnTurist; this.isOnCyklo; this.isOnIdnes; this.isOnRelief; this.isOnSimplePath = false; this.vSimplePathLayer = null; this.isOnSimplePathMarkers = false; this.vSimplePathMarkersLayer = null; this.addedMarkers = {}; this.vMarkersLayer = null; this.vMarkersLayerSelectControl = null; this.vMarkersHighlightedLayer = null; this.projection = new OpenLayers.Projection('EPSG:900913'); this.displayProjection = new OpenLayers.Projection('EPSG:4326'); this.initialize = function() { this.map = new OpenLayers.Map(this.elMap.id, { controls: [], maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), maxResolution: OLM.maxResolution, projection: this.projection, displayProjection: this.displayProjection }); //this.map.addControl(new OpenLayers.Control.MousePosition()); this.prepareDefaultLayers(); this.projectionObject = this.map.getProjectionObject(); if ((settings.pos) && (settings.zoom)) this.setCenter(settings.pos, settings.zoom); this.prepareDefWorkingArea(); } this.prepareDefWorkingArea = function() { this.defWorkingArea = new OpenLayers.Bounds(); this.defWorkingArea.extend(this.fromLatLng(new OLM.LatLng(80, -180))); this.defWorkingArea.extend(this.fromLatLng(new OLM.LatLng(-80, 180))); } this.isInDefWorkingArea = function(pos) { return this.defWorkingArea.containsLonLat((new OpenLayers.LonLat(pos.lng, pos.lat)).transform(this.map.displayProjection, this.projectionObject)); } this.activateNavigation = function() { if (!this.ctrlNavigation) { this.ctrlNavigation = new OpenLayers.Control.Navigation({'zoomWheelEnabled': false}); this.map.addControl(this.ctrlNavigation); } } this.deactivateNavigation = function() { if (this.ctrlNavigation) { this.map.removeControl(this.ctrlNavigation); this.ctrlNavigation = null; } } this.activateMapWheel = function() { if (this.ctrlNavigation) { this.ctrlNavigation.enableZoomWheel(); } } this.deactivateMapWheel = function() { if (this.ctrlNavigation) { this.ctrlNavigation.disableZoomWheel(); } } this.activateScaleLine = function() { if (!this.ctrlScaleLine) { this.ctrlScaleLine = new OpenLayers.Control.ScaleLine(); this.ctrlScaleLine.geodesic = true; this.map.addControl(this.ctrlScaleLine); } } this.deactivateScaleLine = function() { if (this.ctrlScaleLine) { this.map.removeControl(this.ctrlScaleLine); this.ctrlScaleLine = null; } } this.activateSHOCartLogo = function() { if (!this.elSHOCartLogo) { this.elSHOCartLogo = document.createElement('a'); this.elSHOCartLogo.href = 'http://www.shocart.cz/'; this.elSHOCartLogo.target = '_blank'; this.elSHOCartLogo.style.display = 'block'; this.elSHOCartLogo.style.position = 'relative'; this.elSHOCartLogo.style.styleFloat = 'right'; this.elSHOCartLogo.style.cssFloat = 'right'; this.elSHOCartLogo.style.right = '0px'; this.elSHOCartLogo.style.bottom = '0px'; this.elSHOCartLogo.style.zIndex = '9999'; this.elSHOCarLogoImg = document.createElement('img'); this.elSHOCarLogoImg.src = 'http://www.cykloserver.cz/img/shocart_logo_h25_emb.gif'; this.elSHOCarLogoImg.style.position = 'absolute'; this.elSHOCarLogoImg.style.zIndex = '9999'; this.elSHOCartLogo.appendChild(this.elSHOCarLogoImg); this.elMap.appendChild(this.elSHOCartLogo); } } this.deactivateSHOCartLogo = function() { if (this.elSHOCartLogo) { this.elMap.removeChild(this.elSHOCartLogo); this.elSHOCartLogo = null; } } this.moveSHOCartLogo = function(left, bottom, right, top) { if (this.elSHOCarLogoImg) { if (left != null) { this.elSHOCarLogoImg.style.left = left + 'px'; } if (bottom != null) { this.elSHOCarLogoImg.style.bottom = bottom + 'px'; } if (right != null) { this.elSHOCarLogoImg.style.right = right + 'px'; } if (top != null) { this.elSHOCarLogoImg.style.top = top + 'px'; } } } this.activateSimplePath = function(pts, opts) { if (!this.isOnSimplePath) { this.isOnSimplePath = true; this.vSimplePathLayer = new OpenLayers.Layer.Vector('SimplePath'); this.map.addLayer(this.vSimplePathLayer); this.vSimplePathLayer.div.style.zIndex = OLM.LAYER_PATH_ZINDEX; var style = { strokeColor: "#000000", strokeWidth: 5, strokeOpacity: 0.8 }; if (opts.color) style.strokeColor = opts.color; if (opts.width) style.strokeWidth = opts.width; if (opts.opacity) style.strokeOpacity = opts.opacity; var points = new Array(); while (pts.length) { var p = pts.pop(); points.push(this.pointFromLatLng(p)); } var g = new OpenLayers.Geometry.LineString(points); this.vSimplePathLayer.addFeatures([new OpenLayers.Feature.Vector(g, null, style)]); } } this.deactivateSimplePath = function() { if (this.isOnSimplePath) { this.isOnSimplePath = false; this.map.removeLayer(this.vSimplePathLayer); this.vSimplePathLayer.removeAllFeatures(); this.vSimplePathLayer = null; } } this.activateSimplePathMarkers = function(markers) { if (!this.isOnSimplePathMarkers) { this.isOnSimplePathMarkers = true; this.vSimplePathMarkerLayer = new OpenLayers.Layer.Markers('SimplePathMarkers'); this.map.addLayer(this.vSimplePathMarkerLayer); while (markers.length) { var markerInfo = markers.pop(); if (markerInfo.type == 'start') { var size = new OpenLayers.Size(30, 24); var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); var icon = new OpenLayers.Icon('http://www.cykloserver.cz/img/cyklo-ico-start.gif', size, offset); } else if (markerInfo.type == 'end') { var size = new OpenLayers.Size(30, 24); var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); var icon = new OpenLayers.Icon('http://www.cykloserver.cz/img/cyklo-ico-cil.gif', size, offset); } else if (markerInfo.type == 'point') { var size = new OpenLayers.Size(10, 10); var offset = new OpenLayers.Pixel(-(size.w/2), -size.h); var icon = new OpenLayers.Icon('http://www.cykloserver.cz/img/cykloatlas-point.gif', size, offset); } else { var size = new OpenLayers.Size(30, 24); var offset = new OpenLayers.Pixel(-(size.w/2) + 1, -size.h + 1); var icon = new OpenLayers.Icon('http://www.cykloserver.cz/img/cyklo-ico-poi.gif', size, offset); } icon.imageDiv.title = markerInfo.name; var marker = new OpenLayers.Marker(this.fromLatLng(markerInfo.pos), icon); this.vSimplePathMarkerLayer.addMarker(marker); } } } this.deactivateSimplePathMarkers = function() { if (this.isOnSimplePathMarkers) { this.isOnSimplePathMarkers = false; this.map.removeLayer(this.vSimplePathMarkerLayer); this.vSimplePathMarkerLayer = null; } } this.addMarker = function(marker, settings) { if (!this.addedMarkers['id' + marker.olmMarkerId]) { this.addedMarkers['id' + marker.olmMarkerId] = marker; marker.olmLayerAdded = this.vMarkersLayer; this.vMarkersLayer.addFeatures([marker]); } } this.removeMarker = function(marker) { if (this.addedMarkers['id' + marker.olmMarkerId]) { this.vMarkersLayer.removeFeatures([marker]); marker.olmLayerAdded = null; this.addedMarkers['id' + marker.olmMarkerId] = null; } } this.removeAllMarkers = function() { while (this.addedMarkers.length) { var m = this.addedMarkers.pop(); this.vMarkersLayer.removeFeatures([marker]); } } this.prepareDefaultLayers = function() { this.isOnTurist = false; this.isOnCyklo = false; this.isOnIdnes = false; this.isOnRelief = false; this.activateLayerCyklo(); this.prepareMarkersLayer(); } this.prepareMarkersLayer = function() { this.vMarkersLayer = new OpenLayers.Layer.Vector("OLLayerMarkers"); this.map.addLayer(this.vMarkersLayer); this.vMarkersLayer.div.style.zIndex = OLM.LAYER_MARKERS_ZINDEX; this.vMarkersHighlightedLayer = new OpenLayers.Layer.Vector("OLLayerMarkersHighlighted"); this.map.addLayer(this.vMarkersHighlightedLayer); this.vMarkersHighlightedLayer.div.style.zIndex = OLM.LAYER_MARKERSHIGHLIGHTED_ZINDEX; this.vMarkersLayerSelectControl = new OpenLayers.Control.SelectFeature([this.vMarkersLayer, this.vMarkersHighlightedLayer], { callbacks: { click: jQuery.proxy(this.onMarkersLayerFeatureSelected, this), over: jQuery.proxy(this.onMarkersLayerBeforeFeatureHighlighted, this), out: jQuery.proxy(this.onMarkersLayerFeatureUnhighlighted, this) } }); this.map.addControl(this.vMarkersLayerSelectControl); this.vMarkersLayerSelectControl.activate(); } this.onMarkersLayerBeforeFeatureHighlighted = function(ev) { if (ev.olmMarkerId) { if (ev.onMouseOver) { ev.onMouseOver(ev); } this.vMarkersHighlightedLayer.destroyFeatures(); if (ev.attributes) { if (ev.attributes.style) { if (ev.attributes.style.externalGraphicSel) { var p = ev.geometry.clone(); var marker = new OpenLayers.Feature.Vector( p, {}, { externalGraphic: ev.attributes.style.externalGraphicSel, graphicHeight: ev.attributes.style.graphicHeight, graphicWidth: ev.attributes.style.graphicWidth, graphicXOffset: ev.attributes.style.graphicXOffset, graphicYOffset: ev.attributes.style.graphicYOffset, cursor: ev.attributes.style.cursor } ); marker.forMarker = ev; this.vMarkersHighlightedLayer.addFeatures([marker]); } } } } } this.onMarkersLayerFeatureUnhighlighted = function(ev) { if (ev) { if (!ev.olmMarkerId) { if (ev.forMarker) { this.vMarkersHighlightedLayer.destroyFeatures(); } if (ev.forMarker.onMouseOut) { ev.forMarker.onMouseOut(ev); } } else { if (!ev.attributes.style.externalGraphicSel) { if (ev.onMouseOut) { ev.onMouseOut(ev); } } } } } this.onMarkersLayerFeatureSelected = function(ev) { if (ev.onMouseClick) { ev.onMouseClick(ev); } else { if (ev.forMarker.onMouseClick) { ev.forMarker.onMouseClick(ev); } } } this.activateLayerTurist = function() { var z = this.map.getZoom(); if (!this.isOnTurist) { this.isOnTurist = true; this.map.addLayer(OLM.LayerTurist); if (this.isOnCyklo) { this.isOnCyklo = false; this.map.removeLayer(OLM.LayerCyklo); // possible layer removal zoom problem patch if (this.map.getZoom() != z) { this.map.zoomTo(z); } } if (this.isOnIdnes) { this.isOnIdnes = false; this.map.removeLayer(OLM.LayerIdnes); } } } this.activateLayerCyklo = function() { if (!this.isOnCyklo) { this.isOnCyklo = true; this.map.addLayer(OLM.LayerCyklo); if (this.isOnTurist) { this.isOnTurist = false; this.map.removeLayer(OLM.LayerTurist); } if (this.isOnIdnes) { this.isOnIdnes = false; this.map.removeLayer(OLM.LayerIdnes); } } } this.activateLayerIdnes = function() { if (!this.isOnIdnes) { this.isOnIdnes = true; this.map.addLayer(OLM.LayerIdnes); if (this.isOnTurist) { this.isOnTurist = false; this.map.removeLayer(OLM.LayerTurist); } if (this.isOnCyklo) { this.isOnCyklo = false; this.map.removeLayer(OLM.LayerCyklo); } } } this.getMapType = function() { if (this.isOnCyklo) { return 0; } else if (this.isOnTurist) { return 1; } else { return 2; } } this.activateLayerRelief = function() { if (!this.isOnRelief) { this.isOnRelief = true; this.map.addLayer(OLM.LayerRelief); } } this.deactivateLayerRelief = function() { if (this.isOnRelief) { this.isOnRelief = false; this.map.removeLayer(OLM.LayerRelief); } } this.showExtent = function(left, bottom, right, top) { var p1 = this.fromLatLng(new OLM.LatLng(bottom, left)); var p2 = this.fromLatLng(new OLM.LatLng(top, right)); var bnds = new OpenLayers.Bounds(p1.lon, p1.lat, p2.lon, p2.lat); var zoom = this.map.getZoomForExtent(bnds); if (zoom > 8) zoom = 8; this.map.setCenter(bnds.getCenterLonLat(), zoom); } this.getCenter = function(pos, zoom) { return this.getLatLngFromLonLat(this.map.getCenter()); } this.setCenter = function(pos, zoom) { this.map.setCenter(this.fromLatLng(pos), zoom - OLM.zoomOffset); } this.panDirection = function(h, v) { this.map.pan(h * -400, v * -400); } this.panTo = function(latLng) { this.map.panTo(this.fromLatLng(latLng)); } this.getBounds = function() { return this.map.getExtent().transform(this.projection, this.displayProjection).toArray(); } this.getBoundsObj = function() { return this.map.getExtent().transform(this.projection, this.displayProjection); } this.zoomIn = function() { this.map.zoomIn(); } this.zoomOut = function() { this.map.zoomOut(); } this.getZoom = function() { return this.map.getZoom() + OLM.zoomOffset; } this.setZoom = function(lvl) { return this.map.zoomTo(lvl - OLM.zoomOffset); } this.pointFromLatLng = function(pos) { var point = new OpenLayers.Geometry.Point(pos.lng, pos.lat).transform(new OpenLayers.Projection("EPSG:4326"), this.projectionObject); return point; } this.fromLatLng = function(pos) { var lonLat = new OpenLayers.LonLat(pos.lng, pos.lat); lonLat.transform(this.map.displayProjection, this.projectionObject); return lonLat; } this.getLatLngFromLonLat = function(pos) { var ll = pos.transform(this.projection, this.displayProjection); return new OLM.LatLng(ll.lat, ll.lon); } this.getLatLngFromPixel = function(pos) { var ll = this.map.getLonLatFromPixel(pos).transform(this.projection, this.displayProjection); return new OLM.LatLng(ll.lat, ll.lon); } this.initialize(); return this; } OLM.LatLng = function(lat, lng) { this.lat = lat; this.lng = lng; return this; } OLM.NewOLLayerTurist = function() { var layer = new OpenLayers.Layer.TMS('OLLayerTurist', '', {'type':'png', 'getURL':OLM.getLayerTuristURL, 'isBaseLayer':true, 'wrapDateLine': true, resolutions:OLM.resolutions, zoomOffset:OLM.zoomOffset}); return layer; } OLM.NewOLLayerCyklo = function() { var layer = new OpenLayers.Layer.TMS('OLLayerCyklo', '', {'type':'png', 'getURL':OLM.getLayerCykloURL, 'isBaseLayer':true, 'wrapDateLine': true, resolutions:OLM.resolutions, zoomOffset:OLM.zoomOffset}); return layer; } OLM.NewOLLayerIdnes = function() { var layer = new OpenLayers.Layer.TMS('OLLayerIdnes', '', {'type':'png', 'getURL':OLM.getLayerIdnesURL, 'isBaseLayer':true, 'wrapDateLine': true, resolutions:OLM.resolutions, zoomOffset:OLM.zoomOffset}); return layer; } OLM.NewOLLayerRelief = function() { var layer = new OpenLayers.Layer.TMS('OLLayerRelief', '', {'type':'png', 'getURL':OLM.getLayerReliefURL, 'isBaseLayer':false, 'wrapDateLine': true, 'alpha':true, resolutions:OLM.resolutions, zoomOffset:OLM.zoomOffset}); return layer; } OLM.getLayerTuristURL = function(bnds) { var res = this.map.getResolution(); var x = Math.round((bnds.left - this.maxExtent.left) / (res * this.tileSize.w)); var y = Math.round((this.maxExtent.top - bnds.top) / (res * this.tileSize.h)); var z = this.map.getZoom() + this.zoomOffset; return __tt_gettilemapturist({'x':x, 'y':y}, z); } OLM.getLayerCykloURL = function(bnds) { var res = this.map.getResolution(); var x = Math.round((bnds.left - this.maxExtent.left) / (res * this.tileSize.w)); var y = Math.round((this.maxExtent.top - bnds.top) / (res * this.tileSize.h)); var z = this.map.getZoom() + this.zoomOffset; return __tt_gettilemapturistcyklo({'x':x, 'y':y}, z); } OLM.getLayerIdnesURL = function(bnds) { var res = this.map.getResolution(); var x = Math.round((bnds.left - this.maxExtent.left) / (res * this.tileSize.w)); var y = Math.round((this.maxExtent.top - bnds.top) / (res * this.tileSize.h)); var z = this.map.getZoom() + this.zoomOffset; return __tt_gettilemapidnes({'x':x, 'y':y}, z); } OLM.getLayerReliefURL = function(bnds) { var res = this.map.getResolution(); var x = Math.round((bnds.left - this.maxExtent.left) / (res * this.tileSize.w)); var y = Math.round((this.maxExtent.top - bnds.top) / (res * this.tileSize.h)); var z = this.map.getZoom() + this.zoomOffset; return __tt_gettilemaprelief({'x':x, 'y':y}, z); } OLM.initialize = function() { if (!OLM.initialized) { OLM.resolutions = []; OLM.zoomOffset = OLM.minZoom; var res = OLM.maxResolution; for (var zoom = 0; zoom <= OLM.maxZoom; zoom++) { if (zoom >= OLM.minZoom) OLM.resolutions.push(res); res*= 0.5; } OLM.LayerTurist = OLM.NewOLLayerTurist(); OLM.LayerCyklo = OLM.NewOLLayerCyklo(); OLM.LayerIdnes = OLM.NewOLLayerIdnes(); OLM.LayerRelief = OLM.NewOLLayerRelief(); OLM.initialized = true; } } }(OLM));