addMarker ({ BMap, map }) {
var markerArr = [// 地址数据
{ title: '济南发展有限公司', point: '117.15120|36.62904', address: '汉峪金谷', tel: '10086', contact: '陈先生', color: 'red'},
{ title: '山东济南有限公司', point: '117.15220|36.63904', address: '历城区', tel: '12306', contact: '陈先生', color: 'green'},
{ title: '山东凌动有限公司', point: '117.15320|36.64904', address: '高新区', tel: '0521846555', contact: '陈先生', color: 'gray'}
]
var point = new Array() // 定义数组标注经纬信息
var marker = new Array() // 定义数组点对象信息
var info = new Array()// 定义悬浮提示信息
for (var i = 0; i < markerArr.length; i++) { // 遍历
var infoA = markerArr[i].point.split('|')[0]// 分割|
var infoB = markerArr[i].point.split('|')[1]
point[i] = new window.BMap.Point(infoA, infoB)
marker[i] = new window.BMap.Marker(point[i])
map.addOverlay(marker[i])
// marker[i].setAnimation(BMAP_ANIMATION_BOUNCE) // 设置的坐标的跳动
var label = new window.BMap.Label(markerArr[i].title, {offset: new window.BMap.Size(20, -10)})
// 设置 label 样式
label.setStyle({
padding: '3px 7px',
backgroundColor: markerArr[i].color,
color: '#fff',
borderColor: markerArr[i].color
})
marker[i].setLabel(label)
// info[i] = new window.BMap.InfoWindow("<p style='font-size:12px;line-height:1.8em;'" + markerArr[i].title + '</br>地址:' + markerArr[i].address + '</br> 电话:' + markerArr[i].tel + '</br> 联系人:' + markerArr[i].contact + '</p>')// 悬浮提示信息
info[i] = new window.BMap.InfoWindow(
"<div style='font-size:12px;line-height:1.8em;'>" +
'<div style="color:red;height:20px;border:1px solid green"">' +
markerArr[i].title + '</div>' +
'<div id="address_' + i + '" style="color:blue">' + markerArr[i].address + '</div>' +
'</div>')// 悬浮提示信息,通过id对应地址的DOM元素
}
// 给悬浮提示信息添加点击事件
// marker[0].addEventListener('mouseover', function () {
// this.openInfoWindow(info[0])// 悬浮监听提示方法
// })
// marker[1].addEventListener('mouseover', function () {
// this.openInfoWindow(info[1])// 悬浮监听提示方法
// })
// marker[2].addEventListener('mouseover', function () {
// this.openInfoWindow(info[2])// 悬浮监听提示方法
// })
// 悬浮监听提示方式 可用循环 实现
marker.forEach((element, index) => {
element.addEventListener('mouseover', function () {
this.openInfoWindow(info[index])
console.log('打印悬浮事件')
})
})
info.forEach((element, index) => {
element.addEventListener('click', function () {
// this.openInfoWindow(info[index])
console.log('打印点击事件')
})
})
},
百度地图悬浮事件
于 2023-05-09 09:38:19 首次发布