使用百度地图完成【自定义图标】【波纹动画】【点击图标事件】【含完整代码】

使用百度地图完成【自定义图标】【波纹动画】【点击图标事件】功能

最终效果总览,文章末尾git仓库

1、自定义标点
2、标点有波纹动画
3、点击标点有不同的弹框
请添加图片描述

技术栈

vue3,ts,less

需求

UI图↓
在这里插入图片描述
1、图标自定义
2、图标波纹动画
3、点击图标弹出相关信息

需求分析

1、为什么适用百度地图?
业务是为某区域做安全预警,整套系统都适用百度的经纬度,因此为图方便直接适用百度地图,而不是echarts、高德等。
2、依赖百度地图的图标自定义文档、等
相关文档:百度地图的图标自定义文档
3、弃用百度地图的3D画面(虽然真的很好看),原因如下
在这里插入图片描述

相关文档:百度地图mapv
4、确定思路:将初始地图渲染出来,引入自定义图标,围绕图标用css画波纹,添加点击事件
5、为数据大屏需要,地图要改底色

参考

查阅了以下文档,提供了很大的帮助
卡夫卡的baby——CSS3 实现圆圈动态发光特效动画的制作
云端的幻影——百度地图添加自定义图标标注以及自定义动画效果

关键代码

无论是标点的波纹、还是点击事件的弹出框,都是通过对dom绑定自己的css样式实现。
其中抓取百度地图的dom废了一番功夫
本次需求因为有筛选项,所以points要先filter再遍历
index.vue

/**
         * @method 为标点增加波纹
         * tip:不加setTimeout的话,point.markers.domElement是null
         */
        const addWaves = () => {
            setTimeout(() => {
                myPoints.filter((point: MyPoints) => {
                    if (current.type === 0) {
                        return true
                    } else {
                        return point.info.scene === current.type
                    }
                }).forEach((point: MyPoints) => {
                    let divElement = document.createElement('div');
                    divElement.className = 'before';
                    let divElement2 = document.createElement('div');
                    divElement2.className = 'after';
                    point.markers.domElement.innerHtml = point.markers.domElement.childNodes[0];
                    point.markers.domElement.appendChild(divElement);
                    point.markers.domElement.appendChild(divElement2);
                    point.markers.domElement.className = '';
                    point.markers.domElement.className = `dot ${point.info?.color || ""}`;
                    point.markers.domElement.style.overflow = '';
                    point.markers.domElement.firstChild.style.position = 'relative';
                    point.markers.domElement.firstChild.style.zIndex = '5';
                })
            }, 1000);
        }
        /**
         * @method 刷新标点
         */
        const initPoints = () => {
            myPoints.filter((point: MyPoints) => {
                if (current.type === 0) {
                    return true
                } else {
                    return point.info.scene === current.type
                }
            }).forEach((point: MyPoints) => {
                const pt = new BMapGL.Point(point.lng, point.lat);
                const myIcon = new BMapGL.Icon(
                    pointDist[point.info.urgency]["point"],
                    new BMapGL.Size(22, 22)
                );
                const markers = new BMapGL.Marker(pt, { icon: myIcon });
                // 点击事件
                markers.addEventListener("click", () => {
                    //创建信息窗口
                    const sContent = `<div class='infoBox'>
                                        <div>设备编号:设备编号</div>
                                        <div>设备名称:设备名称</div>
                                        <div>设备类型:设备类型</div>
                                        <div>设备地址:设备地址</div>
                                        <div>告警时间:告警时间</div>
                                      </div>`;
                    var opts = {
                        width: 180,
                        title: `<div class='placebox'>${point.info?.urgencyLabel || ''}</div>`,
                        offset: new BMapGL.Size(120, 125),
                        customContent: sContent,
                        enableMessage: false,
                    };
                    current.bgColor = point.info?.color || "gray"
                    const infoWindow = new BMapGL.InfoWindow(sContent, opts);
                    infoWindow.setStyle;
                    markers.openInfoWindow(infoWindow);

                    // 根据情况配置不同的背景
                    nextTick(() => {
                        const BMap_bubble_pop: any = document.getElementsByClassName('BMap_bubble_pop')[0]
                        if (BMap_bubble_pop) {
                            BMap_bubble_pop.className = `BMap_bubble_pop set-${current.bgColor}-bg`
                        } else {
                            // 第一次dom未创建完毕,用settimeout延迟取
                            setTimeout(() => {
                                document.getElementsByClassName('BMap_bubble_pop')[0].className = `BMap_bubble_pop set-${current.bgColor}-bg`
                            }, 200)
                        }
                    })
                });
                point.markers = markers
                map.addOverlay(markers);

            });
        }

index.less

@width: 22px;
@height: 22px;
@orange: orange;
@gray: gray;
@red: red;
.dot {
    display: block;
    width: @width;
    height: @height;
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    z-index: 50;
    &>.before{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        position: absolute;
        top: 0px;
        left: 0px;
        &::after{
            content: '';
            display: block;
            width: 100%;
            height: 100%;
            border-radius: 50%;
            opacity: 0.7;
            animation: scale 1s infinite cubic-bezier(0,0,0.49,1.02);
        }
    }

    &>.after{
        width: 100%;
        height: 100%;
        opacity: 0.4;
        border-radius: 50%;
        position: absolute;
        top: 0px;
        left: 0px;
        animation: scales 1s infinite cubic-bezier(0,0,0.49,1.02);
    }

    &:hover {
        z-index: 100;
    }
    &>div {
        z-index: 3;
        top: 0;
        left: 0;
    }
}

