Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

share|improve this question
    
see this thread for a Polyline example: gis.stackexchange.com/questions/137609/… –  John Gravois May 13 at 21:41

1 Answer 1

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:

[[[coord1,coord2],[coord3,coord4],[coord5,coord6]]]

Here is an example of how to do that:

HTML

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

JavaScript

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

for(i=0;i<arrayOfCoordinates.length;i++){
    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).

require([
  "esri/graphic" 
], function(Graphic) {
  var myPolygon = {"geometry":{"rings":arrayOfCoordinates,"spatialReference":{"wkid":26191}},
    "symbol":{"color":[0,0,0,64],"outline":{"color":[0,0,0,255],
    "width":1,"type":"esriSLS","style":"esriSLSSolid"},
    "type":"esriSFS","style":"esriSFSSolid"}};
  var gra = new Graphic(myPolygon);

map.graphics.add(gra);
});
share|improve this answer
    
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 at 16:50
    
I've updated my answer to show actually adding the graphic to your map. –  ryanrandom May 18 at 13:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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