I am trying to make a div
visible at the position of the cursor when the cursor mouseover a marker using jQuery. Its kind of like a tooltip. However I cannot seem to figure out how to get the X/Y coordinates of the point below the cursor.
Current Code:
google.maps.event.addListener(marker, "mouseover", function(event) {
$("#tooltip").css({
position: "absolute",
top: event.pageY,
left: event.pageX
}).toggle();
I believe event
does not have the properties pageY
and pageX
like in the event in jQuery.
How do I get the osition of the mouse cursor?
The solution that works for me is more straight forward:
Here's a solution that uses the MouseEvent of the click event but does not rely on accessing that via an undocumented property that can change at any time like 'ub' or 'wb' (I think it's 'ya' currently).
Add the following code into the listener function:
This is an extension of my previous answer regarding the computation of the pixel positions (Google maps API v3). Introduce a "global" variable
overlay
:Use
overlay
in the listener to get the projection and the pixel coordinates:You may also have a look at
projection.fromLatLngToDivPixel()
.This gives the mouse position, works for me.
In your initialize function;
and use it like this;