user903772 user903772 - 3 months ago 15
Javascript Question

948ESRI Javascript polygon

I am using esri javascript 3.5. I have a list of points that I want to draw it into a polyon. I found this https://developers.arcgis.com/en/javascript/jssamples/util_relation.html but I don't know how to use it. Can I know where I can get a sample? Say I have a array of points...

dojo.require("esri.map");
dojo.require("esri.graphic");

var map ;
function init() {
map = new esri.Map("map",{
basemap:"topo",
center:[103.8, 1.35], //long, lat
zoom:13
});
dojo.connect(map,"onLoad", loadData);
}//end init
function loadData(){
var arr =[];
var p = new esri.geometry.Point(103.8, 1.35);
arr.push(p);
p = new esri.geometry.Point(103.8, 1.35222);
arr.push(p);
var polySymbolRed = new esri.symbol.SimpleFillSymbol(
esri.symbol.SimpleLineSymbol.STYLE_SOLID,
new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,
new dojo.Color([0, 0, 0, 1]), 1),
new dojo.Color([255, 0, 0, 0.2])
);

var gra = new esri.Graphic(arr,polySymbolRed);
map.graphics.add(gra);


}

Answer

You're very close, you just need to massage that array a bit before creating the .Graphic:

var poly = new esri.geometry.Polygon({
                "spatialReference": {" wkid":4326 },
                "fields": [{
                    "name": "FID",
                    "type": "esriFieldTypeOID",
                    "alias": "FID",
                    "domain": null          
                },...]
            });
var ring = new Array();

//push points into array

poly.addRing(ring);

var gra = new esri.Graphic(poly,polySymbolRed);
//Maybe:
//var gra = new esri.Graphic(esri.geometry.geographicToWebMercator(poly),polySymbolRed);
//depending on what SR your map is using...
Comments