Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  99] [ 4]  / answers: 1 / hits: 15653  / 11 Years ago, tue, december 17, 2013, 12:00:00

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



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:



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.


More From » jquery

 Answers
12

Let's say you had your map object and it's called globalmap (you made as a new 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 MarkerWithLabel plugin, 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):



 var marker1 = new MarkerWithLabel({
position: homeLatLng,
draggable: true,
raiseOnDrag: true,
map: map,
labelContent: $425K,
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'); });

[#73701] Sunday, December 15, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kyla

Total Points: 77
Total Questions: 108
Total Answers: 111

Location: Grenada
Member since Mon, May 8, 2023
1 Year ago
;