簡體   English   中英

JS OpenLayers單擊可獲得ol /功能值

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

我正在使用OpenLayers ,我想從Marker( 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_之外,還有“解決方案”嗎?

您可以使用獲取所有屬性

feature.getProperties()

或者如果您只需要一個,就可以

feature.get('name')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM