Change Projection in OpenLayers Map

deamon picture deamon · Apr 20, 2010 · Viewed 13.9k times · Source

I want to set "EPSG:4326" as the projection of an OpenLayers map, but when I try it, I always get "EPSG:900913".

function init() {

    var options = {
            projection: new OpenLayers.Projection("EPSG:4326")  // ignored
    };

    map = new OpenLayers.Map('map', options);

    var layer = new OpenLayers.Layer.OSM.Osmarender("Osmarender");
    map.addLayer(layer);

    ...

    alert(map.getProjection());  // returns "EPSG:900913"

    ...

}

The base map is open street map.

How can I set the Projection to EPSG:4326?

Answer

user405914 picture user405914 · Jul 29, 2010

As milovanderlinden has pointed out, you have to transform latitude/longitude values (as used in Google or Bing maps) before the can be applied in OpenStreetMap layers likeOsmarenderorMapnik:

var datapoint = new OpenLayers.LonLat(-71.0, 42.0);
var proj_1 = new OpenLayers.Projection("EPSG:4326");
var proj_2 = new OpenLayers.Projection("EPSG:900913");
datapoint.transform(proj_1, proj_2);