openlayersgisprojectionopenlayers-3proj4js

How to use OpenLayers 3 with Proj4js


I'm working on a map viewer project and used openlayers 2 before this. Now I have to use OpenLayers 3 and map viewer app should support many different projections because I have wms and wfs layers from different sources and projections. I've found examples that use openlayers2 and proj4js. But I couldn't find explicit example for using ol3 and proj4js. What is your suggestion?


Solution

  • It seems that sometimes, like when using OpenLayers 3 and proj4 in Angular 2 using webpack, one needs to explicitly tell OL3 where to find it:

    import * as proj4 from "proj4";
    import * as ol from "openlayers";
    ...
    ol.proj.setProj4(proj4);
    

    When all is fine, then after defining a projection, ol.proj.get should return it:

    proj4.defs("EPSG:28992", "...");
    if (!ol.proj.get('EPSG:28992')) {
        console.error("Failed to register projection in OpenLayers");
        ...
    }