4

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;
}
12

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])

  • Thanks so much @fidelfisch, I hadn't read that anywhere and stupidly I didn't think to try. That's working perfectly now. :) – dvmac01 Sep 22 '15 at 14:33
  • Good to hear that's working. Enjoy the map :-) – fidelfisch Sep 22 '15 at 15:55
5

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.