谷歌地图多个maker标记点击显示当前信息弹窗infowindow

1、初始化好谷歌地图后,再创建一个infowindow对象,下面是自己封装的方法
this.infowindow = this.map.createInfoWindow({ pixelOffset: new google.maps.Size(-30, -40) })// 水平偏移量为0,垂直偏移量为-50

2、然后创建好maker后,监听点击事件,谷歌地图的maker标记没有对应的字段存放的,可以通过原型绑定,以下方法的创建对象,都是自定义封装,只需看对应方式

  this.infowindow = this.map.createInfoWindow({ pixelOffset: new google.maps.Size(-30, -40) })// 水平偏移量为0,垂直偏移量为-50

  let obj = {}
  obj.lat = v.addressLat
  obj.lng = v.addressLng

 let makerobj = { pos: { lat: obj.lat, lng: obj.lng }}
  let marker = this.map.addMaker(makerobj)
  marker.makerInfo = obj //通过绑定变量后,再点击事件里面 通过this指向拿到maker对象的绑定变量
  marker.addListener('click', function (event) {
       that.handleClickMaker(marker, this.makerInfo)
  });

3、infowindow弹窗的设置,踩坑多次,不懂其他的以下是验证的关键,自己测试,只有html字符串才能实现,不懂在vue遇到什么坑

  // marker点击事件
        async handleClickMaker (marker, info) {
            this.infoObj = { ...info }
            let url = this.infoObj.pictureUrl
            let picUrl = this.pictureUrl;
            if (url) {
                picUrl = await getPicUrl({
                    picUrl: url,
                });
            }

            this.infoDom = ` <div style=" height: max-content; width: 250px;background-color: #fff;box-sizing: border-box;padding:10px 20px;">
            <div style=" display: flex; align-items: center; width: 100%; height: 20px; font-size: 12px;">
                <span style="font-size: 12px;"><span style=" margin-right: 5px;">地点位置:</span>${this.infoObj.areaName}${this.infoObj.status == 1 ? "启用" : "停用"}】</span>
            </div>
            <div  style=" display: flex; align-items: center; width: 100%; height: 20px; font-size: 12px;">
                <span style="font-size: 12px;"><span style=" margin-right: 5px;">站点属性:</span>${this.getProperty(this.infoObj.property)}</span>
            </div>
            <div style=" font-size: 12px;">站点图片:</div>
            <div style="width: 100%; height: 100px;">
                <img style=" width: 100%; height: 100%;" src="${picUrl}" alt="">
            </div>
        </div>`
  
            this.infowindow.setPosition(marker.getPosition()); //设置弹窗位置
            this.infowindow.setContent(this.infoDom); // 赋值html

            // 如果当前 infowindow 是显示状态,则隐藏它
            if (this.infowindow.getMap()) {
                this.infowindow.close();
            }
            // 显示新的 infowindow,关键的一句
            this.infowindow.open(this.map.gmap);
            this.map.gmap.panTo(marker.getPosition()); //移动视图中心位置
        },
  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值