vue-openlayers實(shí)現(xiàn)地圖坐標(biāo)彈框效果
本文實(shí)例為大家分享了vue-openlayers實(shí)現(xiàn)地圖坐標(biāo)彈框的具體代碼,供大家參考,具體內(nèi)容如下
這個(gè)效果是點(diǎn)擊地圖,彈出坐標(biāo)信息。

點(diǎn)擊地圖邊緣時(shí),底圖會(huì)跟著移動(dòng),使彈窗能完整顯示出來(lái)。
<template>
<div class="vm">
<h2 class="h-title">彈窗 popup</h2>
<div id="map" class="map-x"></div>
<!-- 彈窗元素 -->
<div
class="popup"
ref="popup"
v-show="currentCoordinate"
>
<span class="icon-close" @click="closePopup">✖</span>
<div class="content">{{currentCoordinate}}</div>
</div>
</div>
</template>
<script>
import 'ol/ol.css'
import { Map, View } from 'ol'
import Tile from 'ol/layer/Tile'
import OSM from 'ol/source/OSM'
import { toStringHDMS } from 'ol/coordinate'
import { toLonLat } from 'ol/proj'
import Overlay from 'ol/Overlay'
export default {
name: 'Popup',
data () {
return {
map: null,
currentCoordinate: null, // 彈窗坐標(biāo)數(shù)據(jù)
overlay: null
}
},
methods: {
initMap () {
// 彈窗
this.overlay = new Overlay({
element: this.$refs.popup, // 彈窗標(biāo)簽,在html里
autoPan: true, // 如果彈窗在底圖邊緣時(shí),底圖會(huì)移動(dòng)
autoPanAnimation: { // 底圖移動(dòng)動(dòng)畫
duration: 250
}
})
// 實(shí)例化地圖
this.map = new Map({
target: 'map',
layers: [
new Tile({
source: new OSM() // 使用OSM底圖
})
],
overlays: [this.overlay], // 把彈窗加入地圖
view: new View({
center: [-27118403.38733027, 4852488.79124965], // 北京坐標(biāo)
zoom: 12 // 地圖縮放級(jí)別(打開頁(yè)面時(shí)默認(rèn)級(jí)別)
})
})
this.mapClick() // 初始化地圖成功后,給地圖添加點(diǎn)擊事件
},
mapClick () { // 地圖點(diǎn)擊事件
// 通過(guò) map.on() 監(jiān)聽(tīng),singleclick 是單擊的意思。也可以用 click 代替 singleclick。
this.map.on('singleclick', evt => {
const coordinate = evt.coordinate // 獲取坐標(biāo)
const hdms = toStringHDMS(toLonLat(coordinate)) // 轉(zhuǎn)換坐標(biāo)格式
this.currentCoordinate = hdms // 保存坐標(biāo)點(diǎn)
setTimeout(() => {
// 設(shè)置彈窗位置
// 這里要設(shè)置定時(shí)器,不然彈窗首次出現(xiàn),底圖會(huì)跑偏
this.overlay.setPosition(coordinate)
}, 0)
})
},
// 關(guān)閉彈窗
closePopup () {
// 把彈窗位置設(shè)置為undefined,并清空坐標(biāo)數(shù)據(jù)
this.overlay.setPosition(undefined)
this.currentCoordinate = null
}
},
mounted () {
this.initMap()
}
}
</script>
<style lang="scss" scoped>
/* 彈窗樣式 */
.popup {
min-width: 280px;
position: relative;
background: #fff;
padding: 8px 16px;
display: flex;
flex-direction: column;
transform: translate(-50%, calc(-100% - 12px));
/* 彈窗下方的小三角形 */
&::after {
display: block;
content: '';
width: 0;
height: 0;
position: absolute;
border: 12px solid transparent;
border-top-color: #fff;
bottom: -23px;
left: 50%;
transform: translateX(-50%);
}
}
/* 關(guān)閉彈窗按鈕 */
.icon-close {
cursor: pointer;
align-self: flex-end;
margin-bottom: 10px;
}
</style>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入學(xué)習(xí)Vue nextTick的用法及原理
這篇文章主要介紹了深入學(xué)習(xí)Vue nextTick的用法及原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
vue3集成echarts數(shù)據(jù)刷新后圖表不刷新的解決方法
vue3 集成 echarts 最大的坑就是出現(xiàn)了,reactive 的數(shù)據(jù) 刷新了,但圖表缺不會(huì)刷新,所以本文就給大家詳細(xì)的介紹一下vue3集成echarts數(shù)據(jù)刷新后圖表不刷新的解決方法,需要的朋友可以參考下2023-08-08
Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹
這篇文章主要介紹了Vue export import 導(dǎo)入導(dǎo)出的多種方式與區(qū)別介紹,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Vuex實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車
這篇文章主要為大家詳細(xì)介紹了Vuex實(shí)現(xiàn)簡(jiǎn)單購(gòu)物車,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
vue.js學(xué)習(xí)筆記之v-bind和v-on解析
這篇文章主要介紹了vue.js學(xué)習(xí)筆記之v-bind和v-on解析,v-bind 指令用于響應(yīng)地更新 HTML 特征,v-on 指令用于監(jiān)聽(tīng)DOM事件,文中還給大家提到了v-bind,v-on的縮寫,感興趣的朋友參考下吧2018-05-05
el-table-column 內(nèi)容不自動(dòng)換行的解決方法
本文主要介紹了el-table-column 內(nèi)容不自動(dòng)換行的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Vue實(shí)現(xiàn)PopupWindow組件詳解
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)PopupWindow組件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
vue多功能渲染函數(shù)h()的使用和多種應(yīng)用場(chǎng)景
我們?cè)趘ue項(xiàng)目里面用HTML標(biāo)簽構(gòu)建頁(yè)面時(shí)最終會(huì)被轉(zhuǎn)化成vnode,而h()是直接創(chuàng)建vnode,因此h()能以一種更靈活的方式在各種各樣情景下構(gòu)建組件的渲染邏輯,并且能帶來(lái)性能方式的提升,本文介紹如何使用和列出具體的應(yīng)用場(chǎng)景,需要的朋友可以參考下2024-08-08
ElementUI多個(gè)子組件表單的校驗(yàn)管理實(shí)現(xiàn)
這篇文章主要介紹了ElementUI多個(gè)子組件表單實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

