SketchEditor
表示草图编辑器,允许用户在地图视图上以交互方式绘制几何图形。通过使用相应的SketchCreationMode启动SketchEditor,可以从头开始绘制不同的几何类型,例如点,多点,折线或多边形。
构造方法
public SketchEditor ()
常用方法
//如果草图有效,则获取当前几何
public Geometry getGeometry ()
//指示草图几何体是否有效
public boolean isSketchValid ()
//撤消草图几何体的最后一个动作
public void undo ()
//重做草图几何体的最后一个动作
public void redo ()
//如果草图会话已启动,则停止该草绘会话。getGeometry()在调用stop之前可以获得最终的草图几何。
public void stop ()
//为给定的草图创建模式启动新的编辑会话
public void start (SketchCreationMode creationMode)
//设置草图几何体的不透明度
public void setOpacity (float opacity)
看官网例子就能掌握SketchEditor的用法:
/**
* SketchEditor:表示草图编辑器,允许用户在地图视图上以交互方式绘制几何图形。
*/
public class SketchEditorActivity extends AppCompatActivity {
private final String TAG = MainActivity.class.getSimpleName();
private SimpleMarkerSymbol mPointSymbol;
private SimpleLineSymbol mLineSymbol;
private SimpleFillSymbol mFillSymbol;
private MapView mMapView;
private SketchEditor mSketchEditor;
private GraphicsOverlay mGraphicsOverlay;
private ImageButton mPointButton;
private ImageButton mMultiPointButton;
private ImageButton mPolylineButton;
private ImageButton mPolygonButton;
private ImageButton mFreehandLineButton;
private ImageButton mFreehandPolygonButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sketch_editor);
mMapView = findViewById(R.id.mapView);
ArcGISMap map = new ArcGISMap(Basemap.Type.LIGHT_GRAY_CANVAS, 34.056295, -117.195800, 16);
mMapView.setMap(map);
mGraphicsOverlay = new GraphicsOverlay();
mMapView.getGraphicsOverlays().add(mGraphicsOverlay);
// 创建一个新的草图编辑器并将其添加到地图视图中
mSketchEditor = new SketchEditor();
mMapView.setSketchEditor(mSketchEditor);
mPointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.SQUARE, 0xFFFF0000, 20);
mLineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, 0xFFFF8800, 4);
mFillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.CROSS, 0x40FFA9A9, mLineSymbol);
initViews();
}
private void initViews() {
// get buttons from layouts
mPointButton = findViewById(R.id.pointButton);
mMultiPointButton = findViewById(R.id.pointsButton);
mPolylineButton = findViewById(R.id.polylineButton);
mPolygonButton = findViewById(R.id.polygonButton);
mFreehandLineButton = findViewById(R.id.freehandLineButton);
mFreehandPolygonButton = findViewById(R.id.freehandPolygonButton);
// add click listeners
mPointButton.setOnClickListener(view -> createModePoint());
mMultiPointButton.setOnClickListener(view -> createModeMultipoint());
mPolylineButton.setOnClickListener(view -> createModePolyline());
mPolygonButton.setOnClickListener(view -> createModePolygon());
mFreehandLineButton.setOnClickListener(view -> createModeFreehandLine());
mFreehandPolygonButton.setOnClickListener(view -> createModeFreehandPolygon());
}
/**
* 单击point按钮时,重置其他按钮,显示选中的point按钮和开始点绘图模式。
*/
private void createModePoint() {
resetButtons();
mPointButton.setSelected(true);
mSketchEditor.start(SketchCreationMode.POINT);
}
/**
* 单击multipoint按钮时,重置其他按钮,显示选中的multipoint按钮,然后开始多点绘图模式。
*/
private void createModeMultipoint() {
resetButtons();
mMultiPointButton.setSelected(true);
mSketchEditor.start(SketchCreationMode.MULTIPOINT);
}
/**
* 单击折线按钮时,重置其他按钮,显示选中的折线按钮,然后开始折线图模式。
*/
private void createModePolyline() {
resetButtons();
mPolylineButton.setSelected(true);
mSketchEditor.start(SketchCreationMode.POLYLINE);
}
/**
* 当单击多边形按钮时,重置其他按钮,显示选中的多边形按钮,并启动多边形绘图模式。
*/
private void createModePolygon() {
resetButtons();
mPolygonButton.setSelected(true);
mSketchEditor.start(SketchCreationMode.POLYGON);
}
/**
* 单击FREEHAND_LINE按钮时,重置其他按钮,显示选定的FREEHAND_LINE按钮,和开始徒手画线模式。
*/
private void createModeFreehandLine() {
resetButtons();
mFreehandLineButton.setSelected(true);
mSketchEditor.start(SketchCreationMode.FREEHAND_LINE);
}
/**
* 单击FREEHAND_POLYGON按钮时,重置其他按钮,显示选定的FREEHAND_POLYGON按钮,并启用徒手绘制多边形模式。
*/
private void createModeFreehandPolygon() {
resetButtons();
mFreehandPolygonButton.setSelected(true);
mSketchEditor.start(SketchCreationMode.FREEHAND_POLYGON);
}
/**
* 当单击undo按钮时,撤消SketchEditor上的最后一个事件。
*/
private void undo() {
if (mSketchEditor.canUndo()) {
mSketchEditor.undo();
}
}
/**
* 当单击redo按钮时,在SketchEditor上重做最后一个未完成的事件。
*/
private void redo() {
if (mSketchEditor.canRedo()) {
mSketchEditor.redo();
}
}
/**
* 当单击停止按钮时,检查草图是否有效。如果是,从草图中得到几何图形,设置它将其添加到图形覆盖层。
*/
private void stop() {
if (!mSketchEditor.isSketchValid()) {
reportNotValid(); //如果草图无效,则调用。向用户报告草图无效的原因。
mSketchEditor.stop();
resetButtons();
return;
}
// 从草图编辑器获得几何图形
Geometry sketchGeometry = mSketchEditor.getGeometry();
mSketchEditor.stop();
resetButtons();
if (sketchGeometry != null) {
//从草图编辑器创建一个图形
Graphic graphic = new Graphic(sketchGeometry);
// 根据几何类型分配符号
if (graphic.getGeometry().getGeometryType() == GeometryType.POLYGON) {
graphic.setSymbol(mFillSymbol);
} else if (graphic.getGeometry().getGeometryType() == GeometryType.POLYLINE) {
graphic.setSymbol(mLineSymbol);
} else if (graphic.getGeometry().getGeometryType() == GeometryType.POINT ||
graphic.getGeometry().getGeometryType() == GeometryType.MULTIPOINT) {
graphic.setSymbol(mPointSymbol);
}
// 将图形添加到图形覆盖层
mGraphicsOverlay.getGraphics().add(graphic);
}
}
/**
* 如果草图无效,则调用。向用户报告草图无效的原因。
*/
private void reportNotValid() {
String validIf;
if (mSketchEditor.getSketchCreationMode() == SketchCreationMode.POINT) {
validIf = "Point only valid if it contains an x & y coordinate.";
} else if (mSketchEditor.getSketchCreationMode() == SketchCreationMode.MULTIPOINT) {
validIf = "Multipoint only valid if it contains at least one vertex.";
} else if (mSketchEditor.getSketchCreationMode() == SketchCreationMode.POLYLINE
|| mSketchEditor.getSketchCreationMode() == SketchCreationMode.FREEHAND_LINE) {
validIf = "Polyline only valid if it contains at least one part of 2 or more vertices.";
} else if (mSketchEditor.getSketchCreationMode() == SketchCreationMode.POLYGON
|| mSketchEditor.getSketchCreationMode() == SketchCreationMode.FREEHAND_POLYGON) {
validIf = "Polygon only valid if it contains at least one part of 3 or more vertices which form a closed ring.";
} else {
validIf = "No sketch creation mode selected.";
}
String report = "Sketch geometry invalid:\n" + validIf;
Snackbar reportSnackbar = Snackbar.make(findViewById(R.id.toolbarInclude), report, Snackbar.LENGTH_INDEFINITE);
reportSnackbar.setAction("Dismiss", view -> reportSnackbar.dismiss());
TextView snackbarTextView = reportSnackbar.getView().findViewById(android.support.design.R.id.snackbar_text);
snackbarTextView.setSingleLine(false);
reportSnackbar.show();
Log.e(TAG, report);
}
/**
* De-selects all buttons.
*/
private void resetButtons() {
mPointButton.setSelected(false);
mMultiPointButton.setSelected(false);
mPolylineButton.setSelected(false);
mPolygonButton.setSelected(false);
mFreehandLineButton.setSelected(false);
mFreehandPolygonButton.setSelected(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.undo_redo_stop_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.undo) {
undo();
} else if (id == R.id.redo) {
redo();
} else if (id == R.id.stop) {
stop();
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPause() {
mMapView.pause();
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.resume();
}
@Override
protected void onDestroy() {
super.onDestroy();
mMapView.dispose();
}
}