openlayers学习——4、openlayers弹出框popup弹出层Overlay

openlayers弹出框popup弹出层Overlay

前言:基于Vue,学习openlayers,根据官网demo,记录常用功能写法。
     本人不是专业GIS开发,只是记录,方便后续查找。

参考资料:
openlayers官网:https://openlayers.org/
geojson下载网站:https://datav.aliyun.com/portal/school/atlas/area_selector
地图坐标拾取网站:https://api.map.baidu.com/lbsapi/getpoint/index.html

openlayers核心:Map对象、View视图、Layer图层、Source来源、Feature特征等

在地图DOM同级别设置弹出框DOM

<!-- 这里随意设置,没有什么规则,就像普通盒子模型一样,主要是css设置 -->
<div id="popup" class="ol-popup">
  <a href="#" id="popup-closer" class="ol-popup-closer" @click="removePopup" />
  <div id="popup-content" />
</div>
// 核心就是绝对定位
.ol-popup {
  position: absolute;
  background-color: white;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
  padding: 15px;
  border-radius: 10px;
  border: 1px solid #cccccc;
  bottom: 12px;
  left: -50px;
  min-width: 280px;
}
.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: "✖";
}
import Overlay from 'ol/Overlay'
// 核心思想:创建弹出层对象、绑定DOM、编辑弹出层内容、添加到map中、设置位置
// 弹出框
addPopup (ctn = [118.339408, 32.261271]) {
  this.removePopup()
  // 获取弹出层DOM
  const container = document.getElementById('popup')
  const content = document.getElementById('popup-content')
  if (this.overlay) {
  } else {
    // 创建Overlay弹出层绑定DOM
    this.overlay = new Overlay({
      element: container,
      autoPan: {
        animation: {
          duration: 250
        }
      }
    })
    // 添加到map
    this.map.addOverlay(this.overlay)
  }
  // 弹出层内容
  content.innerHTML = `
                      <b style='color: blue'>弹出层信息</b>
                      <br/>
                      <table>
                        <tr>
                          <th>信息1:</th>
                          <td>XXXXXXXXX</td>
                        </tr>
                        <tr>
                          <th>信息2:</th>
                          <td>XXXXXXXXX</td>
                        </tr>
                      </table>
                      `
  // 设置弹出层位置即可出现
  this.overlay.setPosition(ctn)
},
// 清除弹出框
removePopup () {
  if (this.overlay) {
    // 设置位置undefined可达到隐藏清除弹出框
    this.overlay.setPosition(undefined)
  }
}

最后效果
在这里插入图片描述

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在OpenLayers地图上点击位置后弹出提示,可以使用OverlayPopup来实现。以下是一个简单的例子: ```javascript // 创建地图 var map = new ol.Map({ target: 'map', layers: [ new ol.layer.Tile({ source: new ol.source.OSM() }) ], view: new ol.View({ center: ol.proj.fromLonLat([116.3975, 39.9086]), zoom: 10 }) }); // 创建一个VectorLayer用于添加点 var vectorLayer = new ol.layer.Vector({ source: new ol.source.Vector() }); // 创建一个Overlay用于显示提示 var popupOverlay = new ol.Overlay({ element: document.getElementById('popup'), autoPan: true, autoPanAnimation: { duration: 250 } }); // 创建一个点击事件处理函数 function handleMapClick(event) { // 获取点击位置的坐标 var coordinate = event.coordinate; // 创建一个点Feature并添加到VectorLayer上 var feature = new ol.Feature({ geometry: new ol.geom.Point(coordinate) }); vectorLayer.getSource().addFeature(feature); // 设置提示内容为坐标 var content = '<p>坐标:' + coordinate + '</p>'; document.getElementById('popup-content').innerHTML = content; // 将提示显示在点击位置上 popupOverlay.setPosition(coordinate); map.addOverlay(popupOverlay); } // 监听Map对象的"click"事件,并在回调函数中处理点击事件 map.on('click', handleMapClick); // 将VectorLayer添加到地图map.addLayer(vectorLayer); ``` 在HTML中还需要添加一个Popup的容器,如下所示: ```html <div id="map"></div> <div id="popup" class="ol-popup"> <a href="#" id="popup-closer" class="ol-popup-closer"></a> <div id="popup-content"></div> </div> ``` 这样就可以在地图上点击位置后弹出提示了,提示会显示点击位置的坐标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值