我想通过点击地图来获得不同的叠加层,以便用户可以通过弹出窗口选择想要获取要素信息的人。 我正在使用map.forEachFeatureAtPixel,但我得到一个叠加的图层。
var prue=[];
layers=[]
var displayFeatureInfo = function(pixel) {
var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) {
l=layer.get('name');
console.log("CAPA:",l)
layers.push(l);
console.log(layers);
if (layer == rustic_wfs){
var capa= "rustica";
prue[0]=capa;
return feature;};
if (layer == zonas_wfs){
var capa="zonas";
prue[0]=capa;
return feature;}
});
map.on('click', function(evt) {
displayFeatureInfo(evt.pixel);
});
答案 0 :(得分:2)
ol.Map#forEachFeatureAtPixel
将使用所提供的像素处的所有功能进行调用,直到您返回一个真实值,您可以在代码段(return feature
)中执行此操作。因此,要获得所有功能,永远不要返回真正的价值。该函数需要看起来像这样:
var layers = [];
map.forEachFeatureAtPixel(pixel, function(feature, layer) {
if (layers.indexOf(layer) == -1) {
layers.push(layer);
}
});
答案 1 :(得分:1)
尝试这样的事情:
function displayFeatureInfo(evt) {
var txt = "";
olMap.forEachFeatureAtPixel(evt.pixel, function (feature, source) {
//In the feature is the information of layer
//In the source is the layer
//you can save the "source" in you array to get all layers
var features = feature.getProperties();
Object.getOwnPropertyNames(features).forEach(function (campo, idx, array) {
var valor = features[campo];
txt += "<b>" + campo + ":</b> " + valor + "<br />";
});
var coordinate = evt.coordinate;
content.innerHTML = "<p style='padding: 0px'><b>Información:</b></p>" + txt + "<br/>";
txt += "<br/>------------------------<br/><br/>";
overlay.setPosition(coordinate);
});
};
忽略&#34;叠加&#34;和&#34;内容&#34;,这就像我显示弹出窗口