slevin
slevin

Reputation: 3886

set the projection of map in openlayers 3

I just want to convert the default projection of an Openlayers 3.9.0 from the default EPSG:3857 to EPSG:4326.

So I edited a basic code like

var layer = new ol.layer.Tile({
    source: new ol.source.OSM()
});

var center = [-1.812, 52.443];

var proj = new ol.proj.Projection({
        code: 'EPSG:4326',
        units: 'm'
});

var view = new ol.View({
    center: center,
    zoom: 6,
    projection:proj
});


var map = new ol.Map({
    loadTilesWhileAnimating: false,
    loadTilesWhileInteracting:false,
    target: 'map',
    layers: [layer],
    view: view

});

If center is like var center = [-1.812, 52.443]; it does not go in the UK, as is should be, it goes in the center of the map.

If I do like var center = new ol.geom.Point(-1.812, 52.443); I see no map at all. What am I missing here?

Thanks

Upvotes: 2

Views: 8408

Answers (2)

Alvin Lindstam
Alvin Lindstam

Reputation: 3142

You have two issues:

You should not instantiate the EPSG:4326 projection by yourself, it's done by OpenLayers 3. You get the projection by calling ol.proj.get('EPSG:4326').

The ol.source.OSM source loads it's tiles from services that only support EPSG:3857. Since it's a XYZ-based tilesource, you might actually get the map working (if the tilecoords are valid), but the layer will not be positioned correctly and still be in EPSG:3857. You can use EPSG:4326 as the view projection, but then you have to use a background map that supports it.

A working demo can be found in the official examples.

Upvotes: 3

Bob Holmes
Bob Holmes

Reputation: 626

OL does not currently transform tiles, but that is being worked.

https://github.com/openlayers/ol3/issues/3785

Upvotes: 1

Related Questions