Vue + Openlayers 使用 Overlay 添加 Popup 弹窗

最近使用 Vue 和 Openlayers 写了一个简单的小 dom ,用到了 Overlay 的弹窗功能,记录一下!

实验数据:test.xls

项目运行效果

创建元素:

  <div ref="popCon" id="popup" v-show="isShow"><!-- 弹出信息框 -->
    <span class="iconfont icon-guanbi" @click="closePop"></span>
    <ul id="info">
      <h4>基本信息</h4>
      <p>-------------------------------</p>
      <p>名称:{{name}}</p>
      <p>面积:{{output}}</p>
    </ul>
  </div>

引入依赖:

import 'ol/css';
import{ Overlay } from 'ol'

添加事件:

    getArea(){
      let _this = this;
      let sourceProj = this.map.getView().getProjection()//地图数据源投影坐标系4490
      let select = new Select();
      this.map.addInteraction(select);
      select.on('select',function(e){
        let area = Math.abs(getArea(e.selected[0].getGeometry(),{
          "projection": sourceProj,
          "radius": 6371008.8
        }))
        console.log(area);
        _this.output = _this.formatArea(area);
        console.log(_this.output);
        let elPopup = _this.$refs.popCon;
        let popup = new Overlay({
          element: elPopup, //挂载元素
          positioning:"center-center",
          stopEvent: false,
          offset:[0,-20]
        })
        _this.map.addOverlay(popup) 
        let center = getCenter(e.selected[0].getGeometry().getExtent())
        if(center){
          _this.isShow = true
          popup.setPosition(center)
        }else{
          _this.isShow = false
        }      
      })
    },

样式信息:

<style>
#popup{
  width: 200px;
  background-color: white;
  padding: 18px;
  border-radius: 10px;
  box-shadow: 0 0 15px rgb(177,177,177);
}
#popup span{
  position: absolute;
  top: 0%;
  right: 0%;
}
#info{
  font-size: 14px;
  text-align: left;
}
</style>

运行结果:

dom下载链接:

Vue+Openlayers DOM 下载

Vue OpenLayers气泡窗是一种在Vue.js应用程序中使用OpenLayers地图库的出窗口。您可以使用它来在地图上显示信息,例如位置名称,地址,图像等。以下是创建Vue OpenLayers气泡窗的一些步骤: 1. 安装OpenLayersVue.js 首先,您需要安装OpenLayersVue.js。您可以使用以下命令: ``` npm install ol vue ``` 2. 创建Vue组件 接下来,您需要创建一个Vue组件来包含OpenLayers地图和气泡窗。您可以使用以下代码: ``` <template> <div> <div ref="map" class="map"></div> <div ref="popup" class="ol-popup"> <a href="#" class="ol-popup-closer" @click.prevent="closePopup"></a> <div ref="popupContent"></div> </div> </div> </template> <script> import &#39;ol/ol.css&#39;; import Map from &#39;ol/Map&#39;; import View from &#39;ol/View&#39;; import Overlay from &#39;ol/Overlay&#39;; import TileLayer from &#39;ol/layer/Tile&#39;; import OSM from &#39;ol/source/OSM&#39;; export default { name: &#39;MapWithPopup&#39;, data() { return { map: null, popup: null, popupContent: null } }, mounted() { this.initMap(); }, methods: { initMap() { // create map this.map = new Map({ target: this.$refs.map, layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 2 }) }); // create popup this.popup = new Overlay({ element: this.$refs.popup, autoPan: true, autoPanAnimation: { duration: 250 } }); this.map.addOverlay(this.popup); // add click event to map this.map.on(&#39;click&#39;, this.onClick); }, onClick(event) { // get feature at clicked position const feature = this.map.forEachFeatureAtPixel(event.pixel, (feature) => feature); // check if feature exists if (feature) { // get geometry of feature const geometry = feature.getGeometry(); // get coordinate of geometry const coordinate = geometry.getCoordinates(); // set content of popup this.popupContent = &#39;Feature selected at &#39; + coordinate; // set position of popup this.popup.setPosition(coordinate); } else { // close popup this.popup.setPosition(undefined); } }, closePopup() { // close popup this.popup.setPosition(undefined); } } } </script> <style> .map { height: 400px; } .ol-popup { position: absolute; background-color: white; padding: 15px; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.5); font-size: 14px; line-height: 1.5; display: none; } .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: 48%; margin-left: -10px; } .ol-popup:before { border-top-color: rgba(0, 0, 0, 0.75); border-width: 11px; left: 48%; margin-left: -11px; } .ol-popup-closer { position: absolute; top: 5px; right: 5px; font-size: 12px; font-weight: bold; text-decoration: none; color: #bababa; } .ol-popup-closer:hover { color: #ffcccc; } </style> ``` 3. 使用Vue组件 最后,您可以在Vue.js应用程序中使用组件。您可以将其包含在另一个Vue组件中,也可以在Vue路由中使用它。以下是一个示例: ``` <template> <div> <h1>My Map</h1> <MapWithPopup /> </div> </template> <script> import MapWithPopup from &#39;./MapWithPopup&#39;; export default { name: &#39;MyApp&#39;, components: { MapWithPopup } } </script> ``` 当您在地图上单击时,将显示一个气泡窗口。您可以将此组件扩展为显示更多信息,例如图像,链接等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值