Reputation: 31
On a normal google map, I can get and display the geographical coordinates of my mousepoint using the following code:
google.maps.event.addListener(map, "mousemove", function(event) {
document.getElementById('message').innerHTML =
'Geographical Coordinates: ' + event.latLng.lat() + ', ' + event.latLng.lng();
});
If I calculate a route using the google directions api and move the mouse over the displayed route, the "mousemove" event is not fired. How can I get the coordinates when the mousepointer is on the route?
I have a running example on jsfiddle. When you follow the route with your mouse, the coordinates don't update.
Upvotes: 3
Views: 3108
Reputation: 99
This is an example moving a tooltip of bootstrap at cursor position:
google.maps.event.addListener(poly,"mousemove",function(e){
var _tooltipPolys = $("#tooltipPolys");
if(_tooltipPolys.length == 0) {
_tooltipPolys = $(' \
<div id="tooltipPolys" class="tooltip top" role="tooltip"> \
<div class="tooltip-arrow"></div> \
<div class="tooltip-inner"></div> \
</div> \
');
$("body").append(_tooltipPolys);
$("div.tooltip-inner", _tooltipPolys).text(this.title);
_tooltipPolys.css({
"opacity": ".9",
"position":"absolute"
});
}
var pageX = event.pageX;
var pageY = event.pageY;
if (pageX === undefined) {
pageX = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
pageY = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
}
// Update the current tooltip position each time the cursor moves
_tooltipPolys.css({
"top":(pageY - _tooltipPolys.height() - 10)+"px",
"left":(pageX - (_tooltipPolys.width() / 2))+"px"
});
});
Upvotes: 1
Reputation: 161384
The only way I know of to add a mouseover event to the route itself is to draw the polyline (so you can access it), then add a mouseover event to it.
Upvotes: 0