我试图做的是创建一个DIV并在其上显示世界地图图像或作为背景。当用户在地图上某处点击时,我会捕获鼠标点击位置,然后将鼠标的X,Y坐标转换为经度和纬度,并运行AJAX以显示一些数据。
我的问题是:
1.如何使用JavaScript获取鼠标点击位置 2.如何将全局点击位置转换为图层的相对于DIV的X,Y坐标
我的问题是:
1.如何使用JavaScript获取鼠标点击位置 2.如何将全局点击位置转换为图层的相对于DIV的X,Y坐标
<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;
}
除非你有从头开始做的理由,否则我建议使用Google Maps Api。
你会注意到Map对象有一个点击事件,你可以绑定一个回调函数来接收包含纬度/经度坐标的MouseEvent。看看this example。
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;
}