[GIS] Transform projection from EPSG:4326 into EPSG:900913

geodjangogeojsonopenlayers-2postgis

.

i've got this problem about projection. .i just wanted to know what am i missing. .

i already displayed map base layer using openlayers through

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

i also displayed my vector layer through

var layer = new OpenLayers.Layer.Vector("Butuan");
    map.addLayer(layer);
    var format = new OpenLayers.Format.GeoJSON();
    var feat = format.read({{my_geojson|safe}});
    layer.addFeatures(feat);

and my position is

map.setCenter(new OpenLayers.LonLat(125.54915811889646, 8.90855351100384),11);

which positions the view as i desired it to be.
when i tried to change my base layer to

base_layer =  new OpenLayers.Layer.OSM("OpenStreetMap");

the center of the map is redirected to another location. .i guess that this problem pertains to the projection. i tried to change the center into

map.setCenter(new OpenLayers.LonLat(125.54915811889646, 8.90855351100384),11).transform(geographic, mercator);

which is defined as

var geographic = new OpenLayers.Projection("EPSG:4326");
       var mercator = new OpenLayers.Projection("EPSG:900913");

no changes has been noticed. .the position of the center is still redirected to another location. .

Can anyone suggest solutions on how to solve this problem?

I am using django framework. . vectorformats and simplejson to convert the retrieved spatial data into geojson from postgis. .

Best Answer

The correct call of the map center should be:

           var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
            map.setCenter (lonLat, zoom);

At least this is how it works for me.

If your vector data is not in EPSG:4326, you have to reproject that too. The tiles are in EPSG:900913, and you should not change that.