I'm trying to show some attributes by popup, following a YT video, but cant seem to get it working. Please help me understand what am doing wrong..(features stored in geoserver)
The code is Index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WebGIS App</title>
<!-- <script src="https://cdn.jsdelivr.net/npm/ol@v8.2.0/dist/ol.js"></script> -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v8.2.0/ol.css">
<link rel="stylesheet" href="main.css">
<!-- <link rel="stylesheet" href="/resources/ol-layerswitcher.css"> -->
<!-- <script src="resources/ol-layerswitcher.js"></script> -->
<link rel="stylesheet" href="https://unpkg.com/ol-layerswitcher@4.1.1/dist/ol-layerswitcher.css" />
</head>
<body>
<div id="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
<script src="main.js"></script>
<script src="https://cdn.jsdelivr.net/npm/ol@v8.2.0/dist/ol.js"></script>
<script src="https://unpkg.com/ol-layerswitcher@4.1.1"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</body>
</html>
main.js
var IndiaTile = new ol.layer.Tile({
title: "India States",
source: new ol.source.TileWMS({
url: "http://localhost:8080/geoserver/WS_Mine/wms",
params: { 'LAYERS': 'WS_Mine:India_State', 'TILED': true },
serverType: 'geoserver',
visible: true,
})
});
// map.addLayer(IndiaTile);
var IndiaTile2 = new ol.layer.Tile({
title: "India Country",
source: new ol.source.TileWMS({
url: "http://localhost:8080/geoserver/WS_Mine/wms",
params: { 'LAYERS': 'WS_Mine:India_Country', 'TILED': true },
serverType: 'geoserver',
visible: true,
})
});
var OverlayGroup = new ol.layer.Group({
title: "Overlay Maps",
fold:true,
layers: [IndiaTile, IndiaTile2]
})
map.addLayer(OverlayGroup);
var container=document.getElementById('popup');
var content=document.getElementById('popup-content');
var closer=document.getElementById('popup-closer');
const popup = new ol.Overlay({
element: container,
autoPan: {
animation: {
duration: 250,
},
},
});
map.addOverlay(popup);
closer.onclick=function(){
popup.setPosition(undefined);
closer.blur();
return false;
};
map.on('singleclick', function(evt) {
content.innerHTML = "";
var resolution = mapView.getResolution();
var url = IndiaTile.getSource().getFeatureInfoUrl(evt.coordinate, resolution, 'EPSG:3857', {
'INFO_FORMAT': 'application/json',
'propertyName': 'state,district'
});
if (url) {
$.getJSON(url, function(data) {
var feature = data.features[0];
var props = feature.properties;
content.innerHTML = "<h3>State: </h3><p>" + props.state.toUpperCase() + "</p>" +
"<h3>District: </h3><p>" + props.district.toUpperCase() + "</p>";
popup.setPosition(evt.coordinate);
})
} else {
popup.setPosition(undefined);
}
});
main.css
.ol-popup {
position: absolute;
background-color:white;
box-shadow: 0 1px 4px rgba(0,0,0,0.2);
padding: 15px;
border-radius: 10px;
border: 1px solid #cccccc;
bottom: 12px;
left: -50px;
min-width: 280px;
}
.ol-popup:after, .ol-popup:before {
top: 100%;
border: solid transparent;
content: " ";
height: 0;
width: 0;
position: absolute;
pointer-events: none;
}
.ol-popup:after {
border-top-color: white;
border-width: 10px;
left: 48px;
margin-left: -10px;
}
.ol-popup:before {
border-top-color:#cccccc;
border-width: 11px;
left: 48px;
margin-left: -11px;
}
.ol-popup-closer {
text-decoration: none;
position: absolute;
top: 2px;
right: 8px;
}
.ol-popup-closer:after {
content:"X";
}