Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
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? –  marco235 Mar 5 at 15:29
    
Sorry, I had a typo in my previous comment. Rewording it and will post shortly. –  evv_gis Mar 5 at 15:58
add comment

1 Answer

up vote 1 down vote accepted

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.

share|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. –  marco235 Mar 6 at 15:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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