我使用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, 51, 204, 0.8],
width: 2,
},
},
});发布于 2020-09-26 02:35:30
我看到了两个选择,实现“逻辑”或者在已经实现的地方使用SketchViewModel。顺便说一句,我的“逻辑”是指当最后一个顶点与第一个顶点相等(带有公差)时的完整多边形。
看看这些链接,
ArcGIS JS API Docs - SketchViewModel
您可以实现自己的UI来与模型交互或使用SketchWidget。
https://stackoverflow.com/questions/64065317
复制相似问题