【问题标题】:Openlayers: How do I identify Points with onClick?Openlayers:如何使用 onClick 识别点?
【发布时间】:2019-09-26 19:13:00
【问题描述】:

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

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);
});

点击事件被正确触发,正确的坐标显示在控制台上。还存储了一个特征,但我不知道它是否是一个点以及它是否是正确的。

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

【问题讨论】:

  • markers.indexOf(feature) 呢?
  • 如果您将其声明为 Feature (new Feature()),您可以查看其名称,例如..
  • 你也可以(稍微)改变点击特征的样式,从用户体验的角度来看这也是一个很好的做法,这样用户也可以100%确定点击了哪个点跨度>
  • 谢谢 Rob,这解决了我的问题!图标的更改已经实现并且正在工作(另一种颜色的稍大的图标)所以我没有提到;)

标签: javascript openlayers


【解决方案1】:

尝试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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多