vue3中使用百度地圖的簡單步驟
前言
最近一個項(xiàng)目要用到地圖,因?yàn)槲⑿判〕绦蛴玫囊彩前俣鹊貓D,所以想著網(wǎng)頁端也用百度地圖,在網(wǎng)上查了很多方法,包括引入百度地圖第三方庫,還是有問題,發(fā)現(xiàn)最簡單的方法就是在index.html中引入script,然后直接在相關(guān)頁面肝就完事。
一、申請ak
在百度開發(fā)者平臺上面申請,其他博客都可以看到相關(guān)申請過程,這里就不多述。
因?yàn)檫€處于開發(fā)調(diào)試狀態(tài),所以白名單寫的是**。
二、使用步驟
1.在public下index.html引入相關(guān)script
<script type="text/javascript" src="https://api.map.baidu.com/api?v=1.0&&type=webgl&ak=your_ak" ></script>
2.在相關(guān)頁面編寫代碼
代碼如下(示例):
<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,//這里本人項(xiàng)目中可以有相關(guān)store數(shù)據(jù),建議從自己項(xiàng)目出發(fā)進(jìn)行修改 store.state.record.latitude ) map.centerAndZoom(point, 18) map.enableScrollWheelZoom(true) var label = new window.BMapGL.Label('疲勞地點(diǎn)', { position: point, // 設(shè)置標(biāo)注的地理位置 offset: new window.BMapGL.Size(0, 0) // 設(shè)置標(biāo)注的偏移量 }) // 添加標(biāo)簽 map.addOverlay(label) // 將標(biāo)注添加到地圖中 label.setStyle({ fontSize: '32px', color: 'red' }) var marker = new window.BMapGL.Marker(point) // 創(chuàng)建標(biāo)注 map.addOverlay(marker) // 將標(biāo)注添加到地圖中 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é)
感覺這種方法是最快速的,關(guān)鍵點(diǎn)有一個就是new window.BMapGL.Map
,前面要加window。然后其他用法都可以在官方文檔中查到。
鏈接:
https://lbsyun.baidu.com/index.php?title=jspopularGL/guide/getkey
到此這篇關(guān)于vue3中使用百度地圖的文章就介紹到這了,更多相關(guān)vue3使用百度地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue實(shí)現(xiàn)路由懶加載的詳細(xì)步驟
路由懶加載是指在用戶實(shí)際訪問某個特定路由時,才加載該路由相關(guān)組件的機(jī)制,這種方式可以顯著減少初始加載時的 JavaScript 文件大小,從而提高應(yīng)用的加載速度,所以本文給大家介紹了Vue實(shí)現(xiàn)路由懶加載的詳細(xì)步驟,需要的朋友可以參考下2024-11-11vue中想要mock數(shù)據(jù)在線上環(huán)境生效如何操作
本文主要介紹了在配置了mock數(shù)據(jù)之后在線上環(huán)境使用,主要通過在main.ts文件中注冊和vite.config.ts文件夾中配置插件來實(shí)現(xiàn),感興趣的可以了解一下2025-01-01vue路由守衛(wèi)+登錄態(tài)管理實(shí)例分析
這篇文章主要介紹了vue路由守衛(wèi)+登錄態(tài)管理,結(jié)合實(shí)例形式分析了vue路由守衛(wèi)與登錄態(tài)管理相關(guān)操作步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-05-05javascript中Set、Map、WeakSet、WeakMap區(qū)別
這篇文章主要介紹了javascript中Set、Map、WeakSet、WeakMap區(qū)別,需要的朋友可以參考下2022-12-12vue mint-ui學(xué)習(xí)筆記之picker的使用
本篇文章主要介紹了vue mint-ui學(xué)習(xí)筆記之picker的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10vue實(shí)現(xiàn)鼠標(biāo)移過出現(xiàn)下拉二級菜單功能
這篇文章主要介紹了vue實(shí)現(xiàn)鼠標(biāo)移過出現(xiàn)下拉二級菜單功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12