I am able to fetch the getGetFeatureInfoUrl for the currently selected layer, is it possible to fetch the last added layer FeatureInfo on the map?
My code currently is:
<!DOCTYPE html>
<html>
<head>
<title>WMS GetFeatureInfo (Tile Layer)</title>
<link rel="stylesheet" href="https://openlayers.org/en/v3.19.1/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v3.19.1/build/ol.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
</head>
<body>
<div class="form-group">
<label>Disaster</label>
<select id="dict" class="form-control" onchange="loadfloodlayer(this.id);">
<option>Select disaster</option>
<option value="guntur">guntur</option>
<option value="krishna">krishna</option>
<option value="prakasam">prakasam</option>
</select>
</div>
<div id="map" class="map"></div>
<div id="info"> </div>
<div id="info2"> </div>
<script>
var wmsSource = new ol.source.TileWMS( {
url: 'http://ndem.nrsc.gov.in/geoserver/wms',
params: {'LAYERS': 'ndem50k:stateadmin50census2011'}
});
var wmsLayer = new ol.layer.Tile({
source: wmsSource
});
var mapcenter = [79.60,23.44];
var view = new ol.View({
projection: 'EPSG:4326',
center: mapcenter,
zoom: 5,
extent: [66.2329, 7.68083, 98.2223, 39.03874],
maxZoom:16,
minZoom: 4
});
var map = new ol.Map({
layers: [wmsLayer],
target: 'map',
view: view
});
function loadfloodlayer(idd1) {
hint = "manjula:";
hitn = hint + $('#dict').val();
alert(hitn);
var railvar = new ol.source.TileWMS({
url: 'http://localhost:8089/geoserver/manjula/wms',
params: {LAYERS: hitn}
});
var wmsLayer = new ol.layer.Tile({
source: railvar
});
map.on('singleclick', function(evt) {
document.getElementById('info').innerHTML = '';
document.getElementById('info2').innerHTML = '';
var viewResolution = /** @type {number} */ (view.getResolution());
var url2 = wmsSource.getGetFeatureInfoUrl(
evt.coordinate, viewResolution, 'EPSG:4326',
{'INFO_FORMAT': 'text/html'});
var url = railvar.getGetFeatureInfoUrl(
evt.coordinate, viewResolution, 'EPSG:4326',
{'INFO_FORMAT': 'text/html'});
if (url) {
document.getElementById('info').innerHTML =
'<iframe style="width:100%;height:100%;" src="' + url + '"></iframe>';
}
if (url2) {
document.getElementById('info2').innerHTML =
'<iframe style="width:100%;height:100%;" src="' + url2 + '"></iframe>';
}
});
map.on('pointermove', function(evt) {
if (evt.dragging) {
return;
}
var pixel = map.getEventPixel(evt.originalEvent);
var hit = map.forEachLayerAtPixel(pixel, function() {
return true;
});
map.getTargetElement().style.cursor = hit ? 'pointer' : '';
});
map.addLayer(wmsLayer);
}
</script>
</body>
</html>