.orange {
    &>.before::after {
        background-color: @orange;
    }
    &>.after {
        background-color: @orange;
    }
}

git 地址

地址

如果对您有帮助,请给我点个赞/给git仓库点个收藏,谢谢(比心)

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在Vue中使用百度地图添加自定义覆盖物,可以使用vue-baidu-map这个库来实现。在vue-baidu-map中,可以使用百度地图API中提供的Overlay类来创建自定义覆盖物。 以下是在vue-baidu-map中添加自定义覆盖物的步骤: 1. 安装vue-baidu-map 在终端中运行以下命令来安装vue-baidu-map: ``` npm install vue-baidu-map --save ``` 2. 注册vue-baidu-map组件 在Vue组件中注册vue-baidu-map组件: ``` import BaiduMap from 'vue-baidu-map' export default { components: { BaiduMap } } ``` 3. 在模板中添加地图 在模板中添加vue-baidu-map标签,并设置地图的中心点和缩放级别: ``` <template> <div> <BaiduMap :center="center" :zoom="zoom"></BaiduMap> </div> </template> <script> export default { data() { return { center: {lng: 116.404, lat: 39.915}, zoom: 15 } } } </script> ``` 4. 创建自定义覆盖物 在Vue组件的mounted()生命周期函数中,创建自定义覆盖物: ``` mounted() { const map = this.$refs.baiduMap.getBMap() const BMap = window.BMap const point = new BMap.Point(116.404, 39.915) const myIcon = new BMap.Icon('path/to/myIcon.png', new BMap.Size(30, 30)) const marker = new BMap.Marker(point, {icon: myIcon}) map.addOverlay(marker) } ``` 在上面的代码中,我们创建了一个自定义覆盖物,它是一个图片标记,位于地图的(116.404, 39.915)处。我们使用BMap.Icon类来创建一个自定义图标,并使用BMap.Marker类来创建一个标记,并将它添加到地图中。 5. 在模板中显示自定义覆盖物 在模板中添加自定义覆盖物的容器,并使用CSS样式设置容器的位置和大小: ``` <template> <div> <BaiduMap :center="center" :zoom="zoom"></BaiduMap> <div class="marker-container"></div> </div> </template> <style> .marker-container { position: absolute; left: 50%; top: 50%; width: 30px; height: 30px; margin-left: -15px; margin-top: -15px; } </style> ``` 在上面的代码中,我们添加了一个名为"marker-container"的DIV容器,用于显示自定义覆盖物。我们使用CSS样式将容器放置在地图的中心位置,并设置容器的大小为30x30,该大小与自定义图标的大小相同。 6. 显示水波纹效果 要在自定义覆盖物上显示水波纹效果,可以使用CSS3动画JavaScript。以下是实现水波纹效果的代码: ``` mounted() { const map = this.$refs.baiduMap.getBMap() const BMap = window.BMap const point = new BMap.Point(116.404, 39.915) const myIcon = new BMap.Icon('path/to/myIcon.png', new BMap.Size(30, 30)) const marker = new BMap.Marker(point, {icon: myIcon}) map.addOverlay(marker) const ripple = document.createElement('div') ripple.classList.add('ripple') marker.getContent().appendChild(ripple) marker.addEventListener('click', e => { const x = e.offsetX const y = e.offsetY ripple.style.left = x + 'px' ripple.style.top = y + 'px' ripple.classList.add('ripple-active') setTimeout(() => { ripple.classList.remove('ripple-active') }, 1000) }) } ``` 在上面的代码中,我们使用JavaScript创建了一个DIV元素,用于显示水波纹效果。我们将这个DIV元素添加到自定义覆盖物的容器中,并在自定义覆盖物上添加了一个点击事件监听器。当用户点击自定义覆盖物时,我们获取点击位置的坐标,并将水波纹DIV元素定位到该位置,并添加CSS3动画类"ripple-active"来显示水波纹效果。我们在1秒后从水波纹DIV元素中移除"ripple-active"类,以停止水波纹效果。 7. 添加CSS样式 最后,我们需要添加CSS样式来定义水波纹效果和自定义覆盖物的样式: ``` .marker-container { position: absolute; left: 50%; top: 50%; width: 30px; height: 30px; margin-left: -15px; margin-top: -15px; } .ripple { position: absolute; border-radius: 50%; width: 50px; height: 50px; background-color: rgba(255, 255, 255, 0.5); transform: translate(-50%, -50%); opacity: 0; transition: opacity 1s linear; } .ripple-active { opacity: 1; } ``` 在上面的代码中,我们定义了"marker-container"类来设置自定义覆盖物的样式,以及"ripple"和"ripple-active"类来设置水波纹效果的样式。我们使用"position: absolute"来定位元素,并使用"transform: translate(-50%, -50%)"将元素的中心点设置为其父元素的中心点。我们还使用"opacity"和"transition"属性来定义水波纹效果的透明度和过渡效果。 这样就可以在Vue中使用百度地图添加自定义覆盖物(水波纹)了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值