- <html>
- <head>
- <meta charset="utf-8" />
- <meta name="viewport"
- content="initial-scale=1,maximum-scale=1,user-scalable=no" />
-
- <link rel="stylesheet"
- href="https://js.arcgis.com/4.25/esri/themes/light/main.css" />
- <script src="https://js.arcgis.com/4.25/init.js"></script>
-
- <style>
- html,
- body {
- padding: 0;
- margin: 0;
- height: 100%;
- }
-
- #viewDiv {
- float: left;
- width: 100%;
- height: 100%
- }
- </style>
-
- <script>
- require(["esri/Map", "esri/geometry/Extent",
- "esri/geometry/SpatialReference",
- "esri/views/MapView",
- "esri/views/SceneView",
- "esri/layers/WMSLayer",
- "esri/request",
- ], (
- Map, Extent, SpatialReference,
- MapView, SceneView,
- WMSLayer, esriRequest) => {
-
- var wmsLayer = new WMSLayer({
- url: "http://localhost:8090/iserver/services/map/wms/testlayer",
- })
-
- var map = new Map({
- layers: [wmsLayer]
- });
- const sceneView = new SceneView({
- container: "viewDiv",
- map: map
-
- });
-
- sceneView.on("click", async ({ x, y }) => {
- await wmsLayer.load();
- console.log(wmsLayer.featureInfoFormat);
- var layers = "";
- wmsLayer.allSublayers.forEach(function (item, i) {
- console.log(item.name)
- if (item.name != "0") {
- if (item.name == "0.0") {
- layers += item.name;
- } else {
- layers += ',' + item.name;
- }
- }
- })
-
- const { xmin, ymin, xmax, ymax, spatialReference: { latestWkid, wkid } } = sceneView.extent;
- const bbox = `${xmin},${ymin},${xmax},${ymax}`;
- const crs = `EPSG:${latestWkid ?? wkid}`;
- console.log(wmsLayer.featureInfoUrl)
- const { data } = await esriRequest(wmsLayer.featureInfoUrl, {
- query: {
- SERVICE: "WMS",
- LAYERS: layers,
- QUERY_LAYERS: layers,
- REQUEST: "GetFeatureInfo",
- INFO_FORMAT: "application/json",
- FEATURE_COUNT: 20,
- BBOX: bbox,
- CRS: crs,
- SRS: crs,
- X: Math.round(x),
- Y: Math.round(y),
- VERSION: "1.1.1",
- WIDTH: sceneView.width,
- HEIGHT: sceneView.height,
- },
- responseType: "xml"
- });
- const json = xmlToJson(data);
- });
-
- //xml格式数据转json格式数据
- function xmlToJson(xml) {
- var obj = {};
- if (xml.nodeType == 1) {
- if (xml.attributes.length > 0) {
- obj["@attributes"] = {};
- for (var j = 0; j < xml.attributes.length; j++) {
- var attribute = xml.attributes.item(j);
- obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
- }
- }
- } else if (xml.nodeType == 3) {
- obj = xml.nodeValue;
- }
- if (xml.hasChildNodes()) {
- for (var i = 0; i < xml.childNodes.length; i++) {
- var item = xml.childNodes.item(i);
- var nodeName = item.nodeName;
- if (typeof (obj[nodeName]) == "undefined") {
- obj[nodeName] = xmlToJson(item);
- } else {
- if (typeof (obj[nodeName].length) == "undefined") {
- var old = obj[nodeName];
- obj[nodeName] = [];
- obj[nodeName].push(old);
- }
- obj[nodeName].push(xmlToJson(item));
- }
- }
- }
- return obj;
- };
- });
- </script>
- </head>
-
- <body>
- <div id="viewDiv"></div>
- </body>
- </html>
参考文档:WMSLayer | API Reference | ArcGIS Maps SDK for JavaScript 4.28 | ArcGIS Developers