1

I created an interactive map using Leaflet, when I click on my layers I can get data in popup, my problem is that when I click on the map where there are no features it shows a blank popup. I want to block this popup when it's blank.

This my code:

map.addEventListener('click', onMapClick);
popup = new L.Popup({maxWidth: 1000});
function onMapClick(e) {
var latlngStr = '(' + e.latlng.lat.toFixed(3) + ', ' +         e.latlng.lng.toFixed(3) + ')';
var BBOX =         map.getBounds()._southWest.lng+","+map.getBounds()._southWest.lat+","+map.getBounds()._northEast.lng+","
+map.getBounds()._northEast.lat;
var WIDTH= map.getSize().x;
var HEIGHT = map.getSize().y;
var X = map.layerPointToContainerPoint(e.layerPoint).x;
var Y = map.layerPointToContainerPoint(e.layerPoint).y;
var layerObj = {};
layerObj["test_layer:EV_Lot_ALHOUDA_wgs"] = alhouda6_layer;
layerObj["test_layer:Imm_Coll_Lot_ALHOUDA_wgs84"] =alhouda7_layer;
layerObj["test_layer:Villa_Lot_ALHOUDA_wgs84"] = alhouda8_layer; 
layerObj["test_layer:Eco_Lot_ALHOUDA_wgs84"] =alhouda5_layer;
layerObj["test_layer:Equip_Lot_ALHOUDA_wgs84"] = alhouda4_layer; 
layerObj["test_layer:Rec_Lot_ALHOUDA_wgs84"] =alhouda3_layer;
layerObj["test_layer:Parking_Lot_ALHOUDA_wgs84"] = alhouda2_layer; 
layerObj["test_layer:P200Log_Lot_ALHOUDA_wgs84"] =alhouda1_layer;

layerObj["test_layer:R3_Lot_HFOUNTY_wgs84"] =founty1_layer;
layerObj["test_layer:R5_Lot_HFOUNTY_wgs84"] =founty2_layer;    
layerObj["test_layer:R9_Lot_HFOUNTY"] =founty3_layer;
layerObj["test_layer:R15_Lot_HFOUNTY_wgs84"] =founty4_layer;
layerObj["test_layer:R1_Lot_HFOUNTY_wgs84"] =founty5_layer;
layerObj["test_layer:R2_Lot_HFOUNTY_wgs84"] =founty6_layer; 
layerObj["test_layer:Equip_Lot_HFOUNTY_wgs84"] =founty7_layer;  
 queryLayers = [];
for (key in layerObj) { 
if(map.hasLayer(layerObj[key])) {
 queryLayers.push(key);
 }
}
var urlLayers = queryLayers.join();

var URL = 'http://localhost:8082/geoserver/test_layer/wms?SERVICE=WMS&VERSION=1.1.1&REQUEST=GetFeatureInfo&LAYERS=test_layer:Imm_Coll_Lot_ALHOUDA_wgs84,test_layer:Villa_Lot_ALHOUDA_wgs84,test_layer:Eco_Lot_ALHOUDA_wgs84,test_layer:Equip_Lot_ALHOUDA_wgs84,test_layer:Rec_Lot_ALHOUDA_wgs84,test_layer:EV_Lot_ALHOUDA_wgs,test_layer:P200Log_Lot_ALHOUDA_wgs84,test_layer:Parking_Lot_ALHOUDA_wgs84,R3_Lot_HFOUNTY_wgs84,R5_Lot_HFOUNTY_wgs84,R9_Lot_HFOUNTY,R15_Lot_HFOUNTY_wgs84,R1_Lot_HFOUNTY_wgs84,R2_Lot_HFOUNTY_wgs84,Equip_Lot_HFOUNTY_wgs84&QUERY_LAYERS='+urlLayers+'&propertyName=Zone_Urb,Type_Urb&STYLES=&BBOX='+BBOX+'&FEATURE_COUNT=5&HEIGHT='+HEIGHT+'&WIDTH='+WIDTH+'&FORMAT=image%2Fpng&INFO_FORMAT=text%2fhtml&SRS=EPSG%3A4326&X='+X+'&Y='+Y;
 popup.setLatLng(e.latlng);
popup.setContent("<iframe src='"+URL+"' width='400' height='100' frameborder='0'></iframe>");
if (urlLayers)

map.openPopup(popup); 

enter image description here enter image description here

1

You would like to check if you received results, the array urlLayers exists, but you wanna check its length.
Try changing the condition if(urlLayers) - > if(urlLayers.length > 0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.