简体   繁体   English

JS OpenLayers单击可获得ol /功能值

[英]JS OpenLayers get ol/Feature values on click

I am using OpenLayers and I want to get all values from Marker ( ol.Feature ). 我正在使用OpenLayers ,我想从Marker( ol.Feature )获取所有值。 You can see in docs it is possible to add any value to 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();

I have a click event on map and I want to get those values. 我在地图上有一个click事件,我想获取这些值。

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

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

It looks like ol.Feature has no method for getting these values. 看起来像ol.Feature没有获取这些值的方法。 Is there any 'nicer' solution than feature.values_ ? 除了feature.values_之外,还有“解决方案”吗?

You can get all properties using 您可以使用获取所有属性

feature.getProperties()

or if you just need one, you can do 或者如果您只需要一个,就可以

feature.get('name')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM