我正在尝试使用leaflet地图库,在单击图像叠加时(右键单击/上下文菜单),获取像素坐标。基本上,当用户点击图像时,我需要找到用户点击图像的x、y或宽度、高度。
目前这是我所拥有的。
// Using leaflet.js to pan and zoom a big image.
// See also: http://kempe.net/blog/2014/06/14/leaflet-pan-zoom-image.html
// create the slippy map
var map = L.map('image-map', {
minZoom: 1,
maxZoom: 4,
center: [0, 0],
zoom: 1,
crs: L.CRS.Simple,
});
// dimensions of the image
var w = 2000,
h = 1500,
url = 'http://kempe.net/images/newspaper-big.jpg';
// calculate the edges of the image, in coordinate space
var southWest = map.unproject([0, h], map.getMaxZoom() - 1);
var northEast = map.unproject([w, 0], map.getMaxZoom() - 1);
var bounds = new L.LatLngBounds(southWest, northEast);
// add the image overlay,
// so that it covers the entire map
L.imageOverlay(url, bounds).addTo(map);
// tell leaflet that the map is exactly as big as the image
map.setMaxBounds(bounds);
function onMapClick(e) {
//returns on right click events
console.log(e);
}
//Hadnel on right click functions TODO: MOVE THIS LATER
map.on('contextmenu', onMapClick);
当前在 onMapClick(e) 中,当我点击时检查返回的事件,看不到所有返回的坐标与我点击位置的 x、y 坐标以及图片宽度和高度有任何接近的证据。
实际上,我希望能够看到我点击的图片位置的 x、y 坐标或宽度、高度,假设该图片的尺寸为 20000x15000 像素。
这是 jsfiddle http://jsfiddle.net/rayshinn/yvfwzfw4/1/
不确定原因,但当你缩放到最小时,它似乎有点错误。只需放大到最大即可停止 jsfiddle 上的 bug。这个 bug 不是重点,因为它不会在我的本地环境中发生!好像与 fiddle 有关。