OpenLayers 3 事件应用
常用鼠标事件
地图鼠标左键单击事件
对应的类为ol.Map,事件名为singleclick。
地图鼠标左键双击事件
对应的类为ol.Map,事件名为dblclick。
地图鼠标点击事件
对应的类为ol.Map,事件名为click。
地图鼠标移动事件
对应的类为ol.Map,事件名为pointermove。
地图鼠标拖拽事件
对应的类为ol.Map,事件名为pointerdrag。
地图移动事件
对应的类为ol.Map,事件名为moveend。
一、简单事件应用:鼠标左键单击事件
1、项目结构
2、map.html
<!Doctype html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta content='always' name='referrer'>
<title>OpenLayers 3 :鼠标左键单击事件</title>
<link href='ol.css ' rel='stylesheet' type='text/css'/>
<script type='text/javascript' src='ol.js' charset='utf-8'></script>
</head>
<body>
<div id='map' style='width: 1000px;height: 800px;margin: auto'></div>
<script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: [0, 0],
zoom: 0
}),
target: 'map'
});
map.on('singleclick', function (event) {
console.log("鼠标左键点击了")
})
</script>
</body>
</html>
3、运行截图
二、事件注销
1、map2.html
<!Doctype html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta content='always' name='referrer'>
<title>OpenLayers 3 :事件注销</title>
<link href='ol.css ' rel='stylesheet' type='text/css'/>
<script type='text/javascript' src='ol.js' charset='utf-8'></script>
</head>
<body>
<div id='map' style='width: 1000px;height: 800px;margin: auto'></div>
<script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: [0, 0],
zoom: 0
}),
target: 'map'
});
var dblclickListener = function (event) {
console.log("鼠标左键双击了")
map.un('dblclick', dblclickListener);
console.log("dblclick事件注销")
};
map.on('dblclick', dblclickListener);
map.once('pointerdrag', function (event) {
console.log("地图发生拖拽")
console.log("pointerdrag事件注销")
})
</script>
</body>
</html>
2、运行截图
三、常用事件
1、map3.html
<!Doctype html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta content='always' name='referrer'>
<title>OpenLayers 3 :常用事件</title>
<link href='ol.css ' rel='stylesheet' type='text/css'/>
<script type='text/javascript' src='ol.js' charset='utf-8'></script>
</head>
<body>
<div id='map' style='width: 1000px;height: 800px;margin: auto'></div>
<script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: [0, 0],
zoom: 0
}),
target: 'map'
});
map.on('singleclick', function (event) {
console.log("触发了map的单击事件:singleclick")
});
map.on('dblclick', function (event) {
console.log("触发了map的双击事件:dblclick")
});
map.on('click', function (event) {
console.log("触发了map的点击事件:click")
});
map.on('pointermove', function (event) {
console.log("触发了map的鼠标移动事件:pointermove")
});
map.on('pointerdrag', function (event) {
console.log("触发了map的拖拽事件:pointerdrag")
});
map.on('moveend', function (event) {
console.log("触发了map的地图移动事件:moveend")
});
</script>
</body>
</html>
2、运行截图
四、自定事件
1、map4.html
<!Doctype html>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'>
<meta content='always' name='referrer'>
<title>OpenLayers 3 :自定义事件</title>
<link href='ol.css ' rel='stylesheet' type='text/css'/>
<script type='text/javascript' src='ol.js' charset='utf-8'></script>
</head>
<body>
<div id='map' style='width: 1000px;height: 800px;margin: auto'></div>
<script>
var feature = new ol.Feature({
geometry: new ol.geom.Point([0, 0])
});
feature.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 100,
fill: new ol.style.Fill({
color: 'red'
})
})
}));
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: new ol.source.Vector({
features: [feature]
})
})
],
view: new ol.View({
center: [0, 0],
zoom: 0
}),
target: 'map'
});
map.on('pointermove', function (event) {
if (map.hasFeatureAtPixel(event.pixel)) {
map.forEachFeatureAtPixel(event.pixel, function (f) {
if (f === feature){
f.dispatchEvent({type: 'mousein', event: event});
console.log("鼠标移入圆")
}
});
} else {
feature.dispatchEvent({type: 'mouseout', event: event});
console.log("鼠标移出圆")
}
});
feature.on('mousein', function (event) {
this.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 150,
fill: new ol.style.Fill({
color: 'blue'
})
})
}));
});
feature.on('mouseout', function (event) {
this.setStyle(new ol.style.Style({
image: new ol.style.Circle({
radius: 100,
fill: new ol.style.Fill({
color: 'yellow'
})
})
}));
});
</script>
</body>
</html>
2、运行截图