问题描述
我正在使用ArcGIS JS 4.16允许用户在地图上绘制多边形。这个想法是在任何给定时间只有一个多边形,当您连接两个点时,就可以完成多边形。对于普通的用例,双击或按“ C”似乎有点复杂。
const drawLayer = new Graphicslayer();
const map = new Map({
basemap: 'streets-vector',layers: [drawLayer],});
const view = new MapView({
container: mapRef.current,map: map,center: [-73.93,40.73],zoom: 10,});
const draw = new Draw({ view: view });
document
.getElementById('enableCreatepolygon')
.addEventListener('click',() => {
enableCreatepolygon(draw,view);
});
const enableCreatepolygon = (draw,view) => {
const action = draw.create('polygon');
action.on('vertex-add',(evt) => {
createpolygonGraphic(evt.vertices);
});
action.on('vertex-remove',(evt) => {
createpolygonGraphic(evt.vertices);
});
action.on('cursor-update',(evt) => {
createpolygonGraphic(evt.vertices);
});
action.on('draw-complete',(evt) => {
createpolygonGraphic(evt.vertices);
});
};
const createpolygonGraphic = (vertices) => {
view.graphics.removeAll();
const polygon = {
type: 'polygon',rings: vertices,spatialReference: view.spatialReference,};
const graphic = new Graphic({
geometry: polygon,symbol: {
type: 'simple-fill',color: [51,51,204,0.15],style: 'solid',outline: {
color: [51,0.8],width: 2,},});
解决方法
我看到两个选项,实现“逻辑”或在已经实现的地方使用SketchViewModel
。顺便说一句,“逻辑”是指最后一个顶点等于(具有公差)第一个顶点时的完整多边形。
看看这个链接,
ArcGIS JS API Docs - SketchViewModel
您可以实现自己的UI与模型进行交互,也可以使用SketchWidget
。