溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用vue-openlayers實(shí)現(xiàn)地圖坐標(biāo)彈框效果

發(fā)布時(shí)間:2020-10-30 21:28:49 來源:億速云 閱讀:887 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)使用vue-openlayers實(shí)現(xiàn)地圖坐標(biāo)彈框效果,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識(shí)有一定的了解。

openlayers

這個(gè)效果是點(diǎn)擊地圖,彈出坐標(biāo)信息。

使用vue-openlayers實(shí)現(xiàn)地圖坐標(biāo)彈框效果

點(diǎn)擊地圖邊緣時(shí),底圖會(huì)跟著移動(dòng),使彈窗能完整顯示出來。

<template>
 <div class="vm">
  <h3 class="h-title">彈窗 popup</h3>
  <div id="map" class="map-x"></div>
  
  <!-- 彈窗元素 -->
  <div
   class="popup"
   ref="popup"
   v-show="currentCoordinate"
  >
   <span class="icon-close" @click="closePopup">&#10006;</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í)別(打開頁面時(shí)默認(rèn)級(jí)別)
    })
   })
   this.mapClick() // 初始化地圖成功后,給地圖添加點(diǎn)擊事件
  },
  mapClick () { // 地圖點(diǎn)擊事件
   // 通過 map.on() 監(jiān)聽,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>

關(guān)于使用vue-openlayers實(shí)現(xiàn)地圖坐標(biāo)彈框效果就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI