I am trying to convert WGS84 coordinates (EPSG:4326) to Amersfoort RD New (EPSG:28992) with OpenLayers 3.
I declared EPSG28992:
Proj4js.defs["EPSG:28992"] = "+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +units=m +no_defs";
Got the projection from OL:
var EPSG28992 = new ol.proj.Projection('EPSG:28992');
Now I defined my transform function as:
function transformLatLong(lattitude, longitude)
{
var coordinates = ol.proj.transform([longitude, lattitude], 'EPSG:4326', EPSG28992);
console.log(coordinates);
}
The problem is that ol.proj.transform returns just the same long lat coordinates (i.e. did not transform anything).
What am I missing here?
Best Answer
You are using Proj4js 1.x series declaration whereas now OpenLayers 3 is using Proj4js 2.x series. For this reason, your projection is never registered within OpenLayers 3 and your coordinates reprojection fails.
More an addition that a direct answer but I'm wondering why you want to create a function for a one liner in OpenLayers 3 (but I don't know your context) whereas you can use
ol.proj.fromLonLat
function.You will find below a sample including two approaches in one (through
<script>
tag or with a code declaration at the top of your own code)