More

Switching to a different projection based on zoom level

Switching to a different projection based on zoom level


I have a map on my Geoserver, which I am displaying via wms and Openlayers. I am using a projection that is optimal for the whole area. However, when zooming in I would like to switch to a different projection which has proved to be best for a particular locality.

How can I achieve this? Any examples out there?


The methodology would be to detect a zoom change and apply a updated view with the new projection to the map.

First you have to detect the change of the resolution:

map.getView().on('change:resolution', changeProjection);

Secondly you look if you have reached the desired zoom level and then apply the projection:

var changeProjection = function() { var zoomLevel = map.getView().getZoom(); // look if zoom level is reached if (zoomLevel > 15) { //define new view and apply to map map.setView(new ol.View({ center: map.getView().getCenter(), zoom: zoomLevel, projection: newProjection /* typeof == ol.proj.ProjectionLike */ }); }

Note: The code is untested and you have to set variable newProjection to make it work.