# new SketchEditor2D(options)
二维场景草图编辑类
[ES5引入方式]:
const { SketchEditor2D } = Zondy
[ES6引入方式]:
import { SketchEditor2D } from "@mapgis/webclient-leaflet-plugin"
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
options |
Object | 构造参数 |
mapView |
MapView | 地图视图对象 |
layer |
GraphicsLayer | 草图图层管对象 |
sketchStyle |
SketchStyle | 草图符号 |
snappingOption |
Object | 草图捕获配置项 |
支持如下方法:
[1、开始绘制草图][2、停止绘制]
[3、移除当前草图]
[4、向草线或面草图中插入新的顶点]
[5、更新草图图形的某个顶点]
[6、移除草图图形的某个顶点]
[7、获取草图图形类型]
[8、设置草图样式]
[9、获取草图样式]
[10、获取草图几何对象]
[11、合并多个区几何]
[12、分割草图对象或区几何对象]
[13、撤销当前编辑操作]
[14、恢复被撤销的草图]
示例
// [ES5引入方式]:
const { SketchEditor2D } = Zondy
[ES6引入方式]:
import { MapView, SketchEditor2D } from "@mapgis/webclient-leaflet-plugin" <br/>
import { SketchStyle, SimpleMarkerSymbol, Color, SketchDataType} from "@mapgis/webclient-common" <br/>
var map = new Map()
var mapView = new MapView({
viewId: "mapgis-2d-viewer",
map: this.map,
})
var simpleMarkerSymbol = new SimpleMarkerSymbol({
color: new Color(24, 144, 255, 1),
size: 10,
});
var sketchStyle = new SketchStyle({
vertexStyle: simpleMarkerSymbol,
lineStyle: undefined,
fillStyle: undefined
})
var sketchEditor2D = new SketchEditor2D({
mapView: this.mapView,
layer: new GraphicsLayer(),
vertexStyle: vertexStyle
})
SketchEditor2D.start(SketchDataType.POINT) // 绘制点
SketchEditor2D.start(SketchDataType.POLYLINE) // 绘制线
SketchEditor2D.start(SketchDataType.POLYGON) // 绘制区
*
继承关系
方法
# canRedo()
Boolean
# canUndo()
Boolean
# getGeometry()
# getSketchStyle()
# redo()
示例
// [ES5引入方式]:
const { SketchEditor2D } = Zondy
[ES6引入方式]:
import { MapView, SketchEditor2D } from "@mapgis/webclient-leaflet-plugin" <br/>
import { Polygon, LineString } from "@mapgis/webclient-common" <br/>
var map = new Map()
var mapView = new MapView({
viewId: "mapgis-2d-viewer",
map: this.map,
})
var sketchEditor2D = new SketchEditor2D({
mapView: this.mapView,
layer: new GraphicsLayer()
})
sketchEditor2D.start(SketchDataType.POLYGON)
console.log("是否可以进行恢复操作:" + this.sketchEditor2D.canRedo())
const geometry = this.sketchEditor2D.redo()
console.log("恢复后的几何对象" + geometry)
# remove()
# setSketchStyle(sketchStyle)
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
sketchStyle |
SketchStyle |
示例
// ES5引入方式
const { SimpleFillSymbol, SimpleLineSymbol } = Zondy.Symbol
const { Color, SketchStyle } = Zondy
// ES6引入方式
import { SimpleFillSymbol, SimpleLineSymbol, Color, SketchStyle } from "@mapgis/webclient-common"
// 新建一个填充样式
const fillStyle = new SimpleFillSymbol({
color: new Color(0, 255, 255, 1),
outline: new SimpleLineSymbol({
color: new Color(255, 0, 0, 1),
width: 2
})
})
// 新建一个草图样式
const sketchStyle = new SketchStyle({
// 传入填充样式给区注记使用
fillStyle: fillStyle,
// 绘制线注记时显示分段长度
isShowSegmentLength: true,
// 绘制区注记时显示面积
isShowArea: true
})
sketchEditor2D.setSketchStyle(sketchStyle)
# split(target, splitPolyline)
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
target |
Polygon | SketchEditorNew | 被分割的几何/草图对象 |
splitPolyline |
Polyline | 线几何对象 |
合并后的几何对象
Array.<SketchEditorNew>
示例
// [ES5引入方式]:
const { MapView, SketchEditor2D, Polygon, LineString } = Zondy
[ES6引入方式]:
import { MapView, SketchEditor2D } from "@mapgis/webclient-leaflet-plugin" <br/>
import { Polygon, LineString } from "@mapgis/webclient-common" <br/>
var map = new Map()
var mapView = new MapView({
viewId: "mapgis-2d-viewer",
map: this.map,
})
var sketchEditor2D = new SketchEditor2D({
mapView: this.mapView,
layer: new GraphicsLayer()
})
const polygon = new Polygon({
coordinates: [
[
[108, 29],
[116, 29],
[116, 33],
[108, 33],
[108, 29]
]
]
})
const polyline = new LineString({
coordinates: [
[100, 30],
[120, 30]
]
})
const newSketchEditors = sketchEditor2D.split(polygon,polyline)
# start(dataType)
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
dataType |
SketchDataType | 草图编辑类型 |
示例
// [ES5引入方式]:
const { SketchEditor2D } = Zondy
[ES6引入方式]:
import { MapView, SketchEditor2D } from "@mapgis/webclient-leaflet-plugin" <br/>
import { SketchStyle, SimpleMarkerSymbol, Color, SketchDataType} from "@mapgis/webclient-common" <br/>
var map = new Map()
var mapView = new MapView({
viewId: "mapgis-2d-viewer",
map: this.map,
})
var simpleMarkerSymbol = new SimpleMarkerSymbol({
color: new Color(24, 144, 255, 1),
size: 10,
});
var sketchStyle = new SketchStyle({
vertexStyle: simpleMarkerSymbol,
lineStyle: undefined,
fillStyle: undefined
})
var sketchEditor2D = new SketchEditor2D({
mapView: this.mapView,
layer: new GraphicsLayer(),
vertexStyle: vertexStyle
})
SketchEditor2D.start(SketchDataType.POINT) // 绘制点
SketchEditor2D.start(SketchDataType.POLYLINE) // 绘制线
SketchEditor2D.start(SketchDataType.POLYGON) // 绘制区
# stop()
# undo()
示例
// [ES5引入方式]:
const { MapView, SketchEditor2D, Polygon, LineString, SketchDataType } = Zondy
[ES6引入方式]:
import { MapView, SketchEditor2D } from "@mapgis/webclient-leaflet-plugin" <br/>
import { Polygon, LineString, SketchDataType } from "@mapgis/webclient-common" <br/>
var map = new Map()
var mapView = new MapView({
viewId: "mapgis-2d-viewer",
map: this.map,
})
var sketchEditor2D = new SketchEditor2D({
mapView: this.mapView,
layer: new GraphicsLayer()
})
sketchEditor2D.start(SketchDataType.POLYGON)
console.log("是否可以进行撤销操作:" + this.sketchEditor2D.canUndo())
const geometry = this.sketchEditor2D.undo()
console.log("恢复后的几何对象" + geometry)
# union(polygons)
参数:
| 名称 | 类型 | 描述 |
|---|---|---|
polygons |
Polygon | 被合并的区几何对象 |
合并后的几何对象
示例
// [ES5引入方式]:
const { MapView, SketchEditor2D, polygon } = Zondy
[ES6引入方式]:
import { MapView, SketchEditor2D } from "@mapgis/webclient-leaflet-plugin" <br/>
import { polygon } from "@mapgis/webclient-common" <br/>
var map = new Map()
var mapView = new MapView({
viewId: "mapgis-2d-viewer",
map: this.map,
})
var sketchEditor2D = new SketchEditor2D({
mapView: this.mapView,
layer: new GraphicsLayer()
})
const polygon = new Polygon({
coordinates: [
[
[0, -60],
[0, 60],
[160, 60],
[160, -60],
[0, -60]
]
]
})
const polygon1 = new Polygon({
coordinates: [
[
[10, -60],
[10, 60],
[170, 60],
[170, -60],
[10, -60]
]
]
})
const polygons = [polygon,polygon1]
sketchEditor2D.union(polygons)
事件
# 草图绘制完成事件
属性:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
event |
Object | 事件对象 |
||
type |
LayerEventType |
<optional> |
'drawn' | 事件类型 |
geometry |
Geometry |
<optional> |
绘制的几何对象 |
- Inherited From:
示例
sketchEditor.on('drawn', (event) => {
console.log("绘制的几何对象:", event.geometry)
})
# 草图被选中事件
属性:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
event |
Object | 事件对象 |
||
type |
LayerEventType |
<optional> |
'selected' | 事件类型 |
selectedSketch |
SketchEditor2D | SketchEditor3D |
<optional> |
被选中的草图对象 |
- Inherited From:
示例
sketchEditor.on('selected', (event) => {
console.log("被选中事件:", event.selectedSketch)
})
