需求如下:百度js地图使用自定义信息窗口来自定义地图弹窗,点击弹窗内相应的dom能够触发vue的methods
---------------------------------------------代码------------------------------------------
InfoBox触发事件:相应的参数请参照文档
onMarkerClick: function(e) {
var _this = this
if (_this.infoBox) {
_this.infoBox.close()
}
_this.infoBox = new BMapLib.InfoBox(this.map, _this.getInfoBoxContent(e.target.data), {
boxStyle: {
// opacity: '0.9',
background: '#ffffff',
width: '300px',
height: '250px'
},
offset: _this.markerSize,
closeIconUrl: closeIcon,
closeIconMargin: '8px 8px 0 0',
enableAutoPan: true
// align: INFOBOX_AT_TOP
})
_this.infoBox.enableAutoPan()
_this.infoBox.open(e.target)
},
InfoBox创建:该方法是vue methods里面的, 参数data是自己业务数据,里面html拼接根据自己需求来修改,可直接看到‘查看详情’标签 onclick方法对应的是vue mounted注册的window方法
// 自定义信息窗体显示内容
getInfoBoxContent(data) {
var headStr = '<div class="div-name">' + data.deviceName + '</div>'
var bodyStr = ''
for (var i = 0; i < data.factors.length; i++) {
bodyStr += '<div class="div-factor">' + data.factors[i].key + ':' +
data.factors[i].value + '</div>'
}
var footStr = '<div class="div-btn"><a onclick="onInfoWindowClick()">查看详情></a>
</div>'
var html = `<div class="infoBoxContent">${headStr}${bodyStr}${footStr}</div>`
return html
}
在vue的mounted周期里面注册window方法,并在里面调用vue的方法,
mounted() {
this.createMap()
this.showMarker()
var _this = this
window.onInfoWindowClick = function() {
// alert('++++')
_this.toDeviceDetail()
}
}