检测鼠标点击位置

7
我试图做的是创建一个DIV并在其上显示世界地图图像或作为背景。当用户在地图上某处点击时,我会捕获鼠标点击位置,然后将鼠标的X,Y坐标转换为经度和纬度,并运行AJAX以显示一些数据。
我的问题是:
1.如何使用JavaScript获取鼠标点击位置 2.如何将全局点击位置转换为图层的相对于DIV的X,Y坐标
4个回答


3
在HTML中,您可以设置onMouseMove事件:
<div onMouseMove="getPositions();"
style="width:200px;height:100px"></div>

JavaScript函数:

function getPositions(ev) {
if (ev == null) { ev = window.event }
   _mouseX = ev.clientX;
   _mouseY = ev.clientY;
}

1

除非你有从头开始做的理由,否则我建议使用Google Maps Api

你会注意到Map对象有一个点击事件,你可以绑定一个回调函数来接收包含纬度/经度坐标的MouseEvent。看看this example


0
在事件处理程序中,您可以通过mouseEventArgs获取它。
来自http://msdn.microsoft.com/en-us/library/bb404721.aspx的摘录 -
function onMouseLeftButtonUp(sender, mouseEventArgs)
{
    // Return a Point object representing the x- and y-coordinates of the current mouse position
    // relative to the reference object.
    var pt = mouseEventArgs.getPosition(sender.findName("referenceObject"));

    // Display the current mouse position.
    sender.findName("Status").text = pt.x + " : " + pt.y;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接