Openlayers 3 OL3:通过坐标获取覆盖层
我想通过点击地图来获得不同的覆盖层,这样用户就可以通过弹出窗口选择要获取特征信息的图层。 我正在使用map.forEachFeatureAtPixel,但我只得到一个叠加层Openlayers 3 OL3:通过坐标获取覆盖层,openlayers-3,Openlayers 3,我想通过点击地图来获得不同的覆盖层,这样用户就可以通过弹出窗口选择要获取特征信息的图层。 我正在使用map.forEachFeatureAtPixel,但我只得到一个叠加层 var prue=[]; layers=[] var displayFeatureInfo = function(pixel) { var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) { l=layer.get('nam
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);
});
试着这样做:
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);
});
};
var layers = [];
map.forEachFeatureAtPixel(pixel, function(feature, layer) {
if (layers.indexOf(layer) == -1) {
layers.push(layer);
}
});
功能显示功能信息(evt){
var txt=“”;
olMap.forEachFeatureAtPixel(evt.pixel,函数(特征,源){
//在特征中是层的信息
//在源中是层
//您可以在阵列中保存“源”以获取所有层
var features=feature.getProperties();
getOwnPropertyNames(features).forEach(函数(campo,idx,array){
var valor=特征[campo];
txt+=“+campo+”:“+valor+”
”;
});
var坐标=evt坐标;
content.innerHTML=“信息:
”+txt+“
”;
txt+=“
---------------------------
”;
叠加。设置位置(坐标);
});
};
忽略“覆盖”和“内容”,就像我显示弹出窗口一样,ol.Map#forEachFeatureAtPixel
将使用所提供像素的所有功能调用,直到您返回一个真实值,这是您在代码段中执行的(返回功能
)。因此,要获得所有特性,永远不要返回真实值。函数需要如下所示:
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);
});
};
var layers = [];
map.forEachFeatureAtPixel(pixel, function(feature, layer) {
if (layers.indexOf(layer) == -1) {
layers.push(layer);
}
});
我试图保存源或层,然后循环它们,但我认为问题仍然存在。当我点击两个叠加层时,它只识别其中一个。这可能是因为函数不支持此目的吗?请参见您的“返回特性”,当您返回到一个循环中时,它会中断循环,而不会继续其他层,换句话说,您只有一个1 I操作的循环,我建议将该特性保存到数组中,类似于“var layerinfo={features:features,layers:layer}”然后是“layers.push(layerinfo)”,这样在循环完成后您就可以获得所有层和所有功能