Google 地图 API v3 事件单击在单击MarkerClusterer 时引发?

发布于 2024-09-02 13:20:43 字数 1047 浏览 7 评论 0 原文

我有一个 Google Map API v3 map 对象maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/" rel="nofollow noreferrer">MarkerClusterer。我有一个函数需要在我们单击地图时运行,它被注册为:

google.maps.event.addListener(map, 'click', function (event) {
    CallMe(event.latLng);
});

所以我的问题如下:当我单击 MarkerClusterer 集群时,它的行为不像标记,而不是引发地图上的点击事件,但仅引发来自标记的事件,它调用地图上的点击。

为了测试这一点,我从 markerclusterer 点击生成了一个警报:

google.maps.event.addListener(markerClusterer, "clusterclick", function (cluster) {
    alert('MarkerClusterer click event');
}); 

因此 clusterclick 在地图对象的点击事件之后出现。然后我无法删除地图对象的侦听器作为解决方案。有没有办法测试地图的点击事件中是否有clusterer点击?或者一种复制标记行为并且在调用 clustererclick 时不引发地图点击事件的方法?谷歌和文档对我没有帮助。

I have a Google Map API v3 map object on a page that uses MarkerClusterer. I have a function that need to run when we click on the map to it is registered as:

google.maps.event.addListener(map, 'click', function (event) {
    CallMe(event.latLng);
});

So my problem is as follows: When I click on a cluster of MarkerClusterer instead of behaving like a marker and not raise the click event on the map but only the one from the marker it calls the click from the map.

To test this I have generated an alert from the markerclusterer click:

google.maps.event.addListener(markerClusterer, "clusterclick", function (cluster) {
    alert('MarkerClusterer click event');
}); 

So the clusterclick rises after the click event of map object. I then can't remove the listener of map object as a solution. Is there any way to test if there was a clusterer click in the click event of the map? Or a way to replicate the marker behaviour and do not raise the click event of map when clustererclick is called? Google and documentation didn’t help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等风来 2024-09-09 13:20:44

我找到了另一个可能有效的解决方案。在markerclusterer.js中找到以下代码,

google.maps.event.addDomListener(this.div_, 'click', function() {
  that.triggerClusterClick();
});

并将其更改为:

google.maps.event.addDomListener(this.div_, 'click', function(ev) {
  ev.cancelBubble = true;
  if (ev.stopPropagation) {
    ev.stopPropagation();
  }
  that.triggerClusterClick();
});

根据Google的Martin Matysiak的说法,“这称为事件传播,事件总是在DOM层次结构中“冒泡”。您可以使用[该]代码阻止这种情况发生”。

请参阅:https://groups.google.com /forum/#!topic/google-maps-js-api-v3/PGeNrzv_SAs

I found another solution that may work. Find the following code inside markerclusterer.js:

google.maps.event.addDomListener(this.div_, 'click', function() {
  that.triggerClusterClick();
});

and change it to:

google.maps.event.addDomListener(this.div_, 'click', function(ev) {
  ev.cancelBubble = true;
  if (ev.stopPropagation) {
    ev.stopPropagation();
  }
  that.triggerClusterClick();
});

According to Martin Matysiak of Google "this is called event propagation, the event always "bubbles" up in the DOM hierarchy. You can stop this from happening with [that] code."

See: https://groups.google.com/forum/#!topic/google-maps-js-api-v3/PGeNrzv_SAs

东走西顾 2024-09-09 13:20:44

我使用了这种方法,受到其他答案的启发,但没有复制粘贴库代码或更改库本身:

originalOnAdd = ClusterIcon.prototype.onAdd;
ClusterIcon.prototype.onAdd = function() {
    originalOnAdd.call(this);

    google.maps.event.addDomListener(this.div_, 'click', function (ev) {
        ev.cancelBubble = true;
        if (ev.stopPropagation)
            ev.stopPropagation();
    });
}

I used this method, inspired by the other answers, but without copy-pasting library code or changing the library itself:

originalOnAdd = ClusterIcon.prototype.onAdd;
ClusterIcon.prototype.onAdd = function() {
    originalOnAdd.call(this);

    google.maps.event.addDomListener(this.div_, 'click', function (ev) {
        ev.cancelBubble = true;
        if (ev.stopPropagation)
            ev.stopPropagation();
    });
}
断爱 2024-09-09 13:20:43

这是可行的,但我仍然愿意接受其他更好的答案。

我使用 setTimeout 将地图点击事件中继为 javascript 应该执行的最后一件事,并使用布尔值检查之前是否引发了 clustererclick ,如下所示:

google.maps.event.addListener(map, 'click', function (event) {
    setTimeout(function () {
        if (!clusterClicked) {
            CallMe(event.latLng);
            alert('Map click executed');
        }
        else {
            clusterClicked = false;
            alert('ClusterClicked map click not executed');
        }
    }, 0);
});

google.maps.event.addListener(markerClusterer, "clusterclick", function (cluster) {
    clusterClicked = true;
}); 

Here is something that works but I'm still open to other better answers.

I use a setTimeout to relay the map click event to be the last thing javascript should execute and check with a boolean if clustererclick was raised before with something like this :

google.maps.event.addListener(map, 'click', function (event) {
    setTimeout(function () {
        if (!clusterClicked) {
            CallMe(event.latLng);
            alert('Map click executed');
        }
        else {
            clusterClicked = false;
            alert('ClusterClicked map click not executed');
        }
    }, 0);
});

google.maps.event.addListener(markerClusterer, "clusterclick", function (cluster) {
    clusterClicked = true;
}); 
你的呼吸 2024-09-09 13:20:43

我遇到了同样的问题和最终得到的解决方案:

ClusterIcon.prototype.onAdd = function() {
    this.div_ = document.createElement('DIV');
    if (this.visible_) {
        var pos = this.getPosFromLatLng_(this.center_);
        this.div_.style.cssText = this.createCss(pos);
        this.div_.innerHTML = this.sums_.text;
    }

    var panes = this.getPanes();
    panes.overlayMouseTarget.appendChild(this.div_);

    var that = this;
    google.maps.event.addDomListener(this.div_, 'click', function(e) {
        =======> e.stopImmediatePropagation(); //<====================
        that.triggerClusterClick();
    });
};

我讨厌这样做,但另一方面“扩展”外部库是正常的吗?

I had same problems and the solution i ended up with:

ClusterIcon.prototype.onAdd = function() {
    this.div_ = document.createElement('DIV');
    if (this.visible_) {
        var pos = this.getPosFromLatLng_(this.center_);
        this.div_.style.cssText = this.createCss(pos);
        this.div_.innerHTML = this.sums_.text;
    }

    var panes = this.getPanes();
    panes.overlayMouseTarget.appendChild(this.div_);

    var that = this;
    google.maps.event.addDomListener(this.div_, 'click', function(e) {
        =======> e.stopImmediatePropagation(); //<====================
        that.triggerClusterClick();
    });
};

I hate doing this but in other hand it's normal to "extend" external libraries?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文