簡體   English   中英

openlayers 3獲取所選功能的圖層

[英]openlayers 3 get layer for selected feature

我正在嘗試獲取所選特征的圖層的“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'click'這樣......

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 = '&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()feature.get('name')返回undefined?

在我獲得該功能之后,我想獲得它所在的圖層的“id”。 所以可能在這段代碼中......

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

這可能嗎? 任何幫助是極大的贊賞!!

看看ol.Feature 似乎沒有內置的方法來獲取該功能所在的包含源(或層)。因此,您有兩個選擇,至少我看到它的方式。

首選是保持代碼map.forEachFeatureAtPixel (使用map.forEachFeatureAtPixel ...), 要確保對於每個功能,在調用mySource.addFeature(myFeature)的地方之前,調用set(key, value, opt_silent)鍵將是sourceId (或layerId ),值將是包含源(或層)的標識值。 因此,獲取圖層IDOpenLayers MaGiC將被實現為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);
    ....
}

這樣您就知道每個功能的父源是誰,因為您直接詢問父級。 clickedFeatures1clickedFeatures2是數組,其中一個數組當然可以為空。

至於功能的ID和名稱,該功能在添加時是否具有此類屬性? 如果沒有,在添加功能之前,請按照以下步驟操作:

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

在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM