[GIS] Change the projection at runtime (OpenLayers)

coordinate systemopenlayers-2

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.

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:

I've had similar problems and I've solved it by adding an event listener to the map's baselayerchange event. you would put mapOptionsEtrs properties on the Etrs base layer rather than on the map. Map options are most ignored and overwritten by base layer options.

here is my baselayerchange event handler, which is called with the map as scope.

function onBaseLayerChange(evtObj){
   var mapProj, baseProj, map, newBase, reproject;
   map = this;
   newBase = evtObj.layer;
   mapProj = (map.projection && map.projection instanceof OpenLayers.Projection) ? map.projection : new OpenLayers.Projection(map.projection);
   baseProj = newBase.projection;
   reproject = !(baseProj.equals(mapProj));
   if (reproject) {
      var center, maxExt;
      //calc proper reporojected center
      center = map.getCenter().transform(mapProj, baseProj);
      //calc correct reprojected extents
      maxExt = newBase.maxExtent;
      //set map projection, extent, & center of map to proper values
      map.projection = baseProj;
      map.maxExtent = maxExt;
      map.setCenter(center);
   }
}
Related Question