如何在 Open Layers 3 中动态获取选定的要素属性?
How to get selected features properties dynamically in Open Layers 3?
我在地图中有两种类型,即点和多边形。每种类型都有自己的属性,如 id、doorNo、name 等。我的功能列表由 ;
创建
var features = new ol.format.GeoJSON().readFeatures(geojsonObject, {
featureProjection: 'EPSG:3857'
});
如果我按索引调用功能,我可以使用 get('property_name') 函数获取属性。例如;
features[0].getGeometry().getType() == "Point" // check this feature is Point or not
$doorNo = features[$index].get("doorNo");//this is also works
但同时我的地图上有 select互动。当我 select 一个功能时,我想通过单击事件上的按钮获得所有这些相同的属性。为此,我写了这个;
$('#btnSelected').on('click', function () {
if (selectInteraction) {
// use the features Collection to detect when a feature is selected,
// the collection will emit the add event
var selectedFeatures = selectInteraction.getFeatures();
console.log("Length: " + selectedFeatures.getLength());// this is works
console.log("Coordinates: " + selectedFeatures[0].getGeometry().getCoordinates());//THIS GAVES ME ERROR
}else
console.log('there is no selected feature.');
});
所以,在点击事件中,我想写入控制台所有功能 属性,但即使 selectedFeatures.length 给我正确的数字,我也根本无法获得任何 属性。
我哪里错了?
注意:在地图中,蓝色的是 selected 点。前两个未捕获的错误与此问题无关。
getFeatures
方法 returns 一个 ol.Collection
,不是数组。
ol.Collection
s 不支持使用 JS 括号符号检索其项目。而不是 selectedFeatures[0]
做 selectedFeatures.item(0)
.
我在地图中有两种类型,即点和多边形。每种类型都有自己的属性,如 id、doorNo、name 等。我的功能列表由 ;
创建var features = new ol.format.GeoJSON().readFeatures(geojsonObject, {
featureProjection: 'EPSG:3857'
});
如果我按索引调用功能,我可以使用 get('property_name') 函数获取属性。例如;
features[0].getGeometry().getType() == "Point" // check this feature is Point or not
$doorNo = features[$index].get("doorNo");//this is also works
但同时我的地图上有 select互动。当我 select 一个功能时,我想通过单击事件上的按钮获得所有这些相同的属性。为此,我写了这个;
$('#btnSelected').on('click', function () {
if (selectInteraction) {
// use the features Collection to detect when a feature is selected,
// the collection will emit the add event
var selectedFeatures = selectInteraction.getFeatures();
console.log("Length: " + selectedFeatures.getLength());// this is works
console.log("Coordinates: " + selectedFeatures[0].getGeometry().getCoordinates());//THIS GAVES ME ERROR
}else
console.log('there is no selected feature.');
});
所以,在点击事件中,我想写入控制台所有功能 属性,但即使 selectedFeatures.length 给我正确的数字,我也根本无法获得任何 属性。 我哪里错了?
注意:在地图中,蓝色的是 selected 点。前两个未捕获的错误与此问题无关。
getFeatures
方法 returns 一个 ol.Collection
,不是数组。
ol.Collection
s 不支持使用 JS 括号符号检索其项目。而不是 selectedFeatures[0]
做 selectedFeatures.item(0)
.