Google Maps API v3:允许在自定义OverlayView上使用默认上下文菜单

我有一张带有自定义叠加层的地图(基于https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/overlay-popup)。

自定义叠加层内容包括一个链接/锚标记,并且我希望用户右键单击该链接并选择“在新标签页中打开” ,但是右键单击会被地图取消我无法弄清楚如何防止这种行为。

如果将上面链接的自定义叠加层的示例与默认的信息窗口https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/infowindow-simple进行比较,您会注意到,右键单击“ Hello World”文本时,自定义叠加层不会显示上下文菜单,而信息窗口不会显示上下文菜单。在开发工具中,我注意到信息窗口上的事件处理程序以某种方式允许上下文菜单(删除该处理程序将停止出现上下文菜单),但是由于它在缩小的Google Maps代码中,因此我无法理解

我尝试了以下方法:

google.maps.event.addListener(map,'rightclick',function (e) {
    var event = e.ya;
    var element = event.target;
    if (element.nodename === "A") {
        event.stopImmediatePropagation();
        event.stopPropagation();
        return true;
    }
});

代码已执行,但是仍然没有上下文菜单。取而代之的是,它会破坏地图上的某些内容,然后在地图上随鼠标移动,好像我仍然按下鼠标一样(好像我阻止了mouseup处理程序)。

我还尝试在自定义叠加层(https://developers.google.com/maps/documentation/javascript/reference/overlay-view#OverlayView.preventMapHitsAndGesturesFrom)上设置preventMapHitsFrom,这使得以上内容不再触发,但仍然没有上下文菜单。

我还能够自己附加一个事件处理程序(对不起jQuery):

$(document).on("contextmenu",".map-popup__link",function (e) {
    e.stopImmediatePropagation();
    return true;
});

但是再次不确定如何防止事件被取消。我还尝试在同一元素上触发一个新事件,但这只是创建了一个循环(显然)而没有解决问题。

基于https://stackoverflow.com/a/7414594/1397352 我已经将Popup.prototype.onAdd函数修改为

Popup.prototype.onAdd = function () {
    this.getPanes().floatPane.appendChild(this.containerDiv);

    this.getPanes().overlayMousetarget.appendChild(this.containerDiv);

    // set this as locally scoped var so event does not get confused
    var me = this;

    // Add a listener - we'll accept clicks anywhere on this div,but you may want
    // to validate the click i.e. verify it occurred in some portion of your overlay.
    google.maps.event.addDomListener(this.containerDiv,'contextmenu',function () {
        google.maps.event.trigger(me,'contextmenu');
    });
};

事件处理程序中的断点被击中,但再次没有显示上下文菜单。

有人能使用自定义叠加层吗?

lyx123hsh 回答:Google Maps API v3:允许在自定义OverlayView上使用默认上下文菜单

由于MrUpsidown的评论,我得以在(存档的)信息框库中找到一个解决方案:https://github.com/googlemaps/v3-utility-library/blob/master/archive/infobox/src/infobox.js#L231

我第一次尝试时似乎很接近,但应该设置event.cancelBubble = true;

最终解决方案:

Popup.prototype.onAdd = function () {
    this.getPanes().floatPane.appendChild(this.containerDiv);

    // This handler allows right click events on anchor tags within the popup
    var allowAnchorRightClicksHandler = function (e) {
        if (e.target.nodeName === "A") {
            e.cancelBubble = true;
            if (e.stopPropagation) {
                e.stopPropagation();
            }
        }
    };
    this.contextListener_ = google.maps.event.addDomListener(this.containerDiv,"contextmenu",allowAnchorRightClicksHandler);
};

Popup.prototype.onRemove = function () {
    if (this.contextListener_) {
        google.maps.event.removeListener(this.contextListener_);
        this.contextListener_ = null;
    }
    if (this.containerDiv.parentElement) {
        this.containerDiv.parentElement.removeChild(this.containerDiv);
    }
};

有关其余的弹出式代码,请参见https://developers-dot-devsite-v2-prod.appspot.com/maps/documentation/javascript/examples/overlay-popup

本文链接:https://www.f2er.com/3127277.html

大家都在问