Home > Software engineering >  Issue in Adding WMS layer in the JavaScript API
Issue in Adding WMS layer in the JavaScript API

Time:10-26

I am working on to add a Web Map Service (WMS). Map services offer access to the contents of a map hosted on a server.

Map services can expose different levels of capabilities.

But after going through all the code. I am unable to fetch my WMS layers to the map.

And my map is only showing basemap and the wms layers are not showing at all.

Do somebody help me to find out, what went wrong?

Below is my code:

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
  <title>Map with WMS</title>

  <link rel="stylesheet" href="https://js.arcgis.com/3.38/dijit/themes/claro/claro.css">
  <link rel="stylesheet" href="https://js.arcgis.com/3.38/esri/css/esri.css">
  <style>
    html, body, #map {
      height: 100%;
      width: 100%;
      margin: 0;
      padding: 0;
    }
  </style>
  <script src="https://js.arcgis.com/3.38/"></script>

  <script>
    var map;
    require([
      'esri/map', 'esri/layers/WMSLayer', 'esri/layers/WMSLayerInfo', 'esri/geometry/Extent',
      'dojo/_base/array', 'dojo/dom', 'dojo/dom-construct', 'dojo/parser',
      'dijit/layout/BorderContainer', 'dijit/layout/ContentPane', 'dojo/domReady!'
    ], function(Map, WMSLayer, WMSLayerInfo, Extent, array, dom, domConst, parser) {
      parser.parse();

      map = new Map('map', {
        basemap: "streets-vector",
        center: [-96, 37],
        zoom: 3
      });

      var layer1 = new WMSLayerInfo({
        name: '1',
        title: 'Rivers'
      });
      var layer2 = new WMSLayerInfo({
        name: '2',
        title: 'Cities'
      });
      var resourceInfo = {
        extent: new Extent(-126.40869140625, 31.025390625, -109.66552734375, 41.5283203125, {
          wkid: 4326
        }),
        layerInfos: [layer1, layer2]
      };
      var wmsLayer = new WMSLayer('https://sampleserver1.arcgisonline.com/ArcGIS/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/WMSServer', {
        resourceInfo: resourceInfo,
        visibleLayers: ['1', '2']
      });
      map.addLayers(wmsLayer);

      var details = dom.byId('details');
      domConst.place('<b>Layers</b>:', details);
      var ul = domConst.create('ul', null, details);
      array.forEach(wmsLayer.layerInfos, function(layerInfo) {
        domConst.create('li', { innerHTML: layerInfo.title }, ul);
      });
      domConst.place('<b>WMS Version</b>:'   wmsLayer.version   '<br />', details);
    });
  </script>
</head>

<body class="claro">
  <div id="content"
       data-dojo-type="dijit.layout.BorderContainer"
       data-dojo-props="design:'headline', gutters:true"
       style="width: 100%; height: 100%; margin: 0;">

    <div id="details"
         data-dojo-type="dijit.layout.ContentPane"
         data-dojo-props="region:'left', splitter:true"
         style="overflow:auto; width:200px;">
    </div>

    <div id="map"
         data-dojo-type="dijit.layout.ContentPane"
         data-dojo-props="region:'center'"
         style="overflow:hidden;">
    </div>

  </div>
</body>
</html>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

The addLayers() method takes an array - see https://developers.arcgis.com/javascript/3/jsapi/map-amd.html#addlayers

If you compare with the original sample at https://developers.arcgis.com/javascript/3/sandbox/sandbox.html?sample=layers_wmsresourceinfo, it looks like you've changed map.addLayers([wmsLayer]); to map.addLayers(wmsLayer);

Or change it to use the singular method addLayer().

CodePudding user response:

Instead of

map.addLayers(wmsLayer);

in your code , use

map.addLayers([wmsLayer]);

It will solve your issue.

  • Related