简体   繁体   中英

JS OpenLayers get ol/Feature values on click

I am using OpenLayers and I want to get all values from Marker ( ol.Feature ). You can see in docs it is possible to add any value to 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.

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. Is there any 'nicer' solution than feature.values_ ?

You can get all properties using

feature.getProperties()

or if you just need one, you can do

feature.get('name')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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