vue OpenLayer 标注静止overlay
时间: 2023-11-27 14:48:21 浏览: 144
以下是Vue OpenLayers标注静止overlay的方法:
```html
<template>
<div>
<div id="map" style="height: 500px;"></div>
</div>
</template>
<script>
import Map from "ol/Map";
import Overlay from "ol/Overlay";
import View from "ol/View";
import TileLayer from "ol/layer/Tile";
import OSM from "ol/source/OSM";
export default {
name: "Map",
data() {
return {
map: null,
overlay: null,
};
},
mounted() {
this.initMap();
},
methods: {
initMap() {
// 创建地图容器
this.map = new Map({
target: "map",
layers: [
new TileLayer({
source: new OSM(),
}),
],
view: new View({
center: [0, 0],
zoom: 2,
}),
});
// 创建overlay
this.overlay = new Overlay({
element: document.getElementById("overlay"),
autoPan: true,
autoPanAnimation: {
duration: 250,
},
});
// 将overlay添加到地图中
this.map.addOverlay(this.overlay);
// 弹窗关闭事件
const closer = document.getElementById("popup-closer");
closer.onclick = () => {
this.overlay.setPosition(undefined);
closer.blur();
return false;
};
// 点击地图事件
this.map.on("click", (evt) => {
// 获取点击位置坐标
const coordinate = evt.coordinate;
// 设置overlay的位置
this.overlay.setPosition(coordinate);
});
},
},
};
</script>
<style>
.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: 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: 48%;
margin-left: -10px;
}
.ol-popup:before {
border-top-color: #cccccc;
border-width: 11px;
left: 48%;
margin-left: -11px;
}
</style>
<template>
<div id="overlay" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div>这是一个静止的overlay</div>
</div>
</template>
```
阅读全文