OpenLayers:单击关闭(但不完全)矢量以获取矢量特征
OpenLayers: Clicking close (but not exactly) on a vector to get vector features
我有一张开放街道地图和一些出现在道路上的矢量(线)。当我点击矢量时,我可以获得该矢量的特征,但是我只能通过准确点击矢量的像素来实现。有没有一种方法可以单击 'near' 矢量(可能偏离几个像素)来获取信息,这样我就不必那么精确了?
代码:
这是我目前在点击矢量时用来获取特征的方法:
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);
});
我有一张开放街道地图和一些出现在道路上的矢量(线)。当我点击矢量时,我可以获得该矢量的特征,但是我只能通过准确点击矢量的像素来实现。有没有一种方法可以单击 'near' 矢量(可能偏离几个像素)来获取信息,这样我就不必那么精确了?
代码:
这是我目前在点击矢量时用来获取特征的方法:
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);
});