Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to overlay a wms layer from geoserver (localhost)in Open layers?

What is the basic syntax of Geoserver WMS Layer?

Details are as follows:

workspace name: india
layer name:hospitals

 <script defer="defer" type="text/javascript">
 var map = new OpenLayers.Map('map'); 
 var wms = new OpenLayers.Layer.WMS( "OpenLayers WMS", "";, {layers: 'basic'} );
 var twms = new OpenLayers.Layer.WMS( "World Map", "localhost:8080/geoserver/wms? service=wms& version=1.1.1& request=GetCapabilities", 
     map: 'hospitals', 
     transparent: 'true', 
     layers: 'hospitals'
 } ); 
 map.addLayers([wms, twms]);
share|improve this question
what have you tried so far? Have you tried this sample: – Devdatta Tengshe Apr 23 '13 at 9:07
Ya i tried the basic wms layer, now i want to overlay my layers from geoserver . what me to do? – satya chandra Apr 23 '13 at 9:10
please post the code that you have already used, and let us know where you are stuck right now. – Devdatta Tengshe Apr 23 '13 at 9:20
Please edit your question and put it there, instead of in a comment. Also, what is the url of your geoserver? – Devdatta Tengshe Apr 23 '13 at 9:32
up vote 4 down vote accepted

Your OpenLayers.Layer.WMS statements should look like this:

basemap = new OpenLayers.Layer.WMS( "Layer Name1",
    "http://localhost:8080/geoserver/wms/workspacename", {layers: 'workspacename:layername'} );\
utility = new OpenLayers.Layer.WMS( "Layer Name2",
    "http://localhost:8080/geoserver/wms/workspacename", {layers: 'workspacename:layername', transparent:"true"}, {isBaseLayer:false} );
share|improve this answer
i want add one layer, is it sufficient the follow code? basemap = new OpenLayers.Layer.WMS( "Layer Name1", "localhost:8080/geoserver/wms/workspacename";, {layers: 'workspacename:layername'} ); – satya chandra Apr 23 '13 at 10:37
thanks i got it, successfully added wms from my geo server – satya chandra Apr 23 '13 at 10:43

Please consider the version of the your API. Also define suitable projection for your layer.

var layer2 = new OpenLayers.Layer.WMS(maplayer.displayName,
                { layers: [2], version: '1.3.0' ,'srs'},
                  {singleTile: true,
                      isBaseLayer: false,
                      projection: "EPSG:102113"


share|improve this answer

It should be enough removing '? service=wms& version=1.1.1& request=GetCapabilities' from the url parameter of the layer declaration

share|improve this answer

Test WMS

<link rel="stylesheet" href: "" type="text/css"  />   
<!-- <link rel="stylesheet" href: "C:/ol3_samples/assests/css/samples.css" type="text/css" /> -->" >

var wms_source = new ol.source.TileWMS({
 url: 'http://localhost:6080/geoserver/Jharkhand/wms',
 params: {
          'LAYERS': 'Jharkhand:Jharkhand_block_boundary1'
var wms_layer = new ol.layer.Tile({
   source:  wms_source

var base_map = new ol.layer.Tile({
   source: new ol.source.MapQuest({layer: 'osm'})

    var layers = [

    var vw=new ol.View({
      center: ol.proj.transform([85.6082,23.7175], 'EPSG:4326', 'EPSG:3857'),

     var map= new ol.Map({
     target: 'map',
     layers: layers,
     view: vw

share|improve this answer

Your Answer


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.