--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
The right click event for the Map returns a MouseEvent which includes
the LatLng. I am using it in that instance.
However, the method also handles the right click for a Marker which
returns an Event not a MouseEvent. Event does not include a LatLng so I
have to make the calculation then.
http://code.google.com/apis/maps/documentation/javascript/reference.html
#Marker
I left this out of my code sniplet. This is my modification to GeoXml3
that adds the rightclick event to the marker.
if (parserOptions.rightClickFunction != null) {
google.maps.event.addListener(marker, 'rightclick',
parserOptions.rightClickFunction);
Okay, I’ve got. Hopefully, this will help someone else.
The best way to add the event listener is like this
google.maps.event.addListener(marker, 'rightclick', function(event) { markerRightClick(event, marker.getPosition()) });
Not like this
google.maps.event.addListener(marker, 'rightclick', markerRightClick);
Now you don’t have to keep a dummy OverlayView and deal with the projection like in other posts.
The rest of the code is like this
function mapRightClick(mouseEvent) {
clickedLatLng = mouseEvent.latLng; // Save for context menu “Zoom In”
showContextMenu(mouseEvent.pixel.x, mouseEvent.pixel.y);
}
function markerRightClick(event, LatLon) {
var x;
var y;
if (BrowserDetect.browser == "Explorer") { // Marker event for IE returns pixel in event as x and y
x = event.x;
y = event.y;
}
else if (BrowserDetect.browser == "Mozilla") { // Marker event for Mozilla - must calculate x,y pixel position
x = event.clientX - _googleMap.getDiv().offsetLeft - window.scrollX;
y = event.clientY - _googleMap.getDiv().offsetTop - window.scrollY;
}
else { // Marker event for other browsers (tested for Chrome and Safari) - must calculate x,y pixel position
x = event.pageX - _googleMap.getDiv().offsetLeft;
y = event.pageY - _googleMap.getDiv().offsetTop;
}
clickedLatLng = LatLon; // Save for context menu “Zoom In”
showContextMenu(x, y);
}
function showContextMenu(x, y) {
// Adjust the context menu location if near an edge
if (x > _googleMap.getDiv().offsetWidth - 120) { x = _googleMap.getDiv().offsetWidth - 120 }
if (y > _googleMap.getDiv().offsetHeight - 100) { y = _googleMap.getDiv().offsetHeight - 100 }
// Set context menu position
contextmenu.innerHTML = markerContextHtml;
contextmenu.style.position = "absolute";
contextmenu.style.left = x.toString() + "px";
contextmenu.style.top = y.toString() + "px";
contextmenu.style.visibility = "visible";
// Add context menu to the map
$(_googleMap.getDiv()).append(contextmenu);
}
I am still curious why fromDivPixelToLatLon() was not working in the other browsers. However, I am will to let that go for the sake of getting other things done.
From: google-map...@googlegroups.com [mailto:google-map...@googlegroups.com] On Behalf Of Nathan Raley
Sent: Thursday, December 16, 2010 5:35 PM
To: google-map...@googlegroups.com
Subject: Re: [Google Maps API v3] How to get latitude and longitude from marker right click event
Also, why are you calculating the pixel coordinates and translating to lat/lng when the click event itself provides you with lat/lng of the point clicked or the marker clicked?