[GIS] Re-initializing OpenLayers Map with different projection

coordinate systemopenlayers-2

Is it possible to re-initialize OpenLayers Map with a different projection ?

Scenario:

  1. OL Map with projection 4326 is created, along with click, measurement, drawing handlers.

  2. A button to switch to projection 3857 exists

  3. When the button is clicked, we 'ideally' want to re-initialize the map to 3857,
    without re-init all the handlers (click, measurement, drawing etc), because the tiles are are in 3857.

How do I do this?

Best Answer

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.