1

我有一个 wms 图层,它在地图上显示良好,但是当我尝试在单击时从图层中获取对象时,出现错误 getGetFeatureInfoUrl is not a function

    var wmsSource = new TileWMS({
      url: 'http://1.1.1.1:8080/geoserver/wms',
      params: {'LAYERS': 'workspace:feature', 'TILED': true},
      serverType: 'geoserver',
      transition: 0,
    });
    var wmsLayer = new TileLayer({
      source: wmsSource
    })
    var view = new View({
      center: [0, 0],
      zoom: 1,
    });
    if (checked) {
        map.addLayer(wmsLayer);
  }
    else {
      map.removeLayer(wmsLayer)
  }

  map.on('singleclick', function (evt) {
    var viewResolution = /** @type {number} */ (view.getResolution());
    var feature = wmsSource.getGetFeatureInfoUrl(
      evt['coordinate'],
      viewResolution,
      'EPSG:3857',
      {'INFO_FORMAT': 'text/html'}
    );
    if (feature) {
      fetch(feature)
        .then(function (response) { return response.text(); })
        .then(function (info) {
         console.log(info);
          
        });
    }

我该怎么办?

4

1 回答 1

1

解决方案是从层获取源代码,例如:

map.on('singleclick', function (evt) {
    var viewResolution = /** @type {number} */ (view.getResolution());
    var feature = wmsLayer.getSource().getFeatureInfoUrl(
      evt['coordinate'],
      viewResolution,
      'EPSG:3857',
      {'INFO_FORMAT': 'text/html'}
    );
    if (feature) {
      fetch(feature)
        .then(function (response) { return response.text(); })
        .then(function (info) {
         console.log(info);
          
        });
    }
于 2020-09-07T08:53:59.447 回答