添加4个图层,注意每个图层的属性设置:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>创建一个简单的电子地图</title> <script type="text/javascript" src="code/OpenLayers.js"></script> </head> <body onLoad="init()"> <div id="map_element" style="width:500px; height:500px"> </div> <script type="text/javascript"> var map; var wms_base; function init(){ map=new OpenLayers.Map('map_element',{}); wms_base=new OpenLayers.Layer.WMS( 'Base Layer', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers:'basic'}, {isBaseLayer:true} ); var wms_labels=new OpenLayers.Layer.WMS( 'Location Labels', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers:'clabel,ctylabel,statelabel', transparent:true}, {visibility:false,opacity:.5} ); var wms_state_line=new OpenLayers.Layer.WMS( 'state line layer', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers:'stateboundary',transparent:true}, {displayInLayerSwitcher:false,minScale:13841995.078125} //是否在Control中显示 ); var wms_water_depth=new OpenLayers.Layer.WMS( 'water depth', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers:'depthcontour',transparent:true}, {opacity:.8} ); var wms_roads=new OpenLayers.Layer.WMS( 'roads', 'http://vmap0.tiles.osgeo.org/wms/vmap0', {layers:'priroad,secroad,rail',transparent:true}, {transistionEffect:'resize'} //这个不知道 ); map.addLayers([wms_base,wms_labels,wms_state_line,wms_water_depth,wms_roads]); map.addControl(new OpenLayers.Control.LayerSwitcher({})); if(map.getCenter()){ map.zoomToMaxExtent(); } } </script> </body> </html>
效果展示: