[OpenLayers-Users] Re: Change layer on max zoom level

Bennos benjaminpreisig at gmx.at
Wed Feb 1 11:05:04 EST 2012


Hi,

I found a solution for this problem, so if anyone ever needs it:

I have to say that i use only the zoom-in and zoom-out buttons for which i
created a panel like in http://openlayers.org/dev/examples/panel.html

I am changing the trigger-behaviour of my zoom-in button to check whether my
base layer is at its maximum zoom level and then change the base layer to
another layer with more zoom levels

--------------------------------------------------------------------------------------------------------
//variable to save the zoom plus one, assigned in the zoomend event
var zoomplus=0;
map.events.register('zoomend',this, function(){zoomplus=map.getZoom()+1;});
var panel = new OpenLayers.Control.Panel();	//in my case i am using a panel
and showing only the zoomin and zoomout button
panel.addControls([ 
	new OpenLayers.Control.ZoomIn({
			title:'Zoom in',
			//overwrite default trigger
			trigger: function(){
			       	 //osm is my layer with max zoom level: 18, image is with max
zoom level: 20
				 if
(this.map.baseLayer==osm_tms&&zoomplus==19){this.map.setBaseLayer(this.map.getLayersByName("image")[0]);}
				  this.map.zoomIn();
				    }
			}),
	new OpenLayers.Control.ZoomOut({title:"Zoom out"}) 
]); 
map.addControl(panel);

--------------------------------------------------------------------------------------------------------

To get this behaviour also on double click and mousewheel you have  to add
the if also the Navigation-Control:

--------------------------------------------------------------------------------------------------------
map.addControl(new OpenLayers.Control.Navigation({
		defaultDblClick: function (evt) {
	        if
(this.map.baseLayer==osm&&zoomplus==19){this.map.setBaseLayer(this.map.getLayersByName("image")[0]);}
	        var newCenter = this.map.getLonLatFromViewPortPx( evt.xy ); 
	        this.map.setCenter(newCenter, this.map.zoom + 1);
    	},
    	wheelChange: function(evt, deltaZ) {
	        if
(this.map.baseLayer==osm&&zoomplus==19){this.map.setBaseLayer(this.map.getLayersByName("image")[0]);}
	        var currentZoom = this.map.getZoom();
	        var newZoom = this.map.getZoom() + Math.round(deltaZ);
	        newZoom = Math.max(newZoom, 0);
	        newZoom = Math.min(newZoom, this.map.getNumZoomLevels());
	        if (newZoom === currentZoom) {
	            return;
	        }
	        var size    = this.map.getSize();
	        var deltaX  = size.w/2 - evt.xy.x;
	        var deltaY  = evt.xy.y - size.h/2;
	        var newRes  = this.map.baseLayer.getResolutionForZoom(newZoom);
	        var zoomPoint = this.map.getLonLatFromPixel(evt.xy);
	        var newCenter = new OpenLayers.LonLat(
	                            zoomPoint.lon + deltaX * newRes,
	                            zoomPoint.lat + deltaY * newRes );
	        this.map.setCenter( newCenter, newZoom );
	    }
	}));

--------------------------------------------------------------------------------------------------------

Cheers
Benjamin

--
View this message in context: http://osgeo-org.1560.n6.nabble.com/Change-layer-on-max-zoom-level-tp3918789p4356682.html
Sent from the OpenLayers Users mailing list archive at Nabble.com.


More information about the Users mailing list