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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue中想要mock數(shù)據(jù)在線上環(huán)境生效如何操作
本文主要介紹了在配置了mock數(shù)據(jù)之后在線上環(huán)境使用,主要通過在main.ts文件中注冊和vite.config.ts文件夾中配置插件來實現(xiàn),感興趣的可以了解一下2025-01-01javascript中Set、Map、WeakSet、WeakMap區(qū)別
這篇文章主要介紹了javascript中Set、Map、WeakSet、WeakMap區(qū)別,需要的朋友可以參考下2022-12-12vue實現(xiàn)鼠標移過出現(xiàn)下拉二級菜單功能
這篇文章主要介紹了vue實現(xiàn)鼠標移過出現(xiàn)下拉二級菜單功能,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-12-12