Javascript 谷歌地图API v3:允许自定义覆盖视图上的默认上下文菜单
我有一张带有自定义覆盖图的地图(基于) 自定义覆盖内容包括一个链接/锚定标记,我希望允许用户右键单击链接并选择“在新选项卡中打开”,但是右键单击会被地图取消,我无法确定如何防止这种行为 如果将上面链接的自定义覆盖示例与默认信息窗口进行比较,您会注意到,当右键单击“Hello World”文本时,自定义覆盖不会显示上下文菜单,而信息窗口会显示上下文菜单。在dev工具中,我注意到Info窗口上有一个事件处理程序,它以某种方式允许上下文菜单(删除该处理程序会阻止上下文菜单出现),但是由于它位于缩小的googlemaps代码中,我无法理解它 我尝试了以下方法:Javascript 谷歌地图API v3:允许自定义覆盖视图上的默认上下文菜单,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我有一张带有自定义覆盖图的地图(基于) 自定义覆盖内容包括一个链接/锚定标记,我希望允许用户右键单击链接并选择“在新选项卡中打开”,但是右键单击会被地图取消,我无法确定如何防止这种行为 如果将上面链接的自定义覆盖示例与默认信息窗口进行比较,您会注意到,当右键单击“Hello World”文本时,自定义覆盖不会显示上下文菜单,而信息窗口会显示上下文菜单。在dev工具中,我注意到Info窗口上有一个事件处理程序,它以某种方式允许上下文菜单(删除该处理程序会阻止上下文菜单出现),但是由于它位于缩小的g
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;
}
});
代码已执行,但仍然没有上下文菜单。相反,它会破坏地图上的某些东西,因为地图随后会随着鼠标移动,就好像我仍然按住鼠标一样(看起来我阻止了鼠标处理程序)
我还尝试在自定义覆盖()上设置preventMapHitsFrom
,这使得上面的内容不再触发,但仍然没有上下文菜单
我还可以自己附加一个事件处理程序(请原谅jQuery):
但同样不确定如何防止活动被取消。我还试图在同一个元素上触发一个新事件,但这只是创建了一个循环(显然)而没有解决问题
基于
我已将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');
});
};
事件处理程序中的断点被命中,但再次没有上下文菜单显示
是否有人能将此用于自定义覆盖?多亏了Upsidown先生的评论,我能够在(存档的)信息盒库中找到解决方案: 看起来我的第一次尝试很接近,但应该设置
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);
}
};
查看弹出代码的其余部分@MrUpsidown谢谢,一个链接可能会有所帮助,但我找到了库并找到了相关代码。
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);
}
};