console
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Add Multiple Polygons to FeatureLayer</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.24/esri/themes/light/main.css" />
<script src="https://js.arcgis.com/4.24/"></script>
<style>
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>
<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer",
"esri/tasks/support/FeatureSet",
"esri/geometry/Polygon"
], function (Map, MapView, FeatureLayer, FeatureSet, Polygon) {
const map = new Map({
basemap: "streets"
});
const view = new MapView({
container: "viewDiv",
map: map,
zoom: 10,
center: [-122.4194, 37.7749]
});
const featureCollection = {
featureSet: {
features: [],
geometryType: "polygon"
},
layerDefinition: {
geometryType: "esriGeometryPolygon",
fields: []
}
};
const featureLayer = new FeatureLayer({
source: featureCollection,
objectIdField: "ObjectID",
renderer: {
type: "simple",
symbol: {
type: "simple-fill",
color: "blue",
outline: {
color: [255, 255, 255, 0.5],
width: 1
}
}
}
});
map.add(featureLayer);
const polygon1 = {
geometry: new Polygon({
rings: [
[
[-122.45, 37.8],
[-122.45, 37.9],
[-122.35, 37.9],
[-122.35, 37.8],
[-122.45, 37.8]
]
]
})
};
const polygon2 = {
geometry: new Polygon({
rings: [
[
[-122.4, 37.7],
[-122.4, 37.8],
[-122.3, 37.8],
[-122.3, 37.7],
[-122.4, 37.7]
]
]
})
};
featureCollection.featureSet.features.push(polygon1, polygon2);
featureCollection.featureSet.extent = featureLayer.fullExtent;
featureLayer.refresh();
});
</script>
</head>
<body>
<div id="viewDiv"></div>
</body>
</html>