自定义多边形需要用到的js类库
require(["esri/widgets/Sketch/SketchViewModel" ], function (SketchViewModel)
构造多边形:
var pointSymbol = { // symbol used for points
type: "simple-marker", // autocasts as new SimpleMarkerSymbol()
style: "square",
color: "#8A2BE2",
size: "16px",
outline: { // autocasts as new SimpleLineSymbol()
color: [255, 255, 255],
width: 3 // points
}
}
var polylineSymbol = { // symbol used for polylines
type: "simple-line", // autocasts as new SimpleLineSymbol()
color: "#8A2BE2",
width: "4",
style: "dash"
}
var polygonSymbol = { // symbol used for polygons
type: "simple-fill", // autocasts as new SimpleFillSymbol()
color: "rgba(138,43,226, 0.8)",
style: "solid",
outline: {
color: "white",
width: 1
}
}
view.when(function () {
// create a new sketch view model
var tempGraphicsLayer = new GraphicsLayer();
var sketchViewModel = new SketchViewModel({
view: view,
layer: tempGraphicsLayer,
pointSymbol: pointSymbol,
polylineSymbol: polylineSymbol,
polygonSymbol: polygonSymbol
});
setUpClickHandler();
sketchViewModel.on("draw-complete", addGraphic);
sketchViewModel.on("update-complete", addGraphic);
sketchViewModel.on("update-cancel", addGraphic);
function addGraphic(evt) {
var geometry = evt.geometry;
var symbol;
// Choose a valid symbol based on return geometry
switch (geometry.type) {
case "point":
symbol = pointSymbol;
break;
case "polyline":
symbol = polylineSymbol;
break;
default:
symbol = polygonSymbol;
break;
}
// Create a new graphic; add it to the GraphicsLayer
var polygonGraphic = new Graphic({
geometry: geometry,
symbol: symbol
});
tempGraphicsLayer.add(polygonGraphic);
// Remove stored reference to the updated graphic
// Required in 'update-complete' callback specifically
updateGraphic = null;
}
function setUpClickHandler() {
view.on("click", function (evt) {
view.hitTest(evt).then(function (response) {
var results = response.results;
// Found a valid graphic
var polygon = polygonGraphic.geometry; //polygon
if (Layer.visible) {
for (var i = 0; i < Layer.graphics.items.length; i++) { //遍历你在图层里添加的标记集合
var point = Layer.graphics.items[i].geometry;
if (polygon.contains(point)) {
//点在面中
}
}
}
if (results.length && results[results.length - 1].graphic) {
// Check if we're already editing a graphic
if (!updateGraphic) {
// Save a reference to the graphic we intend to update
updateGraphic = results[results.length - 1].graphic;
// Remove the graphic from the GraphicsLayer
// Sketch will handle displaying the graphic while being updated
tempGraphicsLayer.remove(updateGraphic);
sketchViewModel.update(updateGraphic.geometry);
}
}
}
});
});
}
触发事件
sketchViewModel.create("polygon");
清除
sketchViewModel.reset();
tempGraphicsLayer.removeAll();