Well I am building a GIS web app using ArcGIS Javascript API , I want to create a user interface in which the user provides an array of coordinates and I have to get those coordinates and create a polygon on the map from this array of coordinates. Do you have any idea?


You could use a textarea to collect pairs of coordinates. The coordinates should have a comma between them and each pair should be separated by a new line. Then format the pairs to look like:


Here is an example of how to do that:


<textarea id='textareaID'></textarea>


var arrayOfCoordinates = document.getElementById('textareaID').value.split('\n');

    arrayOfCoordinates[i] = arrayOfCoordinates[i].replace(/,/g, "],[");
arrayOfCoordinates = JSON.parse("[[[" + arrayOfCoordinates + "]]]")

Here is the code required (from the ESRI Javascript API) to create a new graphic using JSON. Pass in your arrayOfCoordinates variable as your rings and the wkid for your map's spatial reference. Then use your map object to add the graphic with map.graphic.add(graphic).

], function(Graphic) {
  var myPolygon = {"geometry":{"rings":arrayOfCoordinates,"spatialReference":{"wkid":26191}},
  var gra = new Graphic(myPolygon);

  • I've tried to use the code in your answer but I didn't know how . I found a sample code of adding polygon to the map from an array of coordinates in wicket arthur , but when I changed the spatial reference in config from WG84(4326) to nord maroc (26191) nothing showed in my map .Here is the code I found in the internet github.com/arthur-e/Wicket/blob/master/doc/arcgis.html Thank you – Zineb Bouhamyia May 16 '15 at 16:50
  • I've updated my answer to show actually adding the graphic to your map. – ryanrandom May 18 '15 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.