我已经忘记了这个问题,但这是我们解决问题的方法。希望对您有所帮助!
简洁版本:
每当 amousedown
出现在形状上时,检查它是否在顶点上,如果是,则用于保存对表示顶点句柄elementFromPoint
的实际 HTML 的引用。<div>
在后续mousemove
和mouseup
事件中,您可以轮询此的屏幕位置<div>
并将其与地图上其他点的位置进行比较。
长版:
我已经从我们的应用程序中提取了相关功能,因此您需要忽略我们的一些特定功能和对象等,但这确实显示了我们正在工作的“snap-to-point”实现。
首先,我们将进行像素到纬度经纬度的转换,因此您需要在某处定义一个简单的叠加层(您总是需要一个来进行这些计算):
_this.editor.overlay = new g.maps.OverlayView();
_this.editor.overlay.draw = function () {};
_this.editor.overlay.setMap(This.map);
现在,只要我们在地图上初始化了一个形状,我们就会mousedown
向它添加一个事件处理程序。
g.maps.event.addListener(_this.shape, 'mousedown', function (event) {
if (event.vertex >= 0) {
var pixel = _this.editor.overlay.getProjection().fromLatLngToContainerPixel(_this.shape.getPath().getAt(event.vertex));
var offset = _this.mapElement.offset();
var handle = document.elementFromPoint(pixel.x + offset.left, pixel.y + offset.top);
if (handle) {
_this.dragHandle = $(handle);
_this.snappablePoints = _this.editor.snappablePoints(_this);
} else {
_this.dragHandle = null;
_this.snappablePoints = null;
}
}
});
(您会注意到对 的调用snappablePoints
,它只是一个内部实用程序函数,它收集所有对该点有效的点。我们在这里这样做是因为在每个单个 上执行这将是一个昂贵的循环mousemove
。)
现在,在我们形状的mousemove
监听器中,因为我们已经保存了对它的引用,所以<div>
我们可以轮询它的屏幕位置并将其与地图上其他点的屏幕位置进行比较。如果一个点在某个像素范围内(我认为我们的像素是 8 个像素),我们保存它并悬停一个小图标,告诉用户我们要捕捉。
g.maps.event.addListener(this.shape, 'mousemove', function (event) {
var projection, pixel, pixel2, offset, dist, i;
if (event.vertex >= 0 && this.dragHandle) {
// If dragHandle is set and we're moving over a vertex, we must be dragging an
// editable polygon point.
offset = this.editor.mapElement.offset();
pixel = {
x: this.dragHandle.offset().left - offset.left + this.dragHandle.width() / 2,
y: this.dragHandle.offset().top - offset.top + this.dragHandle.height() / 2
};
// Search through all previously saved snappable points, looking for one within the snap radius.
projection = this.editor.overlay.getProjection();
this.snapToPoint = null;
for(i = 0; i < this.snappablePoints.length; i++) {
pixel2 = projection.fromLatLngToContainerPixel(this.snappablePoints[i]);
dist = (pixel.x - pixel2.x) * (pixel.x - pixel2.x) + (pixel.y - pixel2.y) * (pixel.y - pixel2.y);
if (dist <= SNAP_RADIUS) {
this.snapToPoint = this.snappablePoints[i];
$('#zone-editor #snapping').css('left', pixel.x + 10 + offset.left).css('top', pixel.y - 12 + offset.top).show();
break;
}
}
if (!this.snapToPoint) {
$('#zone-editor #snapping').hide();
}
});
当用户停止移动鼠标时进行一点清理:
g.maps.event.addListener(this.shape, 'mouseup', function (event) {
// Immediately clear dragHandle, so that everybody knows we aren't dragging any more.
// We'll let the path updated event deal with any actual snapping or point saving.
_this.dragHandle = null;
$('#zone-editor #snapping').hide();
});
最后,我们实际上处理了“捕捉”,这实际上只是形状路径上的事件侦听器中的一小部分逻辑。
g.maps.event.addListener(this.shape.getPath(), 'set_at', function (index, element) {
if (this.snapToPoint) {
// The updated point was dragged by the user, and we have a snap-to point.
// Overwrite the recently saved point and let another path update trigger.
var point = this.snapToPoint;
this.snapToPoint = null;
this.shape.getPath().setAt(index, point);
} else {
// Update our internal list of points and hit the server
this.refreshPoints();
this.save();
};
// Clear any junk variables whenever the path is updated
this.dragHandle = null;
this.snapToPoint = null;
this.snappablePoints = null;
});
鳍。