Openlayers 3 forEachFeatureAtPixel无法正常工作

Openlayers 3 forEachFeatureAtPixel无法正常工作,openlayers-3,Openlayers 3,我有以下代码, 基本上我有两个图层一个osm图层,一个矢量图层,首先我使用ol.interaction.draw在地图上的两个随机位置单击,将id为1和id为2的两个要素节点添加到矢量图层,然后通过选择“拖动点”(左上角)切换到交互拖动,其思想是,当将两个节点中的任何一个拖动到另一个节点上时,被拖动的节点将变为绿色节点,我可以通过轻松地将节点id 1拖动到节点id 2来实现这一点,但当我尝试将节点id 2拖动到节点id 1时,它要求的精度要比反之高得多。两个节点之间的唯一区别是在节点id 2之前

我有以下代码, 基本上我有两个图层一个osm图层,一个矢量图层,首先我使用ol.interaction.draw在地图上的两个随机位置单击,将id为1和id为2的两个要素节点添加到矢量图层,然后通过选择“拖动点”(左上角)切换到交互拖动,其思想是,当将两个节点中的任何一个拖动到另一个节点上时,被拖动的节点将变为绿色节点,我可以通过轻松地将节点id 1拖动到节点id 2来实现这一点,但当我尝试将节点id 2拖动到节点id 1时,它要求的精度要比反之高得多。两个节点之间的唯一区别是在节点id 2之前创建节点id 1。 我在这里看到的问题是forEachFeatureAtPixel,当将节点id 2拖动到节点id 1上时,它很少检测(或回调很少返回节点id 1)功能节点id 1。 我在这个问题上花了很长时间。还是不知道为什么。非常感谢您的帮助。 谢谢


绘制和修改要素
抽签点
阻力点
var raster=新建ol.layer.Tile({
来源:new ol.source.OSM()
});
var map=新ol.map({
图层:[光栅],
目标:“地图”,
视图:新ol.view({
中心:[-11000000,4600000],
缩放:4
})
});
var circle_style=新ol.style.style({
图片:新ol.style.Circle({
半径:5,
填充:新的ol.style.fill({
颜色:“rgba(255,0,0,2)”
})
})
});
var overlap_style=新ol.style.style({
图片:新ol.style.Circle({
半径:8,
填充:新的ol.style.fill({
颜色:“rgba(0,255,0,2)”
})
})
});
var features=new ol.Collection();
var featureOverlay=新ol.layer.Vector({
来源:新ol.source.Vector({
特色:特色
}),
样式:圆形
});
功能覆盖。设置地图(地图);
window.app={};
var-app=window.app;
/**
*@constructor
*@extends{ol.interaction.Pointer}
*/
app.Drag=函数(){
ol.interaction.Pointer.call(这个{
handleDownEvent:app.Drag.prototype.handleDownEvent,
handleDragEvent:app.Drag.prototype.handleDragEvent,
handleMoveEvent:app.Drag.prototype.handleMoveEvent,
handleUpEvent:app.Drag.prototype.handleUpEvent
});
this.coordinate12=空;
this.cursor='pointer';
this.feature=null;
this.previousCursor uu=未定义;
};
ol.inherits(app.Drag、ol.interaction.Pointer);
app.Drag.prototype.handleDownEvent=函数(evt){
var-map=evt.map;
var feature=map.forEachFeatureAtPixel(evt.pixel,
功能(特征、图层){
返回特性;
});
如果(功能){
log(“down:node_id:+feature.getProperties()['id']);
this.coordinate12=evt.coordinate;
this.feature=特征;
}
返回!!功能;
};
app.Drag.prototype.handleDragEvent=函数(evt){
var-map=evt.map;
fs=featureOverlay.getSource().getFeatures();
var feature=map.forEachFeatureAtPixel(evt.pixel,
功能(特征、图层){
返回特性;
});
如果(!(功能===未定义)){
log(“拖动:节点id:+feature.getProperties()['id']);
//log(“this:+this.feature..getId()+”o:+feature.getId());
if(this.feature\uux.getProperties()['id']!=feature.getProperties()['id'])){
log(“绿色:节点id:+feature.getProperties()['id']);
此.feature_uu.setStyle(重叠_u样式);
}否则{
此.feature_u.setStyle(圆形_u样式);
}
}
var deltaX=evt.坐标[0]-此坐标12_0];
var deltaY=evt.坐标[1]-此坐标12_U1];
var geometry=/**@type{ol.geom.SimpleGeometry}*/
(this.feature_u.getGeometry());
几何。翻译(deltaX,deltaY);
此.coordinate12_u0]=evt.coordinate[0];
此.coordinate12_u1]=evt.coordinate[1];
};
app.Drag.prototype.handleMoveEvent=函数(evt){
if(this.cursor){
var-map=evt.map;
var feature=map.forEachFeatureAtPixel(evt.pixel,
功能(特征、图层){
if(this.feature!=null)
{
log(“move forEachFeatureAtPixel:node_id:+feature.getProperties()['id']);
}
返回特性;
});
var element=evt.map.getTargetElement();
如果(功能){
if(this.feature!=null)
{
log(“move forEachFeatureAtPixel:node_id:+feature.getProperties()['id']);
}
if(element.style.cursor!=this.cursor\uux){
this.previousCursor=element.style.cursor;
element.style.cursor=this.cursor\uux;
}
}else if(this.previousCursor=未定义){
element.style.cursor=this.previousCursor\uux;
this.previousCursor uu=未定义;
}
}
};
/**
*@param{ol.MapBrowserEvent}evt地图浏览器事件。
*@return{boolean}`false`停止拖动序列。
*/
app.Drag.prototype.handleUpEvent=函数(evt){
this.coordinate12=空;
this.feature=null;
返回false;
};
var draw=新ol.interaction.draw({
特色:特色,,
类型:(“点”)
});
var drag=new app.drag();
变量id_计数=0;
在('付款人',功能(e)上提款{
e、 feature.setProperties({
“id”:+id\u计数
})
log(e.feature,e.feature.getProperties());
});
地图。添加交互作用(绘制);
var typeSelect=document.getElementById('type');
typeSelect.value='DrawPoint';
typeSelect.onchange=函数(){
如果(typeSelect.value=='DrawPoint')
{
map.removeInteraction(拖动);
地图。添加交互作用(绘制);
}否则
{
映射。移除交互(绘制);
map.addInteraction(拖动);
}
};
阻止正在移动以从
映射返回的功能(第一个或第二个)。forEachFeatureAtPixel
方法。 请参阅下面的工作代码片段


绘制和修改要素
抽签点
阻力点
var raster=新建ol.layer.Tile({
来源:new ol.source.OSM()
});
var map=新ol.map({
图层:[光栅],
目标:“地图”,
视图:新ol.view({
中心:[-11000000,4600000],
缩放:4
})
});
变量