Openlayers WMS - getGetFeatureInfoUrl is not a function

2.8k Views Asked by At

I have a wms layer, it displays fine on the map, but when I try to get an object from the layer when clicked, i get an error 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);
          
        });
    }

what can i do with that?

1

There are 1 best solutions below

0
Ryota228 On BEST ANSWER

The solution is to get source from layer like:

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);
          
        });
    }