JS OpenLayers 在点击时获得 ol/Feature 个值

JS OpenLayers get ol/Feature values on click

我正在使用 OpenLayers,我想从标记 (ol.Feature) 中获取所有值。 您可以在 docs 中看到可以将任何值添加到 ol.Feature.

import Feature from 'ol/Feature';
import Polygon from 'ol/geom/Polygon';
import Point from 'ol/geom/Point';

var feature = new Feature({
  geometry: new Polygon(polyCoords),
  labelPoint: new Point(labelCoords),
  name: 'My Polygon' // <--- CUSTOM VALUE
});

// get the polygon geometry
var poly = feature.getGeometry();

// Render the feature as a point using the coordinates from labelPoint
feature.setGeometryName('labelPoint');

// get the point geometry
var point = feature.getGeometry();

我在地图上有一个 click 事件,我想获取这些值。

this.map.on('click', (args) => {

        this.map.forEachFeatureAtPixel(args.pixel, (feature, layer) => {
            // do something
            console.log(feature.values_); // <---- SEEMS LIKE 'PRIVATE' prop
        });
    });

看起来 ol.Feature 没有获取这些值的方法。有没有比 feature.values_ 更 'nicer' 的解决方案?

您可以使用

获取所有属性
feature.getProperties()

或者如果你只需要一个,你可以

feature.get('name')