How do I change the projection on an existing Map object? I'd like to allow a user to switch between cylindrical (EPSG:4326) and custom-declared projections with the click of a button.
[GIS] Change the projection at runtime (OpenLayers)
coordinate systemopenlayers-2
Related Solutions
I've been looking into this recently, and this is one of the top search results, I figured i'd add some breadcrumbs to my research and experimentation.
Long and Short:
The 2.X line of Open Layers does not support this. The Map, it's properties such as center, resolution and extents are set by the INITIAL base layer of the map. Change to another base layer DOES NOT adjust these properties, so the outcome is unknown. Often, you get a center point/bounds that are not valid for the base layer you are changing to, so you see white tiles. But that is not ALWAYS the outcome (for example, switching from 4326 to 900913 will bring you to a point close to 0degN,0degE).
But, hope is not lost.
Near term:
Some patches were written against open layers that attempt to patch this for the map and many of the layers (see [1]). This comes from a bug filed over 6 (6!!!) years ago [2]. While you'd have to host your own version of OL and patch it by hand (the patch will not apply cleanly to the latest versions, so you have to manually do the edit), this will get you mostly where you want to be. Very nasty, and doesn't handle everything, but overall, gets someone who NEEDS multiple baselayers with different projections on their feet.
Long term:
This is exactly one of the things that OpenLayers 3.0 is hoping to address[3].
One example is the base layer/overlay dichotomy. It is often a challenge to know how and where to set resolutions, maxExtent, etc. for the map. Projection handling between map and layers and the interplay between projection and properties like maxExtent and center are complicated at best.
The Map object in 2.X relied too heavily on the base layer, and that is going to change going forward. So, if you can hold out until late 2013, there may be a pre-made solution for you to use.
[1] http://trac.osgeo.org/openlayers/attachment/ticket/1249/NEW.Reproject.patch
[2] http://trac.osgeo.org/openlayers/ticket/1249
[3] http://openlayers.org/blog/2012/11/14/why-are-we-building-openlayers-3/
Create a map, with 2 layers. 1 shown (4326) and 1 hidden (3857). On your button click just take the 2nd layer (3857) and set it as the base map. That's it.
This is all assuming, of course, that all of your other functions and calculations are projection-aware. Meaning that at every step you're dealing with objects you are transforming them from the base layer projection to some base one (like 4326).
Now, if you have other non-baselayers drawn, you'll have to redraw them, and if you have vector data, you'll have to r-eproject it and re-draw. And if those layers don't support that project (e.g. the WMS service doesn't support 3857) then you just won't see anything.
Best Answer
Please check out might be helpful for you http://www.peterrobins.co.uk/it/olchangingprojection.html and http://www.geoext.org/pipermail/users/2011-June/002310.html
The GeoExt mailing list post, mentions the following: