检测鼠标点击位置

Detect Mouse click location

本文关键字:位置 鼠标 检测      更新时间:2023-09-26

我试图实现的是制作一个div并在其上或作为背景显示世界地图图像&当用户点击地图上的某个地方时,我会捕捉到鼠标点击的位置,

然后我将如何将鼠标的X、Y线转换为经度、纬度,然后运行AJAX来显示一些数据。

我的问题是:

  • 如何使用JavaScript获取鼠标点击位置
  • 如何将全局单击位置转换为层的DIV的相对X、Y坐标

看看这个例子:

  • 单击图像并使用Javascript获取坐标

以下帖子也可能有所帮助:

  • 使用jquery 获取链接图像点击的准确位置

  • 创建HTML图像映射

在HTML中设置鼠标移动事件:

<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;
}

除非你有理由从头开始,否则我推荐谷歌地图Api。

您会注意到Map对象有一个点击事件,您可以将一个回调绑定到该事件,该回调接收一个包含lat/long坐标的MouseEvent。看看这个例子。

在事件处理程序中,您可以通过mouseEvent Args获取它。

摘录自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;
}