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

I need to add a wms layer to my OpenLayers map and it should be generated by MapServer installed locally.

OpenLayers HTML file

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>OpenLayers Demo</title>
<style type="text/css">
width: 600px; 
height: 300px; 
border: 2px solid black;
<script type="text/javascript" src="openlayers/lib/OpenLayers.js"></script>
<script type="text/javascript">

function init() {
var lon = 80.409;
var lat = 8.329;
var zoom = 14;
var map = new OpenLayers.Map("map");

var t = new OpenLayers.Layer.MapServer( "OpenLayers WMS", 
                "", {layers: 'basic'},
                {gutter: 15});

var dist = new OpenLayers.Layer.WMS( "abc","http://localhost:8080/cgi-bin/mapserv.exe?map=C:\\xampp\\htdocs\\",

                    layers: 'parcels',
                    srs: 'EPSG:4326',
                    format: "image/png",
                    isBaseLayer: false,
                    visibility: true,


map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
map.addControl( new OpenLayers.Control.LayerSwitcher() );   

<body onLoad="init()">
<div id="map"></div>

Map File

# To enter comments into a mapfile add a # symbol at the beginning of the line

NAME parcels_map
SIZE 420 300
EXTENT 489885 5450946 490904 5451637
SHAPEPATH "shapefiles"
IMAGECOLOR 255 255 255

TEMPLATE "template.html"
IMAGEPATH "C:\\xampp\\htdocs\\output\\"
IMAGEURL "/output/"
    "wms_title" "WMS Demo Server"
    "wms_onlineresource" "http://localhost:8080/cgi-bin/mapserv.exe?map=C:\\xampp\\htdocs\
\"    "wms_srs" "EPSG:4326" "wms_feature_info_mime_type"  "text/html"

NAME parcel_boundaries
DATA parcels
NAME "parcels_color"
COLOR 255 215 0

# end of map file

Obviously MapServer can generate the map using the given shape file. I have tested it by entering following URL to the browser.


But, when adding this URL as wms service URL, it just gives broken image tiles.

Any help would be very much appreciated.

share|improve this question
By "it just gives broken image tiles", what do you mean? Can you inspect the requests going to your mapserver and try to open one yourself? If there is a problem with mapserver, it will show a message rather than an image. An empty image also indicates a proper answer. – René Wolferink Apr 19 '13 at 16:25

When I added WMS version and CRS parameters to the request URL, it worked just fine.

Now it looks something like following.

var my_layer = new OpenLayers.Layer.WMS("Main Roads", "http://localhost/cgi-bin/mapserv.exe?map=C:/xampp/htdocs/sarisara/", {layers: ['roads'], transparent: true}, {opacity:0.5},{isBaseLayer: false}, {bbox: '79.9865,7.95594,80.892,8.90883'});
share|improve this answer
Can you show this (in code) for those who might need to fix a similar problem in the future. You can just edit this answer to add the extra details. Then you may like to mark this question as "answered" – BradHards Jun 16 '13 at 3:53
AFAICR you should probably also have &service=wms – MarkJ Aug 15 '13 at 20:22
The third argument to OpenLayers.Layer.WMS() contain the key:value parameters that get added to the URL as '&key=value' strings, so you can either put them in the param argument or in the URL. This also means that your isBasLayer and visibility are part of the URL passed to mapserver, and not part of the OpenLayers.Layer properties. – Dave X Sep 13 '13 at 18:57

I don´t know if it helps, but try to add your layers to the map in one line, like this:

 map.addLayers([t, dist ]);

instead of two separate lines.

share|improve this answer
That should make no difference. My current project has separate map.addLayer calls and works fine. – René Wolferink Apr 19 '13 at 16:22

I could not remember where I read it, but you may need to add the following line to your .map file in WEB -> METADATA section.

wms_enable_request "*"
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.