Vue 3 中使用 OpenLayers 實(shí)時(shí)顯示 zoom 及四角坐標(biāo)的實(shí)現(xiàn)代碼
在 Vue 3 中使用 OpenLayers 實(shí)時(shí)顯示 zoom 及四角坐標(biāo)
1. 引言
在前端開發(fā)中,OpenLayers 是一個(gè)強(qiáng)大的開源地圖庫,可以用于 WebGIS 開發(fā)。在 Vue 3 中,我們可以結(jié)合 OpenLayers 來實(shí)現(xiàn)地圖的交互功能,比如獲取當(dāng)前地圖的縮放級別(zoom)以及地圖邊界的坐標(biāo)信息。
本篇文章將介紹如何在 Vue 3 中使用 OpenLayers,實(shí)時(shí)獲取地圖的 zoom 值,并顯示地圖視圖的左上、左下、右上、右下四個(gè)角的坐標(biāo)。
2. 項(xiàng)目環(huán)境
- Vue 3
- OpenLayers
- Vite(可選)
3. 代碼實(shí)現(xiàn)
3.1 安裝 OpenLayers
如果你的 Vue 3 項(xiàng)目還沒有安裝 OpenLayers,可以使用 npm 進(jìn)行安裝:
npm install ol
3.2 代碼實(shí)現(xiàn)
創(chuàng)建一個(gè) Vue 組件(OpenLayersMap.vue
),用于渲染地圖,并監(jiān)聽 moveend
事件來獲取 zoom 值和四角坐標(biāo)。
組件代碼:
<!-- * @Author: 彭麒 * @Date: 2025/4/1 * @Email: 1062470959@qq.com * @Description: 此源碼版權(quán)歸吉檀迦俐所有,可供學(xué)習(xí)和借鑒或商用。 --> <template> <div class="container"> <div class="w-full flex justify-center flex-wrap"> <div class="font-bold text-[24px]">在Vue3中使用OpenLayers實(shí)時(shí)顯示zoom、左下、左上、右上、右下的坐標(biāo)</div> </div> <h4> 當(dāng)前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 地圖實(shí)例,添加了 OSM(OpenStreetMap)作為底圖。- 設(shè)置
EPSG:4326
投影坐標(biāo)系。
- 監(jiān)聽
moveend
事件moveend
事件觸發(fā)時(shí)調(diào)用updateCoordinates
方法,計(jì)算地圖視圖的zoom
值和四個(gè)角的坐標(biāo)。
- 獲取地圖范圍
- 使用
getTopLeft
、getTopRight
、getBottomLeft
、getBottomRight
獲取當(dāng)前地圖視圖的四個(gè)角的坐標(biāo),并格式化數(shù)據(jù)。
- 使用
4. 運(yùn)行效果
運(yùn)行項(xiàng)目后,你將看到 OpenLayers 地圖,并且隨著地圖縮放或移動(dòng),當(dāng)前 zoom 值和四角坐標(biāo)會(huì)實(shí)時(shí)更新。
5. 總結(jié)
本文介紹了如何在 Vue 3 中使用 OpenLayers 來獲取地圖的 zoom 值以及四角坐標(biāo)信息,并實(shí)時(shí)更新數(shù)據(jù)。這種方式可以用于 GIS 應(yīng)用開發(fā),幫助用戶更好地了解當(dāng)前地圖范圍。
到此這篇關(guān)于Vue 3 中使用 OpenLayers 實(shí)時(shí)顯示 zoom 及四角坐標(biāo)的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)Vue 3使用 OpenLayers內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue+openlayers+nodejs+postgis實(shí)現(xiàn)軌跡運(yùn)動(dòng)效果
- Vue3 openlayers加載瓦片地圖并手動(dòng)標(biāo)記坐標(biāo)點(diǎn)功能
- Vue使用openlayers加載天地圖
- vue?openlayers實(shí)現(xiàn)臺風(fēng)軌跡示例詳解
- vue利用openlayers實(shí)現(xiàn)動(dòng)態(tài)軌跡
- Vue結(jié)合openlayers按照經(jīng)緯度坐標(biāo)實(shí)現(xiàn)錨地標(biāo)記及繪制多邊形區(qū)域
- Vue openLayers實(shí)現(xiàn)圖層數(shù)據(jù)切換與加載流程詳解
- Vue利用openlayers實(shí)現(xiàn)點(diǎn)擊彈窗的方法詳解
- Vue使用openlayers實(shí)現(xiàn)繪制圓形和多邊形
相關(guān)文章
Vue源碼學(xué)習(xí)之響應(yīng)式是如何實(shí)現(xiàn)的
最近接觸了vue.js,一度非常好奇vue.js是如何監(jiān)測數(shù)據(jù)更新并且重新渲染頁面,這篇文章主要給大家介紹了關(guān)于Vue源碼學(xué)習(xí)之響應(yīng)式是如何實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2021-10-10如何利用vue-cli監(jiān)測webpack打包與啟動(dòng)時(shí)長
這篇文章主要給大家介紹了關(guān)于如何利用vue-cli監(jiān)測webpack打包與啟動(dòng)時(shí)長的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Vue實(shí)現(xiàn)點(diǎn)擊當(dāng)前元素以外的地方隱藏當(dāng)前元素(實(shí)現(xiàn)思路)
這篇文章主要介紹了Vue實(shí)現(xiàn)點(diǎn)擊當(dāng)前元素以外的地方隱藏當(dāng)前元素,文中給大家擴(kuò)展了vue實(shí)現(xiàn)點(diǎn)擊其他地方隱藏div的三種方法,需要的朋友可以參考下2019-12-12element-ui table組件如何使用render屬性的實(shí)現(xiàn)
這篇文章主要介紹了element-ui table組件如何使用render屬性的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11vue實(shí)現(xiàn)button按鈕的重復(fù)點(diǎn)擊指令方式
這篇文章主要介紹了vue實(shí)現(xiàn)button按鈕的重復(fù)點(diǎn)擊指令方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08