百度地图悬浮事件

在这里插入图片描述

  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('打印点击事件')
                    })
                })
            },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

骨子里的偏爱

上传不易,还请多多支持。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值