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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>OpenLayers Demo</title>
<style type="text/css">
#map{
width: 600px;
height: 300px;
border: 2px solid black;
padding:0;
margin:0;
}
</style>
<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",
"http://vmap0.tiles.osgeo.org/wms/vmap0", {layers: 'basic'},
{gutter: 15});
map.addLayer(t);
var dist = new OpenLayers.Layer.WMS( "abc","http://localhost:8080/cgi-bin/mapserv.exe?map=C:\\xampp\\htdocs\\mapfile.map",
{
layers: 'parcels',
srs: 'EPSG:4326',
format: "image/png",
isBaseLayer: false,
visibility: true,
}
);
map.addLayer(dist);
map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
map.addControl( new OpenLayers.Control.LayerSwitcher() );
}
</script>
</head>
<body onLoad="init()">
<div id="map"></div>
</body>
</html>
Map File
MAP
NAME parcels_map
STATUS ON
SIZE 420 300
EXTENT 489885 5450946 490904 5451637
UNITS METERS
SHAPEPATH "shapefiles"
IMAGECOLOR 255 255 255
WEB
TEMPLATE "template.html"
IMAGEPATH "C:\\xampp\\htdocs\\output\\"
IMAGEURL "/output/"
METADATA
"wms_title" "WMS Demo Server"
"wms_onlineresource" "http://localhost:8080/cgi-bin/mapserv.exe?map=C:\\xampp\\htdocs\\mapfile.map"
"wms_srs" "EPSG:4326"
"wms_feature_info_mime_type" "text/html"
END
END
LAYER
NAME parcel_boundaries
DATA parcels
STATUS DEFAULT
TYPE POLYGON
CLASS
NAME "parcels_color"
COLOR 255 215 0
END
END
END
Obviously MapServer can generate the map using the given shapefile. I have tested it by entering following URL to the browser.
http://localhost/cgi-bin/mapserv.exe?map=C:\\xampp\\htdocs\\mapfile.map
But, when adding this URL as WMS service URL, it just gives broken image tiles.
Any help would be very much appreciated.
-
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– René Wolferink2013年04月19日 16:25:37 +00:00Commented Apr 19, 2013 at 16:25
3 Answers 3
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/example.map&version=1.3.0&CRS=CRS:84", {layers: ['roads'], transparent: true}, {opacity:0.5},{isBaseLayer: false}, {bbox: '79.9865,7.95594,80.892,8.90883'});
-
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– BradHards2013年06月16日 03:53:50 +00:00Commented Jun 16, 2013 at 3:53
-
1AFAICR you should probably also have
&service=wms
MarkJ– MarkJ2013年08月15日 20:22:53 +00:00Commented Aug 15, 2013 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– Dave X2013年09月13日 18:57:42 +00:00Commented Sep 13, 2013 at 18:57
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 "*"
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.
-
That should make no difference. My current project has separate
map.addLayer
calls and works fine.René Wolferink– René Wolferink2013年04月19日 16:22:25 +00:00Commented Apr 19, 2013 at 16:22