【问题标题】:Ol3 unique identifier for a featureOl3 特征的唯一标识符
【发布时间】:2016-04-23 22:28:08
【问题描述】:

我正在尝试创建一些自定义地图。由于拖放功能,我正在使用 ol3。我们的想法是能够为地图上的每个要素设置样式。

我拖放从 JOSM 导出的 .gpx 和 .json 文件,并为每个功能创建一个独特的叠加层。

我可以使用该叠加层上的样式功能更改笔触颜色等。在我进行下一次投放之前,一切都很好。

删除的功能似乎以某种随机顺序出现,并与上一个删除的功能穿插在一起。我需要有一些方法来判断该放置操作中哪些新功能是新的,这样我就可以在不影响我已经设置样式的情况下对这些功能进行样式设置。

我可以从该功能中获得某种唯一标识符吗? 有没有办法可以用唯一的 id 标记功能?

我尝试了 feature.getId() 但在拖放事件触发时未定义。

【问题讨论】:

    标签: javascript openlayers-3


    【解决方案1】:

    您可以以 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']);
        })
    

    有帮助吗?祝你好运。

    【讨论】:

      【解决方案2】:

      创建功能时,您可以将具有自定义属性的对象传递给构造函数。例如:

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

      【讨论】:

        猜你喜欢
        • 2011-02-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-17
        • 2012-07-18
        • 1970-01-01
        • 2014-03-05
        • 2012-06-02
        相关资源
        最近更新 更多