Vue 3 中使用 OpenLayers 實時顯示 zoom 及四角坐標的實現(xiàn)代碼
在 Vue 3 中使用 OpenLayers 實時顯示 zoom 及四角坐標
1. 引言
在前端開發(fā)中,OpenLayers 是一個強大的開源地圖庫,可以用于 WebGIS 開發(fā)。在 Vue 3 中,我們可以結(jié)合 OpenLayers 來實現(xiàn)地圖的交互功能,比如獲取當前地圖的縮放級別(zoom)以及地圖邊界的坐標信息。
本篇文章將介紹如何在 Vue 3 中使用 OpenLayers,實時獲取地圖的 zoom 值,并顯示地圖視圖的左上、左下、右上、右下四個角的坐標。
2. 項目環(huán)境
- Vue 3
- OpenLayers
- Vite(可選)
3. 代碼實現(xiàn)
3.1 安裝 OpenLayers
如果你的 Vue 3 項目還沒有安裝 OpenLayers,可以使用 npm 進行安裝:
npm install ol
3.2 代碼實現(xiàn)
創(chuàng)建一個 Vue 組件(OpenLayersMap.vue),用于渲染地圖,并監(jiān)聽 moveend 事件來獲取 zoom 值和四角坐標。
組件代碼:
<!--
* @Author: 彭麒
* @Date: 2025/4/1
* @Email: 1062470959@qq.com
* @Description: 此源碼版權(quán)歸吉檀迦俐所有,可供學習和借鑒或商用。
-->
<template>
<div class="container">
<div class="w-full flex justify-center flex-wrap">
<div class="font-bold text-[24px]">在Vue3中使用OpenLayers實時顯示zoom、左下、左上、右上、右下的坐標</div>
</div>
<h4>
當前zoom值: {{ czoom }}; <br>
左上{{ tl }} --- 左下{{ bl }} --- 右上{{ tr }} --- 右下{{ br }};
</h4>
<div id="vue-openlayers"></div>
</div>
</template>
<script setup>
import { onMounted, ref } from 'vue';
import 'ol/ol.css';
import { Map, View } from 'ol';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
import { getTopLeft, getBottomRight, getBottomLeft, getTopRight } from 'ol/extent';
const map = ref(null);
const czoom = ref(6);
const tl = ref([]);
const tr = ref([]);
const bl = ref([]);
const br = ref([]);
const updateCoordinates = () => {
if (!map.value) return;
czoom.value = map.value.getView().getZoom();
const extent = map.value.getView().calculateExtent(map.value.getSize());
tl.value = [Number(getTopLeft(extent)[0].toFixed(2)), Number(getTopLeft(extent)[1].toFixed(2))];
tr.value = [Number(getTopRight(extent)[0].toFixed(2)), Number(getTopRight(extent)[1].toFixed(2))];
bl.value = [Number(getBottomLeft(extent)[0].toFixed(2)), Number(getBottomLeft(extent)[1].toFixed(2))];
br.value = [Number(getBottomRight(extent)[0].toFixed(2)), Number(getBottomRight(extent)[1].toFixed(2))];
};
const initMap = () => {
map.value = new Map({
target: "vue-openlayers",
layers: [
new TileLayer({
source: new OSM()
})
],
view: new View({
projection: "EPSG:4326",
center: [116.15, 40.79],
zoom: czoom.value
}),
});
map.value.on('moveend', updateCoordinates);
};
onMounted(() => {
initMap();
updateCoordinates();
});
</script>
<style scoped>
.container {
width: 840px;
height: 630px;
margin: 50px auto;
border: 1px solid #42B983;
}
#vue-openlayers {
width: 800px;
height: 450px;
margin: 0 auto;
border: 1px solid #42B983;
position: relative;
}
</style>3.3 代碼解析
- 初始化地圖
initMap方法創(chuàng)建了 OpenLayers 地圖實例,添加了 OSM(OpenStreetMap)作為底圖。- 設置
EPSG:4326投影坐標系。
- 監(jiān)聽
moveend事件moveend事件觸發(fā)時調(diào)用updateCoordinates方法,計算地圖視圖的zoom值和四個角的坐標。
- 獲取地圖范圍
- 使用
getTopLeft、getTopRight、getBottomLeft、getBottomRight獲取當前地圖視圖的四個角的坐標,并格式化數(shù)據(jù)。
- 使用
4. 運行效果
運行項目后,你將看到 OpenLayers 地圖,并且隨著地圖縮放或移動,當前 zoom 值和四角坐標會實時更新。

5. 總結(jié)
本文介紹了如何在 Vue 3 中使用 OpenLayers 來獲取地圖的 zoom 值以及四角坐標信息,并實時更新數(shù)據(jù)。這種方式可以用于 GIS 應用開發(fā),幫助用戶更好地了解當前地圖范圍。
到此這篇關(guān)于Vue 3 中使用 OpenLayers 實時顯示 zoom 及四角坐標的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Vue 3使用 OpenLayers內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue+openlayers+nodejs+postgis實現(xiàn)軌跡運動效果
- Vue3 openlayers加載瓦片地圖并手動標記坐標點功能
- Vue使用openlayers加載天地圖
- vue?openlayers實現(xiàn)臺風軌跡示例詳解
- vue利用openlayers實現(xiàn)動態(tài)軌跡
- Vue結(jié)合openlayers按照經(jīng)緯度坐標實現(xiàn)錨地標記及繪制多邊形區(qū)域
- Vue openLayers實現(xiàn)圖層數(shù)據(jù)切換與加載流程詳解
- Vue利用openlayers實現(xiàn)點擊彈窗的方法詳解
- Vue使用openlayers實現(xiàn)繪制圓形和多邊形
相關(guān)文章
如何利用vue-cli監(jiān)測webpack打包與啟動時長
這篇文章主要給大家介紹了關(guān)于如何利用vue-cli監(jiān)測webpack打包與啟動時長的相關(guān)資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2022-02-02
Vue實現(xiàn)點擊當前元素以外的地方隱藏當前元素(實現(xiàn)思路)
這篇文章主要介紹了Vue實現(xiàn)點擊當前元素以外的地方隱藏當前元素,文中給大家擴展了vue實現(xiàn)點擊其他地方隱藏div的三種方法,需要的朋友可以參考下2019-12-12
element-ui table組件如何使用render屬性的實現(xiàn)
這篇文章主要介紹了element-ui table組件如何使用render屬性的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11

