Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect Mouse click location

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
like image 215
Moon Avatar asked May 27 '11 16:05

Moon


3 Answers

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

like image 184
Naveed Avatar answered Oct 12 '22 00:10

Naveed


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;
}
like image 34
evilone Avatar answered Oct 12 '22 00:10

evilone


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.

like image 26
mgalgs Avatar answered Oct 12 '22 00:10

mgalgs