我正在尝试获取所选功能的图层的“id”,并尝试了3或4种方法来实现这一目标,但尚未实现。
我添加了这样的功能......
angular.forEach(response.FieldList, function (Field, key) {
if (Field.FieldID != "") {
var shape = response.FieldList[key].Shape;
shape = shape.replace('}', ',"id":' + '"' + Field.FieldID + '"' + '}');
var geoJsonObj = {
'type': 'Feature',
'geometry': JSON.parse(shape),
'name': Field.FieldID,
'id': Field.FieldID
}
var vectorSource = new ol.source.Vector({
features: (new ol.format.GeoJSON()).readFeatures(geoJsonObj)
});
Fields[Field.FieldID] = new ol.layer.Vector({
projection: 'EPSG:4269',
source: vectorSource,
id: Field.FieldID,
name: 'Fields',
style: function (feature, resolution) {
var text = resolution * 100000 < 10 ? response.FieldList[key].Acres : '';
if (text != "") {
styleCache[text] = [new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#319FD3',
width: 1
}),
text: new ol.style.Text({
font: '12px Calibri,sans-serif',
text: text,
fill: new ol.style.Fill({
color: '#000'
}),
stroke: new ol.style.Stroke({
color: '#fff',
width: 3
})
}),
fill: new ol.style.Fill({
color: rcisWebMapUtilities.convertHex(response.FieldList[key].Shade, '0.5')
})
})];
}
else if (text == "") {
styleCache[text] = [new ol.style.Style({
fill: new ol.style.Fill({
color: rcisWebMapUtilities.convertHex(response.FieldList[key].Shade, '0.5')
})
})
]
} return styleCache[text];
}
});
webMapValues.vectorFieldLayer.push(Fields[Field.FieldID])
webMapValues.fieldValues.push({
color: response.FieldList[key].Shade,
plantingName: response.FieldList[key].CropNickName,
acres: response.FieldList[key].Acres,
cropId: response.FieldList[key].CropID,
cropNumber: response.FieldList[key].CropNumber,
fieldID: response.FieldList[key].FiledID,
fieldName: response.FieldList[key].FieldName,
legalDesc: response.FieldList[key].LegalDesc,
policyNum: response.FieldList[key].PolicyNumber
})
var found = $filter('filter')(webMapValues.legend, { plantingName: response.FieldList[key].CropNickName }, true);
if (found == 0) {
webMapValues.legend.push({
color: response.FieldList[key].Shade,
plantingName: response.FieldList[key].CropNickName
})
}
}
});
你可以看到我试图在许多地方设置“id”...甚至改变GeoJSON以包含'id'但它似乎被某种方式丢弃而且当我想使用它时不存在?
我正在使用map.on'点击'这样......
map.on('click', function (evt) {
var pixel = map.getEventPixel(evt.originalEvent);
displayFeatureInfo(evt.pixel, evt.coordinate);
//var coordinate = evt.coordinate;
})
和此代码执行突出显示...
var highlight;
var displayFeatureInfo = function (pixel,coordinate) {
var feature = map.forEachFeatureAtPixel(pixel, function (feature) {
var id = Opelayers magic to get layer id;
return feature;
});
var info = document.getElementById('info');
if (feature) {
info.innerHTML = feature.getId() + ': ' + feature.get('name');
} else {
info.innerHTML = ' ';
}
if (feature !== highlight) {
if (highlight) {
featureOverlay.getSource().removeFeature(highlight);
}
if (feature) {
featureOverlay.getSource().addFeature(feature);
document.getElementById('popup-content').innerHTML = '<p>It is working</p>';
popup.setPosition(coordinate);
}
highlight = feature;
}
};
feature.getId()
和feature.get('name')
返回undefined?
获得该功能后,我想获得它所在图层的“id”。 所以可能在这段代码中......
var feature = map.forEachFeatureAtPixel(pixel, function (feature) {
var id = Opelayers magic to get layer id;
return feature;
});
这可能吗?非常感谢任何帮助!!
答案 0 :(得分:0)
看看ol.Feature。似乎没有内置的方法来获取该功能所在的包含源(或层)。因此,您有两个选择,至少我看到它的方式。
首选是保持代码不变(使用map.forEachFeatureAtPixel
...),但确保每个功能都在您调用mySource.addFeature(myFeature)
的地方之前,您拨打set(key, value, opt_silent)
,其中密钥为sourceId
(或layerId
),该值将是您包含的源(或图层)的标识值。因此 OpenLayers MaGiC获取图层ID 将具体化为feature.get('layerId')
。
第二种选择是,而不是使用map.forEachFeatureAtPixel
,考虑使用以下内容:
// When there is a single click on the map.
map.on('singleclick', function(evt) {
// Get all features at the event's coordinate for mySource1 and for mySource2 separately.
var clickedFeatures1 = mySource1.getFeaturesAtCoordinate(evt.coordinate);
var clickedFeatures2 = mySource2.getFeaturesAtCoordinate(evt.coordinate);
....
}
这样您就知道每个功能的父源是谁,因为您直接询问父级。 clickedFeatures1
和clickedFeatures2
是数组,其中一个数组当然可以为空。
对于功能的ID和名称,该功能在添加时是否具有此类属性?如果没有,在添加功能之前,请按照以下步骤进行操作:
myFeature.setId(42);
myFeature.set('name', 'foo');
mySource.addFeature(myFeature);
答案 1 :(得分:0)
在openlayers 4中,我能够获得每个所选功能的图层,如下所示: (我不确定这是否适用于OL3)
var infoClicker = map.on('click', function(evt) {
map.forEachFeatureAtPixel(evt.pixel,
function(feature, layer) {
var idLayer = layer.get('myLayerID');
请参阅http://openlayers.org/en/latest/apidoc/ol.Map.html#forEachFeatureAtPixel