2011-05-27 165 views
6

我想要实现的是制作DIV并在其上或显示世界地图图像作为背景.. &当用户在地图上点击某处时,我抓住鼠标点击位置,检测鼠标点击位置

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

我的问题是:

  • 如何使用JavaScript获取鼠标点击位置
  • 如何在全球的点击​​位置转换成DIV的相对X,A层

回答

2

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

除非您有理由从头开始,否则我会推荐Google Maps Api

您会注意到Map对象有一个click事件,您可以绑定一个回调函数,该回调函数接收包含纬度/长度坐标的MouseEvent。检查出this example

0

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

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