我有一个可编辑的多边形,就像这里一样。
当用户在地图上移动点(以调整多边形大小)时,我想“捕捉”该事件。我需要这个功能来实现对齐点。
可能吗?
编辑
this.polygon = new google.maps.Polygon({
map: map,
strokeWeight: 2,
editable: true,
path: this._path
});
var dragging = false;
google.maps.event.addListener(this.polygon, 'mousedown', function (event) {
if (event.vertex) {
dragging = true;
}
});
google.maps.event.addListener(this.polygon, 'mousemove', function (event) {
if (dragging) {
// dragging
}
});
google.maps.event.addListener(this.polygon, 'mouseup', function (event) {
dragging = false;
});
代码正在运行,事件被捕获。但是,我无法访问当前拖动的点来更改它的位置。
我还尝试在mousemove
事件内部更改 latLng 对象,但没有效果
临时解决方案
调整大小时无法访问多边形幻影,因此实现捕捉的唯一解决方案是在调整多边形大小后执行此操作。
this.polygon = new google.maps.Polygon({
map: map,
strokeWeight: 2,
editable: true,
path: this._path
});
var path = this.polygon.getPath();
google.maps.event.addListener(path, 'set_at', function (event) {
// Here do the snapping, after the polygon has been resized
});
google.maps.event.addListener(path, 'insert_at', function (event) {
// Here do the snapping, after the polygon has been resized
});