如何在鼠标移动之前使创建的绘图可见
How to make created Draw visible before mouse is moving
有没有办法让刚刚创建的 Draw 交互在第一个鼠标鼠标事件之前可视化自己?
我使用 openlayers 4.6.5。应用程序创建一个 ol.interaction.Draw 来响应键盘事件。类型是 LineString。
第一次鼠标移动后,预期的圆圈显示在鼠标位置。
我的问题是,在用户第一次移动鼠标之前,Draw 交互不会显示任何内容。
交互是由鼠标事件驱动的,因此您需要重新运行最后一个事件。
为您的地图添加 pointermove
个事件的监听器并保存最后一个
var lastEvent;
map.on('pointermove', function(event){ lastEvent = event; });
然后当您添加交互时,让它处理最后一个事件
map.addInteraction(draw);
draw.handleEvent(lastEvent);
版本 4.6.4 适合我。 5 秒后添加交互,然后删除并以 5 秒和 10 秒的间隔添加新交互:
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var source = new ol.source.Vector();
var vector = new ol.layer.Vector({
source: source
});
var map = new ol.Map({
layers: [raster, vector],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
var draw; // global so we can remove it later
function addInteraction() {
draw = new ol.interaction.Draw({
source: source,
type: 'LineString'
});
map.addInteraction(draw);
}
var lastEvent;
map.on('pointermove', function(event){ lastEvent = event; });
var add = false;
setInterval(function(){
add = !add;
if (add) {
addInteraction();
draw.handleEvent(lastEvent);
} else {
map.removeInteraction(draw);
}
}, 5000);
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.4/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.4/ol.js"></script>
<div id="map" class="map"></div>
有没有办法让刚刚创建的 Draw 交互在第一个鼠标鼠标事件之前可视化自己?
我使用 openlayers 4.6.5。应用程序创建一个 ol.interaction.Draw 来响应键盘事件。类型是 LineString。 第一次鼠标移动后,预期的圆圈显示在鼠标位置。 我的问题是,在用户第一次移动鼠标之前,Draw 交互不会显示任何内容。
交互是由鼠标事件驱动的,因此您需要重新运行最后一个事件。
为您的地图添加 pointermove
个事件的监听器并保存最后一个
var lastEvent;
map.on('pointermove', function(event){ lastEvent = event; });
然后当您添加交互时,让它处理最后一个事件
map.addInteraction(draw);
draw.handleEvent(lastEvent);
版本 4.6.4 适合我。 5 秒后添加交互,然后删除并以 5 秒和 10 秒的间隔添加新交互:
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var source = new ol.source.Vector();
var vector = new ol.layer.Vector({
source: source
});
var map = new ol.Map({
layers: [raster, vector],
target: 'map',
view: new ol.View({
center: [-11000000, 4600000],
zoom: 4
})
});
var draw; // global so we can remove it later
function addInteraction() {
draw = new ol.interaction.Draw({
source: source,
type: 'LineString'
});
map.addInteraction(draw);
}
var lastEvent;
map.on('pointermove', function(event){ lastEvent = event; });
var add = false;
setInterval(function(){
add = !add;
if (add) {
addInteraction();
draw.handleEvent(lastEvent);
} else {
map.removeInteraction(draw);
}
}, 5000);
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.4/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.4/ol.js"></script>
<div id="map" class="map"></div>