我正在尝试重写 MapboxDraw.modes.draw_line_string.clickAnywhere 函数,以允许在 draw_line_string 模式下使用 SHIFT 键删除前一个点。它可以工作,但是当它删除前一点时,它会在新位置创建一个新点。
- 画一条线
- 按住 shift 并单击任何先前的点
- 将删除旧的创建一个新的
我想删除旧点并继续添加新点一个例子在这里 https://jsfiddle.net/benderlio/5fxwhkdp/7/
MapboxDraw.modes.draw_line_string.clickAnywhere = function (state, e) {
if (e.originalEvent.shiftKey) {
const line = state.line.coordinates;
const lastClick = line[line.length - 1]
const a = map.project(lastClick)
const res = line.map((i, index) => {
const b = map.project(i)
const d = distance(a, b)
return {
index,
distance: d
};
}).find(i => i.distance < 5 && i.distance > 0)
console.log('res', res);
if (res) {
//state.line.removeCoordinate(res.index)
state.line.coordinates = state.line.coordinates.filter((i, index) => index !== res.index)
}
} else {
console.log('NO SHiFT');
if (state.currentVertexPosition > 0 && isEventAtCoordinates(e, state.line.coordinates[state.currentVertexPosition - 1]) ||
state.direction === 'backwards' && isEventAtCoordinates(e, state.line.coordinates[state.currentVertexPosition + 1])) {
return this.changeMode('simple_select', { featureIds: [state.line.id] });
}
this.updateUIClasses({ mouse: 'add' });
state.line.updateCoordinate(state.currentVertexPosition, e.lngLat.lng, e.lngLat.lat);
if (state.direction === 'forward') {
state.currentVertexPosition++;
state.line.updateCoordinate(state.currentVertexPosition, e.lngLat.lng, e.lngLat.lat);
} else {
state.line.addCoordinate(0, e.lngLat.lng, e.lngLat.lat);
}
}
};
UPD: ok,重点是设置currentVertexPosition
state.line.coordinates.splice(res.index,1)
state.currentVertexPosition = state.currentVertexPosition - 1