[GIS] Vector data(point) is drawn in wrong place on map (OpenLayers)

javascriptopenlayers-2pointvector

I need to show a map (world map, used default OpenLayers WMS) and one point on it (with events like onhover, etc). Here is my code:

                   var options = {
                            projection: new OpenLayers.Projection("EPSG:900913"),
                            maxResolution: 6000
                        };
                        map = new OpenLayers.Map('map', options);

                        var wmsLayer = new OpenLayers.Layer.WMS(
                        "OpenLayers WMS", 
                        "http://vmap0.tiles.osgeo.org/wms/vmap0",
                        {layers: 'basic'}
                    ); 

                    var vectors = new OpenLayers.Layer.Vector("Vector Layer");

                    point = new OpenLayers.Geometry.Point(20.088844299316406, 51.8321709083475);
                    vectors.addFeatures([new OpenLayers.Feature.Vector(point)]);

                    map.addLayers([wmsLayer, vectors]);

                    map.zoomToMaxExtent();

But this code locates the point is drawn not in correct place, but somewhere near Africa (that place lat and lon is 0, 0). Question: Why this happens and can I fix it? I just need to locate the point to the correct place. Paradox when I print this point in console then it shows that the point lan and lon are as needed (as defined). But it is still in the wrong place…

Best Answer

You need to translate the point from lat/lon to your map projection:

point = new OpenLayers.Geometry.Point(20.088844299316406, 51.8321709083475);    
point.transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
vectors.addFeatures([new OpenLayers.Feature.Vector(point)]);

This will transform your variable point to the map projection.