To apply Proj4js in Openlayers3, you need to reference proj4js in HTML, and then reference the desired projection JS definition, such as Http://epsg.io/21781-1753.js
The coordinate transformation of this epsg:21781 is then supported in Openlayers.
< script src = "http://cdnjs.cloudflare.com/ajax/libs/proj4js/2.2.1/proj4.js" type = "Text/javascript" ></ script > < script src = "Http://epsg.io/21781-1753.js" type ></ script >
Http://epsg.io/21781-1753.js will return a JS, this JS once executed will give PROJ4 to add a supporting projection, as follows:
Proj4.defs ("epsg:21781", "+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=600000 +y_0= 200000 +ellps=bessel +towgs84=660.077,13.551,369.344,2.484,1.783,2.939,5.66 +units=m +no_defs ");
Here is the implementation of Openlayer
/** * fetches a Projection object for the code specified. * * @param {ol.proj.ProjectionLike} projectionlike either a Co De string which is * a combination of authority and identifier such as "epsg:4326", or an * existing projection OB Ject, or undefined. * @return {ol.proj.Projection} Projection object, or null if not in list. * @api Stable*/Ol.proj.get=function(projectionlike) {varprojection; if(Projectionlikeinstanceofol.proj.Projection) {Projection=Projectionlike; } Else if(goog.isstring (projectionlike)) {varCode =Projectionlike; varProjections =Ol.proj.projections_; Projection=Projections[code];
Judge Proj4js was introduced.
if(OL. Enable_proj4js &&!goog.isdef (projection) &&typeofProj4 = = ' function ') {
//If the required code=epsg is introduced, it will Proj4.defs (code) to return the definition and add to the Openlayers vardef =proj4.defs (code); if(Goog.isdef (def)) {varUnits =def.units; if(!Goog.isdef (Units)) { if(Goog.isdef (Def.to_meter)) {units=def.to_meter.toString (); Ol.proj.meters_per_unit[units]=Def.to_meter; }} projection=Newol.proj.Projection ({code:code, units:units, AxisOrientation:def.axis}); Ol.proj.addProjection (projection); varCurrentcode, Currentdef, Currentproj, Proj4transform; for(Currentcodeinchprojections) {Currentdef=proj4.defs (Currentcode); if(Goog.isdef (currentdef)) {currentproj=Ol.proj.get (Currentcode); if(Currentdef = = =def) {Ol.proj.addEquivalentProjections ([currentproj, projection]); } Else{proj4transform=proj4 (Currentcode, code); Ol.proj.addCoordinateTransforms (currentproj, projection, Proj4transform.forward, proj4transform.inverse); } } } } Else{Goog.asserts.assert (goog.isdef (projection)); Projection=NULL; } } } Else{projection=NULL; } returnProjection
Application of PROJ4JS in Openlayers3