In the last post, we created polyline geometry using polyline library. Today in this post, i will describe how to create polygon using "polygon library" with Esri ArcGIS JS API. And we will add this polygon to map graphically.
We will learn how to create polylgon using ArcGIS JS API "polygon library".
You can use any operating system and any ide or editor. I will use "Windows PC" and "Visual Studio Code". And i will use ESRI ArcGIS JS Library from web. But you can install library using bower from github repository.
| -- ESRI ArcGIS JS API 3.23
| -- | -- js
| -- | -- | -- main.js
| -- | -- css
| -- | -- | -- main.css
| -- | -- fonts
| -- | -- | -- font-awesome-4.7.0
| -- | -- | -- | -- css
| -- | -- | -- | -- |-- font-awesome.min.css
| -- | -- | -- | -- fonts
| -- | -- | -- | -- less
| -- | -- | -- | -- scss
| -- | -- index.html
*Only used files in project has been shown. There are other files for dependency.
Firstly we will create polygon using ESRI ArcGIS JS API "Polygon" library. We will work in "main.js" file.
We will add polygon library using require method.
require([
"esri/geometry/Polygon"
], function (
Polygon
){...});
Now, we can create polygon instance. We will not use any parameters. We will use addRing method to add points that creating our polyline in next step.
var polygon = new Polygon();
We will add points to polygon using "addRing" method. This method has take one parameter that is an array veriable consist of points. We already learned how to create point. I will create polygon that has rectangle shape.
polygon.addRing([
new Point(28.991808, 41.014267),
new Point(28.991808, 41.004267),
new Point(29.001808, 41.004267),
new Point(29.001808, 41.014267),
new Point(28.991808, 41.014267)
]);
We will create simple fill symbol to show our polygon on map.
We will add simple fill symbol library using require method.
require([
"esri/symbols/SimpleFillSymbol"
], function (
SimpleFillSymbol
){...});
We will create new symbol instance. We will not use any parameters for now. But we will learn how to create special symbol in future posts.
var simpleFillSymbol = new SimpleFillSymbol();
We learned how to create graphic previous post. We will create new graphic instance using two parameters. First parameter is geometry for this example it will be our polygon. And second parameter is symbol for this example it will be our simple fill symbol.
var polygonGraphic = new Graphic(polygon, simpleFillSymbol);
Now, we can add polygon graphic to graphics layer using add method.
graphicsLayer.add(polygonGraphic);
We can see the result by running the "index.html" file.
We learned how to create polygon geometry and add to map using ArcGIS JS API and Dojo in web.
Thank you for your attention and see you next time!
You can view old post below links.