1

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

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

我可以使用该叠加层上的样式功能更改笔触颜色等。这一切都很好,直到我做下一次下降。

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

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

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

4

2 回答 2

0

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

它有帮助吗?祝你好运。

于 2014-11-17T15:37:47.120 回答
0

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

var myFeature = new ol.Feature({
        geometry: ..., 
        labelPoint: ..,
        name:...,
        customProp1: ...,
        customProp2: ...,
        myCustomID: myRandomIDGenerator()
      })
于 2016-01-26T09:36:36.580 回答