import*as MapboxDraw from'@mapbox/mapbox-gl-draw/dist/mapbox-gl-draw';import*as Constants from'@mapbox/mapbox-gl-draw/src/constants';import*as CommonSelectors from'@mapbox/mapbox-gl-draw/src/lib/common_selectors';const SimpleSelectWithoutMiddleVertexMode = MapboxDraw.modes.simple_select;
SimpleSelectWithoutMiddleVertexMode.onTap = SimpleSelectWithoutMiddleVertexMode.onClick=function(state, e){// 目的是为了避免绘制区域被选中而被拖拽编辑(clickOnFeature)等操作。自带属性没有发现可以实现这种功能// Click (with or without shift) on no featureif(CommonSelectors.noTarget(e))returnthis.clickAnywhere(state, e);// also tapif(CommonSelectors.isOfMetaType(Constants.meta.VERTEX)(e))returnthis.clickOnVertex(state, e);//tapif(CommonSelectors.isFeature(e))return;// 直接return 简单粗暴 而不是this.clickOnFeature(state, e);};exportdefault SimpleSelectWithoutMiddleVertexMode;
import SimpleSelectWithoutMiddleVertexMode from'../utils/drawSimpleSelect';/**
* 添加绘图工具
*/const modes = MapboxDraw.modes;exportconstADD_DRAW=()=>{if(draw){REMOVE_DRAW();}
draw =newMapboxDraw({displayControlsDefault:false,controls:{polygon:true,trash:false,},defaultMode:'draw_polygon',boxSelect:false,touchEnabled:false,keybindings:false,modes:{...MapboxDraw.modes,// draw_rectangle // mapboxgl-draw-rectangle-drag// draw_rectangle: DrawRectangle,simple_select: SimpleSelectWithoutMiddleVertexMode,// 取消MapboxDraw自定义取消选中功能},});
map.addControl(draw,'bottom-right');//绘制好的框选框禁止拖动
modes.simple_select.onDrag=function(state, e){//when polygon is deselected onDrag will be false and user not be able to drag it// console.log(state, e);return;};};