简体   繁体   English

openlayers 3获取所选功能的图层

[英]openlayers 3 get layer for selected feature

I'm trying to get the "id" of the layer for a selected feature and have tried maybe 3 or 4 methods for achieving this but have yet to achieve it. 我正在尝试获取所选特征的图层的“id”,并尝试了3或4种方法来实现这一目标,但还没有实现它。

I add my features like this... 我添加这样的功能......

 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                          
                          })
                      }
                }
            });

as you can see I'm trying to set the "id" in many places...even altering the GeoJSON to include 'id' but it seems to get discarded somehow and is not there when I want to use it? 你可以看到我试图在许多地方设置“id”...甚至改变GeoJSON以包含'id'但它似乎被丢弃了某种方式并且当我想使用它时不存在?

I am using a map.on 'click" like this... 我正在使用map.on'click'这样......

map.on('click', function (evt) {
        var pixel = map.getEventPixel(evt.originalEvent);
        displayFeatureInfo(evt.pixel, evt.coordinate);

        //var coordinate = evt.coordinate;


    })

and this code to perform the highlight... 并且此代码执行突出显示...

 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 = '&nbsp;';
        }

        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() and feature.get('name') return undefined? feature.getId()feature.get('name')返回undefined?

after I get the feature I would like to get the "id" of the layer it is on. 在我获得该功能之后,我想获得它所在的图层的“id”。 so probably in this code... 所以可能在这段代码中......

var feature = map.forEachFeatureAtPixel(pixel, function (feature) {
            var id = Opelayers magic to get layer id;
            return feature;
        });

Is this possible? 这可能吗? any help is greatly appreciated!! 任何帮助是极大的赞赏!!

Look at ol.Feature . 看看ol.Feature There does not seem to be a built-in way to get the containing source (or layer) that the feature is in. So, you have two choices, at least the way I see it. 似乎没有内置的方法来获取该功能所在的包含源(或层)。因此,您有两个选择,至少我看到它的方式。

First choice is to keep your code as is (using map.forEachFeatureAtPixel ...) but make sure that for each feature, before the place that you call mySource.addFeature(myFeature) , you call set(key, value, opt_silent) where the key would be sourceId (or layerId ) and the value would be your identifying value for the containing source (or layer). 首选是保持代码map.forEachFeatureAtPixel (使用map.forEachFeatureAtPixel ...), 要确保对于每个功能,在调用mySource.addFeature(myFeature)的地方之前,调用set(key, value, opt_silent)键将是sourceId (或layerId ),值将是包含源(或层)的标识值。 So the OpenLayers MaGiC to get Layer ID would be materialized as feature.get('layerId') . 因此,获取图层IDOpenLayers MaGiC将被实现为feature.get('layerId')

The second choice is, instead of using map.forEachFeatureAtPixel , to consider using something along the lines of: 第二种选择是,而不是使用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);
    ....
}

This way you know who the parent source is for each feature, because you ask the parent directly. 这样您就知道每个功能的父源是谁,因为您直接询问父级。 clickedFeatures1 and clickedFeatures2 are arrays, either one of which could, of course, be empty. clickedFeatures1clickedFeatures2是数组,其中一个数组当然可以为空。

As for the feature's ID and name, does the feature have such attributes when it is added? 至于功能的ID和名称,该功能在添加时是否具有此类属性? If not, before adding the feature, go along the lines of this: 如果没有,在添加功能之前,请按照以下步骤操作:

myFeature.setId(42);
myFeature.set('name', 'foo');
mySource.addFeature(myFeature);

In openlayers 4, I am able to get the layer of each selected feature like this: (Im not sure if this works in OL3) 在openlayers 4中,我能够得到每个所选功能的图层:(我不确定这是否适用于OL3)

var infoClicker = map.on('click', function(evt) {
    map.forEachFeatureAtPixel(evt.pixel,
        function(feature, layer) {
            var idLayer = layer.get('myLayerID');

See http://openlayers.org/en/latest/apidoc/ol.Map.html#forEachFeatureAtPixel http://openlayers.org/en/latest/apidoc/ol.Map.html#forEachFeatureAtPixel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM