Get Position of Mouse Cursor on Mouseover of Google Maps V3 API Marker
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).
map.data.addListener('mouseover', function (e) {
var keys = Object.keys(e);
var x, y;
for (var i = 0; i < keys.length; i++) {
if (MouseEvent.prototype.isPrototypeOf(e[keys[i]])) {
x = e[keys[i]].clientX;
y = e[keys[i]].clientY;
}
}
});
This is an extension of my previous answer regarding the computation of the pixel positions (Google maps API v3). Introduce a "global" variable overlay
:
var overlay = new google.maps.OverlayView();
overlay.draw = function() {};
overlay.setMap(map); // 'map' is new google.maps.Map(...)
Use overlay
in the listener to get the projection and the pixel coordinates:
google.maps.event.addListener(marker, 'mouseover', function() {
var projection = overlay.getProjection();
var pixel = projection.fromLatLngToContainerPixel(marker.getPosition());
// use pixel.x, pixel.y ... (after some rounding)
});
You may also have a look at projection.fromLatLngToDivPixel()
.