一、什么是Popup弹窗
二、为什么需要pop弹窗
三、如何实现
3-1、开发流程
1、创建一个DOM元素,并设置样式2、将DOM转换成overlay3、监听点击事件,判断如果点击的区域内存在点要素,就显示overlay
3-2、涉及的类
// 二. 将dom转换成overlayconst popup = new ol.Overlay({//要转换成overlay的HTML元素element: document.querySelector('#popup'),//当前窗口可见autoPan: true,//Popup放置的位置positioning: 'bottom-center',//是否应该停止事件传播到地图窗口stopEvent: true,autoPanAnimation: {//当Popup超出地图边界时,为了Popup全部可见,地图移动的速度duration: 250,}})
四、案例
4-1、创建DOM
<div id="mapCon"><!-- Popup --><div id="popup" class="ol-popup"><a href="#" id="popup-closer" class="ol-popup-closer"></a><div id="popup-content"><h2>武汉</h2><img src="../image/location.png" /></div></div></div>
样式部分
.ol-popup {position: absolute;background-color: white;-webkit-filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.2));padding: 15px;border-radius: 10px;border: 1px solid #cccccc;bottom: 45px;left: -50px;z-index: 999;}.ol-popup:after,.ol-popup:before {top: 100%;border: solid transparent;content: ' ';height: 0;width: 0;position: absolute;pointer-events: none;}.ol-popup:after {border-top-color: white;border-width: 10px;left: 48px;margin-left: -10px;}.ol-popup:before {border-top-color: #cccccc;border-width: 11px;left: 48px;margin-left: -11px;}.ol-popup-closer {text-decoration: none;position: absolute;top: 2px;right: 8px;}.ol-popup-closer:after {content: '✖';}#popup-content {font-size: 14px;font-family: '微软雅黑';}#popup-content img {width: 30px;}
4-2、创建Overlay层
overlay是地图上的一个透明层
// 二. 将dom转换成overlayvar container = document.getElementById('popup');var content = document.getElementById('popup-content');var closer = document.getElementById('popup-closer')const popup = new ol.Overlay({//要转换成overlay的HTML元素element:container//当前窗口可见autoPan: true,//Popup放置的位置positioning: 'bottom-center',//是否应该停止事件传播到地图窗口stopEvent: true,autoPanAnimation: {//当Popup超出地图边界时,为了Popup全部可见,地图移动的速度duration: 250,},})map.addOverlay(popup)
4-3、监听点击事件
//添加关闭按钮的单击事件(隐藏popup)closer.onclick = function () {//未定义popup位置popup.setPosition(undefined);//失去焦点closer.blur();return false;};// 为map添加点击事件监听,渲染弹出popupmap.on('click', function (e) {// 获取当前点击的点是否存在要素, 并返回const feature = map.forEachFeatureAtPixel(//e.pixel,function (feature, layer) {return feature})if (feature) {if (popup.getPosition() == undefined) {var position = feature.getGeometry().flatCoordinatespopup.setPosition(position)}}})//为map添加鼠标移动事件监听,当指向标注时改变鼠标光标状态map.on('pointermove', function (e) {var pixel = map.getEventPixel(e.originalEvent);var hit = map.hasFeatureAtPixel(pixel);map.getTargetElement().style.cursor = hit ? 'pointer' : '';});
五、完整示例
<body><div id="map_container"><div id="popup" class="ol-popup"><a href="#" id="popup-closer" class="ol-popup-closer"></a><div id="popup-content"><h2>武汉</h2><img src="./image/location.png" alt=""></div></div></div><script>var map = new ol.Map({target: "map_container",layers: [gaodeMapLayer],view: new ol.View({projection: 'EPSG:4326',center: [114.30, 30.50],zoom: 4})})var labelStyle = new ol.style.Style({image: new ol.style.Icon(/** @type {olx.style.IconOptions} */({anchor: [0.5, 60],anchorOrigin: 'top-right',anchorXUnits: 'fraction',anchorYUnits: 'pixels',offsetOrigin: 'top-right',// offset:[0,10],//图标缩放比例// scale:0.5,//透明度opacity: 0.75,//图标的urlsrc: './image/location.png'})),text: new ol.style.Text({//位置textAlign: 'center',//基准线textBaseline: 'middle',//文字样式font: 'normal 14px 微软雅黑',//文本内容text: "武汉市",//文本填充样式(即文字颜色)fill: new ol.style.Fill({color: '#aa3300'}),stroke: new ol.style.Stroke({color: '#ffcc33',width: 2})})});var Point = new ol.Feature({geometry: new ol.geom.Point([114.30, 30.50])})Point.setStyle(labelStyle)var source = new ol.source.Vector({features: [Point]})var layer = new ol.layer.Vector({source})map.addLayer(layer);/*** 实现popup的html元素*/var container = document.getElementById('popup');var content = document.getElementById('popup-content');var closer = document.getElementById('popup-closer');/*** 1、在地图容器中创建一个Overlay*/var popup = new ol.Overlay(/** @type {olx.OverlayOptions} */({//要转换成overlay的HTML元素element: container,//当前窗口可见autoPan: true,//Popup放置的位置positioning: 'bottom-center',//是否应该停止事件传播到地图窗口stopEvent: false,autoPanAnimation: {//当Popup超出地图边界时,为了Popup全部可见,地图移动的速度duration: 250}}));map.addOverlay(popup);/*** 2、添加关闭按钮的单击事件(隐藏popup)* @return {boolean} Don't follow the href.*/closer.onclick = function () {//未定义popup位置popup.setPosition(undefined);//失去焦点closer.blur();return false;};/*** 3、为map添加点击事件监听,渲染弹出popup*/map.on('click', function (evt) {//判断当前单击处是否有要素,捕获到要素时弹出popupvar feature = map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {return feature;});if (feature) {var position = feature.getGeometry().flatCoordinatesif (popup.getPosition() == undefined) {//设置popup的位置popup.setPosition(position);}}});/*** 为map添加鼠标移动事件监听,当指向标注时改变鼠标光标状态*/map.on('pointermove', function (e) {var pixel = map.getEventPixel(e.originalEvent);var hit = map.hasFeatureAtPixel(pixel);map.getTargetElement().style.cursor = hit ? 'pointer' : '';});</script></body>
:::tips
1、点击要素,出现一个popup弹窗,仅仅只针对openlayer的点要素
2、通过IG Server加载过来的点要素,仅仅是一张图片,在map容器上显示的不是一个要素。
:::
