More

Convert Lon Lat to XY in Openlayers 3

Convert Lon Lat to XY in Openlayers 3


I have a map which retrieves Points of Interest from a database and displays them however the Lon Lat need to be transformed into XY to be placed properly. Currently the all just appear at O,O and I believe they have been transformed to decimal degrees.

I have tried using ol.proj.transform but this returns undefined. Do I have to create a calculation to convert these or is there a method to do this? The result of this function is returned to a vector layer which loads the features onto a map, albeit in the wrong position.

There are many examples using Openlayers 2 but the syntax is completely different. My non-working example is below:

/* Features conversion */ function wsToCollection(routeData) { //console.log(routeData); var fc = new Object; fc.type = "FeatureCollection"; fc.features = []; for (var i = 0; i < routeData.Points.length; i++) { var feature = new Object; feature.type = "Feature"; //console.log(feature); feature.geometry = new Object; feature.geometry.type = "Point"; feature.geometry.coordinates = []; var Lon = routeData.Points[i].Longitude; //var LonTrans = ol.proj.transform(Lon, 'EPSG:3857', 'EPSG:4326'); // not working console.log(LonTrans); var Lat = routeData.Points[i].Latitude; //var LatTrans = Lat.transform('EPSG:3857', 'EPSG:4326'); // also not working feature.geometry.coordinates[0] = routeData.Points[i].Longitude; feature.geometry.coordinates[1] = routeData.Points[i].Latitude; feature.properties = new Object; feature.properties.name = routeData.Points[i].Description; feature.properties.Guid = routeData.Points[i].Guid; feature.properties.LongId = routeData.Points[i].LongId; fc.features.push(feature); } return fc; }

When you transform you always transform pair of points:

ol.proj.transform([23.4, 42.5], 'EPSG:4326','EPSG:3857');

(this transforms from EPSG 4326 to EPSG 3857; the coordinates are [lon, lat])


I was looking for this function too, and I have found it in some of the examples on their site.

You can useol.proj.fromLonLat([19.062072, 47.473478]).

http://openlayers.org/en/v3.13.1/apidoc/ol.proj.html#.fromLonLat


Watch the video: OpenLayers Add a Layer