谷歌地图事件-获取点击元素的父级

Google maps event - get parent of clicked element

本文关键字:元素 事件 获取 谷歌地图      更新时间:2023-10-20

我有一个函数,可以用来创建新对象"claster"。在每个冲突是标记从谷歌地图API。我想点击这个标记并访问claster.icons[]。我正在函数声明中绑定点击事件。怎么做?

代码

function newClaster(_center){
        this.centerIco = _center;
        this.center = this.centerIco.getPosition();
        this.icons = [];
        this.icons.push(this.centerIco);
        this.addIcon = function(_icon){this.icons.push(_icon)};
        this.marker = new google.maps.Marker({
            position : this.center,
            icon : {
                url : 'map/circle.png',
                scaledSize : new google.maps.Size(40,40),
                size : new google.maps.Size(40,40),
            },
            map:map,
        });
        this.setCenter = function(ctr){
            this.centerIco = ctr;
            this.center = ctr.getPosition();
        }
        this.findCenter = function(){
            this.centerIco = this.icons[parseInt((this.icons.length)%2)];
            this.center = this.centerIco.getPosition();
        }
        google.maps.event.addListener(this.marker, 'click', function(){
            this.icons 
        });
    }

我正在绑定点击标记。

添加一个函数并在单击时将标记发送给它,然后您可以通过访问标记对象来获得图标:

 google.maps.event.addListener(this.marker, 'click', getIcons(this));
function getIcons(marker){
  console.log(marker.icons);
}