whitenoisedb
whitenoisedb

Reputation: 403

How can I know which projection code a layer is using?

I've spent a lot of time realizing this:

var view = new ol.View({

    //this doesn't work
    //center: [-73.979681,40.7033127],

    //this works
    center: ol.proj.transform([-73.979681,40.7033127], 'EPSG:4326', 'EPSG:3857'),

    zoom: 8
});

I've found on OpenLayer's quickstart how to transform a projection from one to another. However, that isn't very clear on another tutorials. I'm not used to those specific projection codes, neither which layer uses which.

Is there a list where I can know which projection code a layer uses?

Upvotes: 0

Views: 186

Answers (1)

erilem
erilem

Reputation: 2664

By default, when no projection is specified in the options passed to the ol.View constructor, the view projection is Web Mercator (whose EPSG code is EPSG:3857).

You can do map.getView().getProjection() to get the view projection. And you call getCode on the returned projection object to get its code.

To transform coordinates from lon/lat to Web Mercator you need to use the following:

 var coords = ol.proj.transform([lon, lat], 'EPSG:4326', 'EPSG:3857');

OpenLayers v3.5.0, due next week, will make this a bit more convenient, with a fromLonLat function.

Upvotes: 1

Related Questions