1

I have a simple example on http://jsfiddle.net/29bsw/

I have a map with an OSM layer as base layer and an additional vector layer. The projection of the map is defined as EPSG:900913 and the projection of the vector layer is defined as EPSG:4326:

//Map
var map = new OpenLayers.Map("map", {
    projection: new OpenLayers.Projection("EPSG:900913")
});

//Base Layer
var osm = new OpenLayers.Layer.OSM();   
map.addLayer(osm);

//Vector Layer
var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {
    projection: new OpenLayers.Projection("EPSG:4326")
});
map.addLayer(vectorLayer);  

Now I would like to add some features to the vector layer without transforming any geometry point from EPSG:4326 to EPSG:900913. The point should be lie in Hamburg, Germany, but without transforming (see commented on line 21 in the example) the point lies near by Africa.

Because I specified the projection of the map and of the vector layer, is there any way that OpenLayers transform the point from EPSG:4326 to EPSG:900913 automatically?

  • If the basemap is already in EPSG:4326 and if I would like to have the vector layer in EPSG:4326 there is no need to transform, when I set both projections to EPSG:4326? Or I don't understand it correct? – mbulau Mar 5 '14 at 15:29
  • Sorry, I had a typo in my previous comment. Rewording it and will post shortly. – evv_gis Mar 5 '14 at 15:58
2

Updated JSFiddle

The trick is to use preFeatureInsert inside your Vector Layer constructor:

preFeatureInsert: function(feature) {
            feature.geometry.transform(new OpenLayers.Projection("EPSG:4326"),new OpenLayers.Projection("EPSG:900913"))
       }

My earlier comment was incorrect in that I had mistyped what projection the OSM was built in. When I was retyping the edit, I realized a possible answer would be to simply use this parameter.

Essentially, this will take the input data as 4326 and transform it to 900913 for the entire vector layer before adding the data to the map.

| improve this answer | |
  • 1
    No Problem. Great answer, I think this is a simple way to bring the transformation to the "background" with no need to transform every single point. Not sure if this a common way but it's simple. – mbulau Mar 6 '14 at 15:22

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.