vue百度地圖實(shí)現(xiàn)自定義彈框樣式
引入百度地圖
在index.html文件中引入百度地圖 申請(qǐng)百度密鑰
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=密鑰"></script> <script type="text/javascript" src="http://api.map.baidu.com/library/InfoBox/1.2/src/InfoBox_min.js" ></script>
在webpack.base.conf.js文件內(nèi)添加 externals 選項(xiàng)
無需再用import引入
module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
externals: {
'BMap': 'BMap',
'BMapLib': 'BMapLib'
},
output: {}
}
地圖開發(fā)代碼參考
<template>
<!--地圖容器-->
<div id="map_box"></div>
</template>
<script>
export default {
name:'',
data () {
return {
map: null,
infoBox: null,
}
},
mounted(){
this.showMap()
//動(dòng)態(tài)添加的dom 調(diào)用vue事件
let _this = this
window.imageClick= function() {
_this.vueImageClick()
}
},
methods: {
/**
* 地圖展示
*/
showMap() {
this.map = new BMap.Map('map_box')//對(duì)應(yīng)地圖容器id
let centerPoint = new BMap.Point(113.302114, 22.452695)
this.map.centerAndZoom(centerPoint, 15)
this.map.enableScrollWheelZoom(true)
this.map.enableDoubleClickZoom(true)
/*
//地圖樣式
this.map.setMapStyle({
styleJson: []
})*/
this.markerPoint()
},
/**
* 添加地圖marker
*/
markerPoint() {
let _this = this
this.map.clearOverlays()
let iconImage = new BMap.Icon(require('../assets/icon/menuIcon.png'), new BMap.Size(24, 24))
let point = new BMap.Point(113.302114, 22.452695)
let marker = new BMap.Marker(point, { icon: iconImage }) // 創(chuàng)建標(biāo)注
marker.addEventListener('click', () => {
//關(guān)閉其他標(biāo)記點(diǎn)的彈框
if (_this.infoBox != null) {
_this.infoBox.close()
}
_this.markerPointClick(113.302114, 22.452695)
})
_this.map.addOverlay(marker)
},
/**
* 點(diǎn)擊marker彈出信息框
*/
markerPointClick(lat, lng) {
let _this = this
let html = '<div class="infoBoxContent">\n' +
'<p οnclick="imageClick()">按鈕</p>\n' +
'</div>'
let opts = {
boxStyle: {
width: '435px',
height: '233px'
// margin: '30px 0',
},
closeIconMargin: '25px 5px 0 0',
closeIconUrl: require('../assets/icon/close_btn.png'),
enableAutoPan: true,
align: INFOBOX_AT_TOP
}
this.infoBox = new BMapLib.InfoBox(this.map, html, opts)
/*this.infoBox._getCloseIcon = function() {
return ''
}*/
let point = new BMap.Point(lat, lng)
let marker = new BMap.Marker(point)
this.infoBox.open(marker)
},
vueImageClick(){
console.log('彈框中按鈕的點(diǎn)擊事件')
}
}
}
</script>
<style scoped>
#map_box{
width: 100%;
height: 650px;
}
</style>
<style>
/*自定義彈框樣式--需要寫在公共樣式中才起作用*/
.infoBoxContent{
width: 435px;
height: 233px;
background-color: cyan;
}
</style>
效果展示
上面代碼部分未貼動(dòng)態(tài)加的節(jié)點(diǎn)與相關(guān)樣式,可以自行添加

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue3開啟攝像頭并進(jìn)行拍照的實(shí)現(xiàn)示例
本文主要介紹了vue3開啟攝像頭并進(jìn)行拍照的實(shí)現(xiàn)示例,主要是使用navigator.mediaDevices.getUserMedia這個(gè)API來實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01
Vue純前端實(shí)現(xiàn)導(dǎo)出excel中的圖片與文件超鏈接
這篇文章主要為大家詳細(xì)介紹了Vue如何純前端實(shí)現(xiàn)導(dǎo)出excel中的圖片與文件超鏈接,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下2025-03-03
Vue實(shí)現(xiàn)自動(dòng)檢測(cè)以及版本的更新
當(dāng)用戶在當(dāng)前站點(diǎn)停留時(shí)間比較長(zhǎng),中途站點(diǎn)進(jìn)行升級(jí)更新之后,用戶如果不刷新頁面就任然停留在舊的頁面里,如何讓用戶收到一個(gè)提示,引導(dǎo)用戶進(jìn)行更新操作呢?下面給大家介紹如何站點(diǎn)更新如何在生產(chǎn)環(huán)境提示用戶更新,進(jìn)行頁面刷新操作,核心原理其實(shí)很簡(jiǎn)單2023-03-03
vue+spring boot實(shí)現(xiàn)校驗(yàn)碼功能
這篇文章主要為大家詳細(xì)介紹了vue+spring boot實(shí)現(xiàn)校驗(yàn)碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Vue中Vue-Baidu-Map基本使用方法實(shí)例
最近有一個(gè)項(xiàng)目需要用到地圖來展示位置并進(jìn)行數(shù)據(jù)交互,用vue-baidu-map實(shí)現(xiàn)出來,下面這篇文章主要給大家介紹了關(guān)于Vue中Vue-Baidu-Map基本使用的相關(guān)資料,需要的朋友可以參考下2023-03-03
詳解element-ui表格的合并行和列(非常細(xì)節(jié))
最近在需求中遇到了elementUI合并行,索性給大家整理下,這篇文章主要給大家介紹了關(guān)于element-ui表格的合并行和列的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06

