简体   繁体   English

openlayers 3-单击多边形边界

[英]openlayers 3 - Click on polygon boundaries

On a map, I have drew a polygon and I want to popup some information when I click on ONLY boundaries of the polygon. 在地图上,我绘制了一个多边形,当我单击多边形的边界时,我想弹出一些信息。 Do you have any solutions to force the selection on boundaries? 您是否有解决方案来强制选择边界?

            var featureContext = new ol.Feature({
                geometry: new ol.geom.Polygon([polyCoords])
            })

            featureContext.setStyle(
                new ol.style.Style({
                    stroke: new ol.style.Stroke({
                        color: context[cc].displayColor,
                        width: 3
                    })
                })
            );

            var layerContext = new ol.layer.Vector({
                source: new ol.source.Vector({
                    features: [featureContext]
                })
            });
            map.addLayer(layerContext);

What I have done right now: 我现在所做的是:

   map.on('singleclick', function(evt) {
        if (evt.dragging) {
          return;
        }
        var feature = map.forEachFeatureAtPixel(map.getEventPixel(evt.originalEvent), function(feature) {
            return feature;
        });
        if (feature) {
           popUp(feature);
        }
    });

But this will select everything on the polygon (area, etc.) and popup informations in the polygon. 但这将选择多边形上的所有内容(区域等)以及多边形中的弹出信息。

Thanks for your help. 谢谢你的帮助。

Not optimal but maybe if you draw the boundaries as a line (in addition to polygon) and only accept if feature is a line ? 不是最佳选择,但是如果您将边界绘制为线(除了多边形),并且仅当特征为线时才接受? Also add an id in the properties of the line which point to the polygon. 还要在指向多边形的线的属性中添加一个id。 There must be some better ways ... but if you have only a few small polygons this is ok. 必须有一些更好的方法...但是,如果您只有几个小多边形,那就可以了。

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

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