问题
使用leaflet.wms.js插件,我只需单击WMS层就可以显示关于WMS层的信息(使用GetFeatureInfo)。问题是geoserver只提供纯文本的数据,如下图所示,它是一片混乱。
因此,我想过滤GetFeatureInfo查询的结果,以便只显示有用的信息。我编写了一堆JavaScript行,它过滤了包含GetFeatureInfo请求结果的<div>。
var GetFeatureInfo = document.getElementsByClassName("leaflet-popup-content")[0].innerHTML;
tipo = GetFeatureInfo.split(/'/)[21];
legenda = GetFeatureInfo.split(/'/)[27];
document.getElementsByClassName("leaflet-popup-content")[0].innerHTML = tipo + ":<br/>PERICOLOSITÀ " + legenda;我试图在脚本的底部添加这些行,女巫调用并配置映射,但是它没有工作。我想这些行不是在正确的时候执行的。
溶液
多亏了塞巴斯蒂安·舒尔茨,我成功地过滤了GetFeatureInfo查询的结果。我们需要扩展L.WMS.Source类,并使用钩子showFeatureInfo编辑类在弹出窗口中显示GetFEatureInfo的方式。如下所示:
var CleanInfoSource = L.WMS.Source.extend({
'showFeatureInfo': function(latlng, info){
if (!this._map){return;}
tipo = info.split(/'/)[21];
legenda = info.split(/'/)[27];
info = tipo + ":<br/>PERICOLOSITÀ " + legenda;
this._map.openPopup(info, latlng);
}
});
var minambPAI = new CleanInfoSource("http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/WMS_v1.3/Vettoriali/PAI_pericolosita.map",{
format: "image/png",
transparent: true,
attribution: "<a href='http://www.pcn.minambiente.it'>Ministero dell’Ambiente</a>",
info_format: "text/plain"
}
);正如塞巴斯蒂安所说,这种方法(以及其他方法)在文档中。我还发现钩子语法在leaflet.wms.js脚本中。我想是的..。:)
发布于 2017-09-18 10:58:24
根据传单WMS 文档,您需要扩展类L.WMS.Source并覆盖钩子(例如showFeatureInfo)。检查此代码段并编辑信息变量,以生成自定义弹出。
var map = L.map('map').setView([43.53, 10.32], 13);
var openTopoMap = L.tileLayer(
'http://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
{attribution: '<a href="https://opentopomap.org/copyright">OpenTopoMap</a>'}).addTo(map);
var MySource = L.WMS.Source.extend({
'showFeatureInfo': function(latlng, info) {
if (!this._map) {
return;
}
// do whatever you like with info
console.log(info)
this._map.openPopup(info, latlng);
}
});
var minambPAI = new MySource("http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/WMS_v1.3/Vettoriali/PAI_pericolosita.map",
{
format: "image/png",
transparent: true,
attribution: "<a href='http://www.pcn.minambiente.it'>Ministero dell’Ambiente</a>",
info_format: "text/plain"
}
);
var periAlluvioneMME = minambPAI.getLayer('RN.PAI.PERICOLOSITA.ALLUVIONE').addTo(map);
var periFranaMME = minambPAI.getLayer('RN.PAI.PERICOLOSITA.FRANA_01');
var control = L.control.layers({}, {
'Pericolosità Alluvioni: moderata a molto elevata': periAlluvioneMME,
'Pericolosità Frane: moderata a molto elevata': periFranaMME
})
control.addTo(map);https://stackoverflow.com/questions/46268753
复制相似问题