javascript 右键单击标记 - Google Maps V3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20634008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:55:20  来源:igfitidea点击:

Right Click on marker - Google Maps V3

javascriptjquerygoogle-mapsgoogle-maps-api-3right-click

提问by SeanCAtkinson

I've been trying to get a right click working on my markers but so far it seems that the event just isn't fired. As a workaround I'm currently detecting a right click on the map and then finding the nearest marker and triggering the right click method - obviously thats not great.

我一直在尝试对我的标记进行右键单击,但到目前为止,似乎该事件并未触发。作为一种解决方法,我目前正在检测地图上的右键单击,然后找到最近的标记并触发右键单击方法 - 显然那不是很好。

According to the docs, markers do have a right click event so I'm not sure what I'm doing wrong. https://developers.google.com/maps/documentation/javascript/reference#Marker

根据文档,标记确实有一个右键单击事件,所以我不确定我做错了什么。https://developers.google.com/maps/documentation/javascript/reference#Marker

google.maps.event.addListener(marker, 'rightclick', handleRightClick)

doesn't work but

不起作用但是

google.maps.event.addListener(map, 'rightclick', handleRightClick)

works fine. As does:

工作正常。就像:

google.maps.event.addListener(marker, 'click', handleRightClick)

I'm also using a custom marker plugin called markerWithLabel - http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.9/docs/reference.html- I thought I'd cracked it when I discovered that this didn't add an event listener for the right click but even when I added one in myself, still no luck. I added this at line 257:

我还使用了一个名为 markerWithLabel 的自定义标记插件 - http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerwithlabel/1.1.9/docs/reference.html- 我以为我'当我发现这没有为右键单击添加事件侦听器时,我破解了它,但即使我在自己中添加了一个,仍然没有运气。我在第 257 行添加了这个:

google.maps.event.addDomListener(this.eventDiv_, "rightclick", function(e) {
    if (me.marker_.getClickable()) {
        google.maps.event.trigger(me.marker_, "rightclick", e);
        cAbortEvent(e);
    }
})

Has anyone had similar issues or found better workarounds?

有没有人遇到过类似的问题或找到更好的解决方法?

Thanks in advance.

提前致谢。

采纳答案by ffflabs

Let's say you had your map object and it's called globalmap (you made as a new google.maps.Map)

假设你有你的地图对象,它被称为 globalmap (你作为一个新的 google.maps.Map 制作的)

This will add a marker on maps' center:

这将在地图的中心添加一个标记:

var newmarker=new google.maps.Marker({map:globalmap, position: globalmap.getCenter()});

this will listen to right click on the marker;

这将收听右键单击标记;

google.maps.event.addListener(newmarker,  'rightclick',  function(mouseEvent) { alert('Right click triggered'); });

EDIT:Regarding the MarkerWithLabelplugin, I can see that it's extending from google.maps.OverlayView, and passes in a marker in the marker_property of the object. This means you could still have your way doing (copying from the example):

编辑:关于MarkerWithLabel插件,我可以看到它是从 google.maps.OverlayView 扩展的,并在对象的marker_属性中传入一个标记。这意味着您仍然可以按照自己的方式做(从示例中复制):

 var marker1 = new MarkerWithLabel({
   position: homeLatLng,
   draggable: true,
   raiseOnDrag: true,
   map: map,
   labelContent: "5K",
   labelAnchor: new google.maps.Point(22, 0),
   labelClass: "labels", // the CSS class for the label
   labelStyle: {opacity: 0.75}
 });

and then add the right click behaviour as

然后将右键单击行为添加为

google.maps.event.addListener(marker1.marker_,  'rightclick',  function(mouseEvent) { alert('Right click triggered'); });

回答by Dr.Molle

Your last attempt:

您的最后一次尝试:

google.maps.event.addDomListener(this.eventDiv_, "rightclick",function(){/**/});

...will not work, because there is no rightclick-DOMMouseEvent.

...不会工作,因为没有rightclick-DOMMouseEvent。

You must observe the click-event of the eventDiv_and check if the button-property of the event is set to 2(when it does a right-click has been detected)

您必须观察click-eventeventDiv_并检查button事件的-property 是否设置为2(检测到右键单击时)

回答by geocodezip

The rightclick event works on a google.maps.Marker for me:

右键单击事件适用于 google.maps.Marker 对我来说:

  google.maps.event.addListener(marker, 'rightclick', function() {
    infowindow.setContent("<div style='width:100px'>rightclick</div>");
    infowindow.open(map,marker);
  });

working example

工作示例