首頁 > 軟體

Vue利用vue-baidu-map實現獲取經緯度和搜尋地址

2022-09-24 14:00:40

在開發專案的時候,發現需要獲取經緯度,由於這個專案是用vue寫的,最後決定使用vue-baidu-map來快速獲取經緯度

基於 Vue.js 封裝的百度地圖元件,執行流暢,程式碼簡單易懂,幾乎包含了百度地圖官方的所有API,檔案地址:https://dafrok.github.io/vue-baidu-map/

1.申請百度地圖金鑰

搜尋'百度地圖開放平臺',選擇頭部導航欄開發檔案裡的JavaScript API

然後點選申請金鑰

2.安裝vue-baidu-map

終端執行

npm install vue-baidu-map --save

main.js裡全域性註冊

import Vue from 'vue'
import BaiduMap from 'vue-baidu-map'

Vue.use(BaiduMap, {
  // ak 是在百度地圖開發者平臺申請的金鑰 詳見 http://lbsyun.baidu.com/apiconsole/key */
  ak: 'YOUR_APP_KEY'
})

引入元件,初始化地圖,開啟滑鼠滾輪控制地圖縮放

<template>
  <baidu-map id="allmap" @ready="mapReady" :scroll-wheel-zoom="true">
  </baidu-map>
</template>

<script>
export default {
    methods: {
        //地圖初始化
        mapReady({ BMap, map }) {
          this.point = new BMap.Point(113.27, 23.13); // 選擇一個經緯度作為中心點
          map.centerAndZoom(this.point, 12); //設定地圖中心點和縮放級別
          this.BMap=BMap
          this.map=map
        }
    }
}
</script>

<style>
#allmap{
  height: 450px;
  width: 100%;
  margin: 10px 0;
}
</style>

一個簡單的地圖就出來了

3.獲取經緯度

3.1 為地圖新增點選事件,獲取經緯度

<template>
  <baidu-map id="allmap" @ready="mapReady" @click="getLocation" :scroll-wheel-zoom="true">
  </baidu-map>
</template>

<script>
export default {
    data () {
        return {
          infoWindowShow:false,
          longitude:'',
          latitude:'',
        }
      },
    methods: {
        getLocation(e){
          this.longitude=e.point.lng
          this.latitude=e.point.lat
          this.infoWindowShow=true
        },
    }
}
</script>

3.2 新增BmInfoWindow資訊視窗元件和BmMarker標註,點選地圖時彈出

<template>
  <baidu-map id="allmap" @ready="mapReady" @click="getLocation" :scroll-wheel-zoom="true">
      <bm-marker v-if="infoWindowShow" :position="{lng: longitude, lat: latitude}">
          <bm-label content="" :labelStyle="{color: 'red', fontSize : '24px'}" :offset="{width: -35, height: 30}"/>
      </bm-marker>
      <bm-info-window :position="{lng: longitude, lat: latitude}" :show="infoWindowShow" @clickclose="infoWindowClose">
          <p>緯度:{{this.latitude}}</p>
          <p>經度:{{this.longitude}}</p>
      </bm-info-window>
  </baidu-map>
</template>

3.3 新增BmAutoComplete元件,輸入地址自動補全

<template>
  <baidu-map id="allmap" @ready="mapReady" @click="getLocation" :scroll-wheel-zoom="true">
      <div style="display:flex;justify-content:center;margin:15px">
          <bm-auto-complete v-model="searchJingwei" :sugStyle="{zIndex: 999999}">
            <el-input v-model="searchJingwei" style="width:300px;margin-right:15px" placeholder="輸入地址"></el-input>
          </bm-auto-complete>
          <el-button type="primary" @click="getBaiduMapPoint">搜尋</el-button>
      </div>
      <bm-marker v-if="infoWindowShow" :position="{lng: longitude, lat: latitude}">
          <bm-label content="" :labelStyle="{color: 'red', fontSize : '24px'}" :offset="{width: -35, height: 30}"/>
      </bm-marker>
      <bm-info-window :position="{lng: longitude, lat: latitude}" :show="infoWindowShow" @clickclose="infoWindowClose">
          <p>緯度:{{this.latitude}}</p>
          <p>經度:{{this.longitude}}</p>
      </bm-info-window>
  </baidu-map>
</template>

3.4 最後點選搜尋按鈕地圖跳轉到搜尋位置並開啟資訊視窗和標註

<script>
export default {
    data () {
        return {
          infoWindowShow:false,
          longitude:'',
          latitude:'',
        }
      },
    methods: {
        getBaiduMapPoint(){
          let that=this
          let myGeo = new this.BMap.Geocoder()
          //逆地址解析
          myGeo.getPoint(this.searchJingwei,function(point){
            if(point){
              that.map.centerAndZoom(point,15)
              that.latitude=point.lat
              that.longitude=point.lng
              that.infoWindowShow=true
            }
          })
        },
    }
}
</script>

最後放上全部程式碼

<template>
  <div>
      <baidu-map
        style="display:flex;flex-direction: column-reverse;" 
        id="allmap"
        @ready="mapReady"
        @click="getLocation"
        :scroll-wheel-zoom="true"
      >
        <div style="display:flex;justify-content:center;margin:15px">
          <bm-auto-complete v-model="searchJingwei" :sugStyle="{zIndex: 999999}">
            <el-input v-model="searchJingwei" style="width:300px;margin-right:15px" placeholder="輸入地址"></el-input>
          </bm-auto-complete>
          <el-button type="primary" @click="getBaiduMapPoint">搜尋</el-button>
        </div>
        <bm-map-type :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']" anchor="BMAP_ANCHOR_TOP_LEFT"></bm-map-type>
        <bm-marker v-if="infoWindowShow" :position="{lng: longitude, lat: latitude}">
          <bm-label content="" :labelStyle="{color: 'red', fontSize : '24px'}" :offset="{width: -35, height: 30}"/>
        </bm-marker>
        <bm-info-window :position="{lng: longitude, lat: latitude}" :show="infoWindowShow" @clickclose="infoWindowClose">
          <p>緯度:{{this.latitude}}</p>
          <p>經度:{{this.longitude}}</p>
        </bm-info-window>
      </baidu-map>
  </div>
</template>

<script>
export default {
  data () {
    return {
      searchJingwei:'',
      infoWindowShow:false,
      longitude:'',
      latitude:'',
      point:''
    }
  },
  methods: {
    //地圖初始化
    mapReady({ BMap, map }) {
      // 選擇一個經緯度作為中心點
      this.point = new BMap.Point(113.27, 23.13);
      map.centerAndZoom(this.point, 12);
      this.BMap=BMap
      this.map=map
    },
    //點選獲取經緯度
    getLocation(e){
      this.longitude=e.point.lng
      this.latitude=e.point.lat
      this.infoWindowShow=true
    },
    getBaiduMapPoint(){
      let that=this
      let myGeo = new this.BMap.Geocoder()
      myGeo.getPoint(this.searchJingwei,function(point){
        if(point){
          that.map.centerAndZoom(point,15)
          that.latitude=point.lat
          that.longitude=point.lng
          that.infoWindowShow=true
        }

      })
    },
    //資訊視窗關閉
    infoWindowClose(){
      this.latitude=''
      this.longitude=''
      this.infoWindowShow=false
    },
  },
}
</script>

<style lang="scss" scoped>
#allmap{
  height: 450px;
  width: 100%;
  margin: 10px 0;
}
</style>

最終實現效果

到此這篇關於Vue利用vue-baidu-map實現獲取經緯度和搜尋地址的文章就介紹到這了,更多相關Vue vue-baidu-map經緯度內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com