OpenLayers Popup(浮窗)的Vue组件

根据openlayers官方的popup实例改的,使用Vue2+TS,功能比较简单

OLPopop.vue

<template>
  <div ref="container" class="ol-popup">
    <a href="#" ref="closer" class="ol-popup-closer"></a>
    <slot></slot>
  </div>
</template>
import Overlay from "ol/Overlay";
import { Component, Vue, Ref } from "vue-property-decorator";

@Component
export default class OLPopup extends Vue {
  @Ref("container") container!: HTMLDivElement;
  @Ref("closer") closer!: HTMLDivElement;
  overlay = new Overlay({});
  private mounted() {
    this.overlay = new Overlay({
      element: this.container,
      autoPan: {
        animation: {
          duration: 250,
        },
      },
    });

    this.closer.onclick = () => {
      this.overlay.setPosition(undefined);
      this.closer.blur();
      return;
    };
  }
}
.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: "✖";
}

调用
HelloWorld.vue

<template>
  <div class="main">
    <div id="map" class="map"></div>
    <OLPopup ref="olPopup">
      <div>
        <p>You clicked here:</p>
        <code>{{ info }}</code>
      </div>
    </OLPopup>
  </div>
</template>
import "ol/ol.css";
import Map from "ol/Map";
import TileLayer from "ol/layer/Tile";
import View from "ol/View";
import OSM from "ol/source/OSM";
import { toLonLat } from "ol/proj";
import { toStringHDMS } from "ol/coordinate";
import { Component, Vue, Ref } from "vue-property-decorator";

import OLPopup from "../widgets/OLPopop.vue";

@Component({ components: { OLPopup } })
export default class HelloWorld extends Vue {
  private info: string = "";
  @Ref("olPopup") myPopup!: OLPopup;

  private mounted() {
    const map = new Map({
      layers: [
        new TileLayer({
          source: new OSM(),
        }),
      ],
      target: "map",
      view: new View({
        center: [0, 0],
        zoom: 2,
      }),
    });

    map.addOverlay(this.myPopup.overlay);

    map.on("singleclick", (evt) => {
      const coordinate = evt.coordinate;
      const hdms = toStringHDMS(toLonLat(coordinate));
      this.info = hdms;
      this.myPopup.overlay.setPosition(coordinate);
    });
  }
}
.main {
  width: 100%;
  height: 100%;
  position: fixed;
  left: 0;
  top: 0;
  overflow: hidden;
}
.map {
  width: 100%;
  height: 100%;
}

效果
在这里插入图片描述
感觉还算简练,样式可以再优化一下,下次试一下插槽支不支持elementui的表格之类的

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的 OpenLayers Overlay 与 Vue 组件结合的案例: 1. 在 Vue 组件中定义一个 div 元素,并设置其样式和位置,作为 OpenLayers Overlay 的容器。 ```html <template> <div class="map-overlay" ref="overlay"></div> </template> <style> .map-overlay { position: absolute; top: 10px; right: 10px; z-index: 100; } </style> ``` 2. 在 Vue 组件中引入 OpenLayers 库,并在 mounted 钩子函数中创建一个 Overlay 对象,并将其添加到地图上。 ```javascript import ol from 'openlayers'; export default { mounted() { const map = new ol.Map({ target: 'map', // ... }); const overlay = new ol.Overlay({ element: this.$refs.overlay, positioning: 'top-right' }); map.addOverlay(overlay); } } ``` 3. 在 Vue 组件中定义一个方法,用于更新 Overlay 中的内容。这个方法可以根据需要接受一个参数,并将其渲染到 Overlay 容器中。 ```javascript export default { // ... methods: { updateOverlay(content) { this.$refs.overlay.innerHTML = content; } } } ``` 4. 在 Vue 组件中使用 updateOverlay 方法,更新 Overlay 中的内容。 ```javascript export default { // ... mounted() { // ... const overlay = new ol.Overlay({ element: this.$refs.overlay, positioning: 'top-right' }); map.addOverlay(overlay); // 更新 Overlay 内容 this.updateOverlay('Hello, World!'); } } ``` 这个案例中,我们使用了 OpenLayers 的 Overlay 对象来创建一个动面板,然后将其与 Vue 组件结合,通过 updateOverlay 方法来更新 Overlay 中的内容。这种方式可以方便地将 OpenLayers 的地图功能与 Vue 组件结合起来,实现更加灵活和丰富的交互效果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值