[GIS] OpenLayers: Problems adding WMS layer to OSM

geoserveropenlayers-2openstreetmapwms

I need to create a map with two layers. The first layer (basemap) is the OSM layer, and the second layer is my custom GeoServer WMS layer. This layer is in EPSG:4326 projection. Is this possible?

I have read many posts but I didn't solve my problem. I don't find this GeoServer layer in my map.

map = new OpenLayers.Map('map'{projection:"EPSG:900913",
                               maxResolution:2319.89824519781,
                               units:'m' }); 

osm = new OpenLayers.Layer.OSM( "Simple OSM Map"); 
wms = new OpenLayers.Layer.WMS( "OpenLayers WMS", "95.172.21.2:8080/geoserver/wms";,
                                 {layers: 'asl.caserta:distretti_4326',trasparent: true},
                                 {isBaseLayer: false}); 
map.addLayers([osm, wms]); 
map.setCenter( new OpenLayers.LonLat(14.22231, 41.26700).transform( new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject() ), 12 ); –

Best Answer

There are a couple of issues with your code. Firstly, the map was not properly initialized. The Url of WMS service, as well as the transparent paramter was incorrect.

I have corrected the code. The following works for me:

var map, layer;
function init(){
    var geographic = new OpenLayers.Projection("EPSG:4326");
    var mercator = new OpenLayers.Projection("EPSG:900913");

    map = new OpenLayers.Map( 'map',{projection: mercator} );


    var osm = new OpenLayers.Layer.OSM( "Simple OSM Map"); 
    map.addLayer(osm);
    layer = new OpenLayers.Layer.WMS("WMS", "http://95.172.21.2:8080/geoserver/wms",
                         {layers: 'asl.caserta:distretti_4326',transparent: true},
                         {isBaseLayer: false}); 
    map.addLayer(layer);
    map.setCenter(new OpenLayers.LonLat(14.22231, 41.26700).transform(geographic,mercator), 10); 
}