gpt4 book ai didi

javascript - OpenLayers:单击关闭(但不完全)矢量以获取矢量特征

转载 作者:行者123 更新时间:2023-11-29 21:15:40 26 4
gpt4 key购买 nike

我有一个 Open Street Map 和一些出现在道路上的矢量(线)。当我点击矢量时,我可以获得该矢量的特征,但是我只能通过准确点击矢量的像素来实现。有没有一种方法可以让我点击“靠近”矢量(可能偏离几个像素)来获取信息,这样我就不必那么精确了?

代码:

这是我目前在点击矢量时用来获取特征的方法:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com