我有一个开放的街道地图和一些矢量(线)出现在道路上。当我单击矢量时,我能够获得该矢量的特征,但是我只能通过完全单击该矢量的像素来实现。有没有一种方法可以单击“接近”向量(可能偏离几个像素)来获取信息,所以我不必那么精确?

码:

这是我单击矢量时当前用于获取功能的内容:

var displayFeatureInfo  = function (pixel, coordinate) {
            var features = [];
            map.forEachFeatureAtPixel(pixel, function (feature, layer) {
                features.push(feature); // Pushes each feature found into the array
            });

            if (features.length > 0) {   // If there are one or more features
                $("#popup").html('<object data=' + "http://URLToLoadDataFrom '/>'); // Load the data using jQuery
                popup.setPositioning('top-left');
                popup.setPosition(coordinate);
                container.style.display = 'block';
            } else {
                container.style.display = 'none';
            }

        };

        map.on('click', function (evt) {
            var coordinate = evt.coordinate;
            displayFeatureInfo(evt.pixel, coordinate);
        });


提前致谢。

最佳答案

您可以在提供的像素之外构建范围,并使用该范围而不是单个点来进行特征选择。认为,这意味着您必须使用vector.getSource().forEachFeatureIntersectingExtent方法而不是map.forEachFeatureAtPixel

检查此(fiddle here):

var displayFeatureInfo  = function (pixel, coordinate) {
            var features = [];
            //this is the offset in pixels. Adjust it to fit your needs
            var pixelOffSet = 5;
            var pixelWithOffsetMin = [pixel[0]-pixelOffSet,pixel[1]+pixelOffSet];
            var pixelWithOffsetMax = [pixel[0]+pixelOffSet,pixel[1]-pixelOffSet];
            var XYMin =map.getCoordinateFromPixel(pixelWithOffsetMin)
            var XYMax =map.getCoordinateFromPixel(pixelWithOffsetMax)
            var extent = XYMax.concat(XYMin);
            var extentFeat= new ol.Feature(new ol.geom.Polygon([[
            [extent[0],extent[1]],
            [extent[0],extent[3]],
            [extent[2],extent[3]],
            [extent[2],extent[1]],
            [extent[0],extent[1]]
            ]]));

            vector.getSource().forEachFeatureIntersectingExtent(extentFeat.getGeometry().getExtent(),
            function (feature) {
                features.push(feature); // Pushes each feature found into the array
            });

            if (features.length > 0) {   // If there are one or more features
                console.log("features found on offset clicked",features)
               // container.style.display = 'block';
            } else {
              console.log("no features on offset click")
            }

        };

        map.on('click', function (evt) {
            var coordinate = evt.coordinate;
            displayFeatureInfo(evt.pixel, coordinate);
        });

关于javascript - OpenLayers:在矢量上单击关闭(但不完全)以获取矢量特征,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39509418/

10-09 23:00
查看更多