2080
2080

Reputation: 1417

JavaScript Polymaps library: Get mouse coordinates

I use the polymaps library to display a custom tiled map. Whenever the user clicks on it, I need to know the coordinates of that point. Not the lat/lon values that map.mouse(e) gives, but pixel coordinates.

var po = org.polymaps;

var div = document.getElementById("map");

var map = po.map()
    .container(div.appendChild(po.svg("svg")))
    .zoomRange([0,8])
    .zoom(1)
    .add(po.image()
    .url(getTile));

map.add(po.interact())
.add(po.hash());

$(div).mousemove(function(e) {
    ???
})

Does this library provide a function to do this?

Upvotes: 0

Views: 56

Answers (1)

mwilson
mwilson

Reputation: 12900

To obtain the mouse position on the original image from which the tilemap was created, use the following code:

$(div).mousemove(function(e) {

  // Get column, row and zoom level of mouse position
  var crz = map.locationCoordinate(map.pointLocation(map.mouse(e)));

  // Normalize the column and row values to the 0..1 range
  var zoomMultiplier = Math.pow(2, crz.zoom-1);

  var x01 = crz.column/zoomMultiplier;
  var y01 = crz.row/zoomMultiplier;

  // Multiply with the original image width and height
  var originalWidth = 32768;
  var originalHeight = 32768;

  var mx = x01*originalWidth;
  var my = y01*originalHeight;

  // Now we have the mouse coordinates on the original image!
  console.log(mx, my);
})

Upvotes: 1

Related Questions