[GIS] How to open layer using OpenLayers 2 and GeoServer

geoserveropenlayers-2

I know it's a very basic question but I've been searching for an answer for over 4 hours and I can't get it to work.

I have GeoServer running locally and I'm trying to open a layer in my browser through OpenLayers 2.

This is the code I have:

<html>
<head>
   <title>OpenLayers Example</title>
    <script src="http://openlayers.org/api/OpenLayers.js"></script>
    </head>
    <body>
      <div style="width:100%; height:100%" id="map"></div>
      <script defer="defer" type="text/javascript">
        var map = new OpenLayers.Map('map');
        var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS",
            "http://localhost:8080/geoserver/wms", {layers: 'nurc:nyc_buildings'}     );
        map.addLayer(wms);
        map.zoomToMaxExtent();
      </script>

</body>
</html>

I copied everything exactly like the example in the OpenLayers website, only changed the URL for the layer and the layer workspace/name.

I tried changing the url to "localhost:8080/geoserver/nurc/wms" and every other possible combination, and I can't get it to work. All I can see is the "+" and "-" for zooming in and out of the layer, but the layer is blank, and sometimes it shows only pink tiles.

Firebug does not show any errors.

Any suggestions please?

Best Answer

I guess that your "workspace:layername" combination does not exist. If you open the OpenLayers Preview in Geoserver you will see the layername as a part of the URL like in this example "nurc:ArcSample":

http://localhost:8080/geoserver/nurc/wms?service=WMS&version=1.1.0&request=GetMap&layers=nurc:Arc_Sample&styles=&bbox=-180.0,-90.0,180.0,90.0&width=660&height=330&srs=EPSG:4326&format=application/openlayers

Just try to use exactly the "workspace:layername" combination that you will find in the OL-Preview-URL.