亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue3中使用百度地圖的簡單步驟

 更新時間:2023年06月13日 08:50:31   作者:多喝開水少熬夜  
最近項目要用到百度地圖api,好久沒用到地圖,就百度了一番,下面這篇文章主要給大家介紹了關于vue3中使用百度地圖的簡單步驟,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下

前言

最近一個項目要用到地圖,因為微信小程序用的也是百度地圖,所以想著網(wǎng)頁端也用百度地圖,在網(wǎng)上查了很多方法,包括引入百度地圖第三方庫,還是有問題,發(fā)現(xiàn)最簡單的方法就是在index.html中引入script,然后直接在相關頁面肝就完事。

一、申請ak

在百度開發(fā)者平臺上面申請,其他博客都可以看到相關申請過程,這里就不多述。

因為還處于開發(fā)調(diào)試狀態(tài),所以白名單寫的是**。

二、使用步驟

1.在public下index.html引入相關script

 <script
    type="text/javascript"
    src="https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=your_ak"
></script>

2.在相關頁面編寫代碼

代碼如下(示例):

<template>
  <div class="bmap" id="container"></div>
  <div></div>
</template>
<script>
import { useStore } from 'vuex'
// import { ref } from 'vue'
export default {
  name: 'BmapDemo',
  mounted() {
    const store = useStore()
    var map = new window.BMapGL.Map('container')
    var point = new window.BMapGL.Point(
      store.state.record.longitude,//這里本人項目中可以有相關store數(shù)據(jù),建議從自己項目出發(fā)進行修改
      store.state.record.latitude
    )
    map.centerAndZoom(point, 18)
    map.enableScrollWheelZoom(true)
    var label = new window.BMapGL.Label('疲勞地點', {
      position: point, // 設置標注的地理位置
      offset: new window.BMapGL.Size(0, 0) // 設置標注的偏移量
    })
    // 添加標簽
    map.addOverlay(label) // 將標注添加到地圖中
    label.setStyle({
      fontSize: '32px',
      color: 'red'
    })
    var marker = new window.BMapGL.Marker(point) // 創(chuàng)建標注
    map.addOverlay(marker) // 將標注添加到地圖中
    var scaleCtrl = new window.BMapGL.ScaleControl() // 添加比例尺控件
    map.addControl(scaleCtrl)
    var zoomCtrl = new window.BMapGL.ZoomControl() // 添加縮放控件
    map.addControl(zoomCtrl)
    var cityCtrl = new window.BMapGL.CityListControl() // 添加城市列表控件
    map.addControl(cityCtrl)
  },
  setup() {
    // const store = useStore()
    // let latitude = ref('')
    // let longitude = ref('')
    // console.log(store.state.record.latitude)
    // latitude.value = store.state.record.latitude
    // longitude.value = store.state.record.longitude
    // return {
    //   latitude,
    //   longitude
    // }
  }
}
</script>
<style scoped>
.bmap {
  width: 800px;
  height: 600px;
  border: 1px solid #000;
}
</style>

顯示結(jié)果:

總結(jié)

感覺這種方法是最快速的,關鍵點有一個就是new window.BMapGL.Map,前面要加window。然后其他用法都可以在官方文檔中查到。

鏈接:
https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/getkey

到此這篇關于vue3中使用百度地圖的文章就介紹到這了,更多相關vue3使用百度地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論