官方示例:https://developers.arcgis.com/javascript/latest/sample-code/sandbox/index.html?sample=sketch-geometries

HTML:
~~~
<div id="viewDiv">
<div id="topbar">
<button class="action-button esri-icon-blank-map-pin" id="pointButton" type="button"
title="Draw point"></button>
<button class="action-button esri-icon-polyline" id="polylineButton" type="button"
title="Draw polyline"></button>
<button class="action-button esri-icon-polygon" id="polygonButton" type="button"
title="Draw polygon"></button>
<button class="action-button esri-icon-checkbox-unchecked" id="rectangleButton" type="button"
title="Draw rectangle"></button>
<button class="action-button esri-icon-radio-unchecked" id="circleButton" type="button"
title="Draw circle"></button>
<button class="action-button esri-icon-trash" id="resetBtn" type="button" title="Clear graphics"></button>
</div>
</div>
~~~
CSS:
~~~
#topbar{background: #fff;position: absolute;top: 15px;right: 15px;padding: 10px;}
.action-button{font-size: 16px;background-color: transparent;border: 1px solid #D3D3D3;color: #6e6e6e;height: 32px;width: 32px;text-align: center;box-shadow: 0 0 1px rgba(0, 0, 0, 0.3);}
.action-button:hover, .action-button:focus{background: #0079c1;color: #e4e4e4;}
.active{background: #0079c1;color: #e4e4e4;}
~~~
~~~
require([
'esri/Map',
'esri/views/MapView',
'esri/Graphic',
'esri/widgets/Sketch/SketchViewModel',
'esri/layers/GraphicsLayer',
'dojo/domReady!'
], function(Map, MapView, Graphic, SketchViewModel, GraphicsLayer) {
var map = new Map({
basemap: 'streets' // dark-gray
});
var mapView = new MapView({
map: map,
container: 'js_map',
center: [102.9331224074, 25.1049040686],
zoom: 6,
// rotation: -127.7
});
// 添加graphicLayer
var graphicsLayer = new GraphicsLayer(),
graphicsObj = {},
updateGraphic = null;
map.layers.add(graphicsLayer);
var pointSymbol = {
type: "simple-marker",
style: "square",
color: "#8A2BE2",
size: "16px",
outline: {
color: [255, 255, 255],
width: 3
}
}
var polylineSymbol = {
type: "simple-line",
color: "#8A2BE2",
width: "4",
style: "dash"
}
var polygonSymbol = {
type: "simple-fill",
color: "rgba(138,43,226, 0.8)",
style: "solid",
outline: {
color: "white",
width: 1
}
}
mapView.when(function() {
var sketchViewModel = new SketchViewModel({
view: mapView,
layer: graphicsLayer,
pointSymbol: pointSymbol,
polylineSymbol: polylineSymbol,
polygonSymbol: polygonSymbol
});
mapView.on("click", function(evt) {
mapView.hitTest(evt).then(function(res) {
var results = res.results;
if(results.length && results[results.length-1].graphic) {
if(!updateGraphic) {
updateGraphic = results[results.length-1].graphic;
graphicsLayer.remove(updateGraphic);
sketchViewModel.update(updateGraphic.geometry);
}
}
});
});
function addGraphic(evt) {
var geometry = evt.geometry;
var symbol;
switch(geometry.type) {
case "point":
symbol = pointSymbol;
break;
case "polyline":
symbol = polylineSymbol;
break;
default:
symbol = polygonSymbol;
break;
}
var id = Date.now();
graphicsObj[id] = new Graphic({
attributes: {id: id},
geometry: geometry,
symbol: symbol
});
graphicsLayer.add(graphicsObj[id]);
updateGraphic = null;
alert('繪制完成~');
setActiveButton();
}
sketchViewModel.on("draw-complete", addGraphic); // 繪制完成
sketchViewModel.on("update-complete", addGraphic); // 更新完成
sketchViewModel.on("update-cancel", addGraphic); // 更新取消
function setActiveButton(selectedButton) {
mapView.focus();
var elements = document.getElementsByClassName("active");
for(var i=0; i<elements.length; i++) {
elements[i].classList.remove("active");
}
if(selectedButton) {
selectedButton.classList.add("active");
}
}
// 按鈕-繪制點
document.getElementById("pointButton").onclick = function() {
sketchViewModel.create("point");
setActiveButton(this);
}
// 按鈕-繪制線
document.getElementById("polylineButton").onclick = function() {
sketchViewModel.create("polyline");
setActiveButton(this);
}
// 按鈕-繪制區域
document.getElementById("polygonButton").onclick = function() {
sketchViewModel.create("polygon");
setActiveButton(this);
}
// 按鈕-繪制矩形
document.getElementById("rectangleButton").onclick = function() {
sketchViewModel.create("rectangle");
setActiveButton(this);
}
// 按鈕-繪制圓
document.getElementById("circleButton").onclick = function() {
sketchViewModel.create("circle");
setActiveButton(this);
}
// 按鈕-重置
document.getElementById("resetBtn").onclick = function() {
// sketchViewModel.reset();
// graphicsLayer.removeAll();
if(updateGraphic) {
sketchViewModel.reset();
var selectedGraphicId;
graphicsLayer.graphics.items.forEach(function(v) {
var id = v.attributes.id;
if(graphicsObj[id]) selectedGraphicId = id;
});
if(selectedGraphicId) {
graphicsLayer.remove(graphicsObj[selectedGraphicId]);
delete graphicsObj[selectedGraphicId];
}
} else {
alert('請選擇一個圖層~');
}
setActiveButton();
}
});
});
~~~
- 事件
- mouse縮放與拖動
- drag拖動
- 事件兼容
- animation/transition
- canvas
- 改變圖片顏色
- html轉圖片
- 視頻操作
- 圖片縮放、水印、放大鏡
- 虛線
- 圓環進度條
- 形狀事件
- 圓角矩形
- 繪制注意
- arcTo與貝塞爾
- 橢圓及橢圓進度
- 五角星進度
- 常用圖形
- 計算顯示文本寬度
- 算法
- 幾何算法
- 地圖應用相關
- 運行符
- web安全
- 新窗口打開
- xss
- 分享交流
- php環境搭建及xhr交互
- node環境搭建及xhr交互
- node之socketio
- svg之入門介紹
- svg動畫
- vue之搜索聯想
- vue之登錄和echarts
- vue之組件交互與slot
- vue之loading
- vue之上傳進度
- webpack及cli
- 開發技巧
- 常用
- 移動端
- 錯誤處理
- 預加載
- 代理判斷
- 數組擴展
- 對象擴展
- 字符串擴展
- 語音播報
- 收集
- 文章/日記
- 框架/庫/插件
- 工具
- 學習網站
- 專業術語
- 正則
- 常用驗證
- 方法基礎
- es6擴展
- 深入實踐
- 快捷使用
- html
- css
- http協議
- http
- https
- socket
- 地圖/圖表
- mapbox
- echarts
- arcgis
- MapView及事件
- 添加WMS/WMTS層
- 增刪點線面
- 入門使用
- popup彈層
- 大數據處理
- 批量點
- 批量線
- 在線繪制
- GraphicLayer顯示/隱藏
- 動態改變位置
- 去除版權信息
- 添加控件
- Symbol
- 自定義path標記
- 圖片標記
- 文本標記
- 旋轉
- UI
- 自定義
- 3D地圖
- 創建實例
- basemap
- 底圖切換
- 自定義底圖
- 中心和范圍
- pupup彈層更新
- 坐標轉換
- 方向線
- leaflet
- amap
- 框架/類庫/腳手架
- vue
- 常見問題
- 組件框架
- vue-router
- 命名視圖
- url參數映射到prop
- sublime支持
- 隨手記
- 常用功能
- threejs
- 常用效果
- 其他特效
- requirejs
- 簡單使用
- jquery
- 方法擴展
- 使用筆記
- 組件擴展
- react
- 黨見問題
- 學習筆記
- 學習筆記-進階
- react-redux
- react-router
- redux
- 其他模塊說明
- 組件框架
- sublime支持
- gulp
- 安裝使用
- js壓縮
- css壓縮
- 組合使用
- copy文件
- 項目使用
- protobuf
- 入門
- layui
- 登錄驗證
- laydate
- 安裝工具
- yarn
- reactNative
- 入門介紹
- vueNative
- 入門介紹
- 版本控制
- git常用
- git擴展
- git問題
- git其他
- git擴展2
- 編輯器
- vscode
- atom
- webstorm
- 插件
- clipboard
- 奇淫巧技
- js
- 個性打印
- css
- 濾鏡效果
- 文本省略
- 當前色
- 新特性
- 花樣邊框效果
- 波紋效果
- 個性placeholder
- 偽元素內容
- 容器居中
- 知識點
- js
- 遞歸
- 沙箱
- 內存泄漏
- es6語法
- 變量介紹
- FileRead
- ajax
- web存儲
- css
- rem布局