簡體   English   中英

OpenLayers:單擊關閉(但不完全)矢量以獲取矢量特征

[英]OpenLayers: Clicking close (but not exactly) on a vector to get vector features

我有一個開放街道地圖和一些出現在道路上的矢量(線)。 當我點擊矢量時,我能夠獲得該矢量的特征,但是我只能通過精確點擊矢量的像素來做到這一點。 有沒有辦法可以單擊“靠近”矢量(可能相差幾個像素)來獲取信息,這樣我就不必那么精確了?

代碼:

這是我目前用來在單擊矢量時獲取特征的方法:

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

檢查這個(在這里小提琴):

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

暫無
暫無

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

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