Openlayers:如何使用 onClick 识别点?



在我的开放图层地图中,有一个点列表,它被处理成一个图标列表,然后在地图上正确显示。

var markers = [icon1, icon2, icon3]
var vectorSource = new ol.source.Vector({
features:markers});
var stationLayer = new ol.layer.Vector({
source:vectorSource});
map.addLayer(stationLayer);

在下一步中,我想使地图上的这些点可点击。单击该点时,地图外的表格应显示有关所选点的更多信息。不同的点是通过它们在数组中的位置来定义的。

在阅读了几个示例后,我尝试了以下方法:

map.on('click',function(event){
var coord = event.coordinate;
console.log(coord)
var source = stationLayer.getSource();
var feature = source.getClosestFeaturetoCoordinate(coord);
});

点击事件正确触发,控制台上显示右侧坐标。还存储了一个功能,但我无法判断它是否是一个点以及它是否是正确的。

有什么方法可以从点击点获取标记中的索引?

尝试使用getFeaturesAtCoordinate它将获得几何与提供的坐标相交的所有要素。

....
let features = vectorSource.getFeaturesAtCoordinate(event.coordinate);
if (features.length > 0) {
let data = features[0];
console.log(data.get("NameOfProperty1"));
console.log(data.get("NameOfProperty2"));
});
....

更多关于这里: https://openlayers.org/en/latest/apidoc/module-ol_source_Cluster-Cluster.html#getFeaturesAtCoordinate

最新更新