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)
無(wú)需再用import引入
module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
externals: {
'BMap': 'BMap',
'BMapLib': 'BMapLib'
},
output: {}
}
地圖開(kāi)發(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)文章
echarts鼠標(biāo)覆蓋高亮顯示節(jié)點(diǎn)及關(guān)系名稱詳解
下面小編就為大家分享一篇echarts鼠標(biāo)覆蓋高亮顯示節(jié)點(diǎn)及關(guān)系名稱詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
使用Vite搭建vue3+TS項(xiàng)目的實(shí)現(xiàn)步驟
本文主要介紹了使用Vite搭建vue3+TS項(xiàng)目的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
vue3+vite項(xiàng)目中顯示SVG圖片的實(shí)現(xiàn)
vite-plugin-svg-icons是一個(gè)Vite插件,其作用是將SVG圖標(biāo)文件轉(zhuǎn)換為Vue組件,本文主要介紹了vue3+vite項(xiàng)目中顯示SVG圖片的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Vue實(shí)現(xiàn)動(dòng)態(tài)路由導(dǎo)航的示例
本文主要介紹了Vue實(shí)現(xiàn)動(dòng)態(tài)路由導(dǎo)航的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
vue在App.vue文件中監(jiān)聽(tīng)路由變化刷新頁(yè)面操作
這篇文章主要介紹了vue在App.vue文件中監(jiān)聽(tīng)路由變化刷新頁(yè)面操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue+elementUi中的table實(shí)現(xiàn)跨頁(yè)多選功能(示例詳解)
最近在開(kāi)發(fā)工業(yè)品超市的后臺(tái)系統(tǒng),遇到一個(gè)需求,就是實(shí)現(xiàn)在一個(gè)table表格中多選數(shù)據(jù),在網(wǎng)上查了好多,有些方法真的是無(wú)語(yǔ),下面通過(guò)本文給大家分享vue+elementUi中的table實(shí)現(xiàn)跨頁(yè)多選功能,感興趣的朋友跟隨小編一起看看吧2024-05-05
Vue Router路由動(dòng)態(tài)緩存組件使用詳解
這篇文章主要介紹了Vue Router路由動(dòng)態(tài)緩存組件使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-04-04
VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例
本篇文章主要介紹了VUE利用vuex模擬實(shí)現(xiàn)新聞點(diǎn)贊功能實(shí)例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

