vue3中使用百度地圖的簡單步驟
前言
最近一個項目要用到地圖,因為微信小程序用的也是百度地圖,所以想著網(wǎng)頁端也用百度地圖,在網(wǎng)上查了很多方法,包括引入百度地圖第三方庫,還是有問題,發(fā)現(xiàn)最簡單的方法就是在index.html中引入script,然后直接在相關頁面肝就完事。
一、申請ak
在百度開發(fā)者平臺上面申請,其他博客都可以看到相關申請過程,這里就不多述。

因為還處于開發(fā)調試狀態(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>顯示結果:

總結
感覺這種方法是最快速的,關鍵點有一個就是new window.BMapGL.Map,前面要加window。然后其他用法都可以在官方文檔中查到。
鏈接:
https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/getkey
到此這篇關于vue3中使用百度地圖的文章就介紹到這了,更多相關vue3使用百度地圖內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Vue3實現(xiàn)跨頁面?zhèn)髦档膸追N常見方法
在Vue 3中,跨頁面?zhèn)髦悼梢酝ㄟ^多種方式實現(xiàn),具體選擇哪種方法取決于應用的具體需求和頁面間的關系,本文列舉了幾種常見的跨頁面?zhèn)髦捣椒?感興趣的同學跟著小編來看看吧2024-04-04
在Vue-cli里應用Vuex的state和mutations方法
今天小編就為大家分享一篇在Vue-cli里應用Vuex的state和mutations方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue使用formData時候傳遞參數(shù)是個空值的情況處理
這篇文章主要介紹了vue使用formData時候傳遞參數(shù)是個空值的情況處理,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05

