簡體   English   中英

Ol3功能的唯一標識符

[英]Ol3 unique identifier for a feature

我正在嘗試創建一些自定義地圖。 由於拖放功能,我正在使用ol3。 想法是能夠為地圖上的每個要素設置樣式。

我拖放了從JOSM導出的.gpx和.json文件,並為每個功能創建了一個唯一的疊加層。

我可以使用該疊加層上的樣式功能來更改筆觸顏色等。 一切正常,直到我下一滴。

刪除的功能似乎以某種隨機順序出現,並與上一刪除中的功能相互穿插。 我需要某種方法來說明該拖放操作中的哪些新功能,以便我可以對這些功能進行樣式設置而不會影響已經設置樣式的功能。

我可以從功能中獲得某種唯一的標識符嗎? 有沒有一種方法可以標記具有唯一ID的功能?

我嘗試過feature.getId(),但在拖放事件觸發時未定義。

您可以以json格式定義功能

var geoSource = new ol.source.GeoJSON({
    /* @type {olx.source.GeoJSONOptions} */
    "projection": "EPSG:3857"  //us
    , "object": {
        "type": "FeatureCollection"
        , "crs": { "type": "name", "properties": { "name": "EPSG:4326" } }//'EPSG:3857'//euro 'urn:ogc:def:crs:EPSG::4326'//'urn:ogc:def:crs:OGC:1.3:CRS84'
        , "features": [
          {
              "type": "Feature", "id": "01"
              , "geometry": { "type": "Point", "coordinates": [-80.0874386, 26.7816928] }
              , "properties": { "myproperty": "West Palm Beach" }
          }
        , {
            "type": "Feature", "id": "02"
            , "geometry": { "type": "Point", "coordinates": [-82.0838700, 26.9262480] }
            , "properties": { "myproperty": "Punta Gonda" }
        }
        ]
    }
});

然后,您可以通過

    var ff = geoSource.getFeatureById('02');
    alert(ff.getProperties()['myproperty']);

或者如果您需要分析所有功能,則可以

    geoSource.getFeatures().forEach(function (ff) {
        alert(ff.getProperties()['myproperty']);
    })

有幫助嗎? 祝好運。

創建特征時,可以將具有自定義屬性的對象傳遞給構造函數。 例如:

var myFeature = new ol.Feature({
        geometry: ..., 
        labelPoint: ..,
        name:...,
        customProp1: ...,
        customProp2: ...,
        myCustomID: myRandomIDGenerator()
      })

暫無
暫無

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

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