最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect Mouse click location - Stack Overflow

programmeradmin10浏览0评论

What i am trying to achieve is make a DIV and display world map image on it or as a background.. & when user clicks some somewhere on the map, i catch the mouse-click location,

then i am some how going to translate the mouse's X,Y cords into longitude,latitude and then run an AJAX to display some data.

My problem is:

  • How to get mouse click location with JavaScript
  • How to translate the global click location into a DIV's relative X,Y coords of a layer

What i am trying to achieve is make a DIV and display world map image on it or as a background.. & when user clicks some somewhere on the map, i catch the mouse-click location,

then i am some how going to translate the mouse's X,Y cords into longitude,latitude and then run an AJAX to display some data.

My problem is:

  • How to get mouse click location with JavaScript
  • How to translate the global click location into a DIV's relative X,Y coords of a layer
Share Improve this question asked May 27, 2011 at 16:07 MoonMoon 20k56 gold badges141 silver badges203 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 5

Look at this example:

  • Click image and get coordinates with Javascript

Following posts may also help:

  • Get accurate position for a click on a linked image using jquery

  • Creating HTML Image Maps

In HTML you set onMouseMove event:

<div onMouseMove="getPositions();"
style="width:200px;height:100px"></div>

And javascript function:

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

Unless you have a reason to do it from scratch, I would recommend Google Maps Api.

You'll notice that the Map object has a click event to which you can bind a callback that receives a MouseEvent that contains lat/long coords. Check out this example.

In the event handler you can get it through the mouseEvent Args.

Excerpt from 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;
}
发布评论

评论列表(0)

  1. 暂无评论