Sketch Widget - get geometries

513
6
Jump to solution
08-17-2019 11:13 AM
AzariaszTrzciński
New Contributor III

Hi everyone!

I've got a code: JS Bin - Collaborative JavaScript Debugging, I can mark something, but how can I save my work so I could use it later? I would be great to have it in JSON format. Could someone help me?

0 Kudos
1 Solution

Accepted Solutions
AzariaszTrzciński
New Contributor III

Yeah! I did it!
If someone would to copy this is all the code: JS Bin - Collaborative JavaScript Debugging 

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Azariasz,

   You can use the toJSON and fromJSON method of the Graphics class. https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html#toJSON 

0 Kudos
AzariaszTrzciński
New Contributor III

Thank you for comment, I saw this method, but I still don't know how to get geometries?
In 3.21 version I used this code:

function addGraphic(evt) {
		//deactivate the toolbar and clear existing graphics 
		tb.deactivate(); 
		map.enableMapNavigation();
		
		// figure out which symbol to use
		var symbol = fillSymbol;
		
		map.graphics.add(new Graphic(evt.geometry, symbol));
		var obj=evt.geometry.rings;
		var poly_cords="";
		for ( var path = 0; path <obj.length; path ++ ) {
			//For each point in the path...
			for ( var pt = 0; pt < obj[path].length; pt++ ) {
				poly_cords+=obj[path][pt][0].toFixed(1)+', ';
				poly_cords+=obj[path][pt][1].toFixed(1)+' \n';
			}
			poly_cords+='---\n';
		}
		document.getElementById('cords').value += poly_cords;
	}

Now I even try:

sketch.on("draw-end", addGraphic);
function addGraphic(evt) {
	console.log("WORKS!");
}

but still nothing :( How can I get it?

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor
sketch.on('create', function(evt){
  if(evt.state === 'complete'){
    console.log(evt.graphic);
  }
});
AzariaszTrzciński
New Contributor III

It helps a lot! I also played with

sketch.on("update", function(event){
	...
}‍‍‍

but I realised that I really don't need to know about every single change. I only want to save done work. It should be like this:

  • user is adding / changing / deleting shapes
  • then click the button "Save my work"
  • program gather all geometries, convert to JSON or string
  • add to database

How to do that? Is it possible?

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Same thing check the event state for "complete"

sketch.on("update", function(event){
  if(event.state === 'complete'){
    //do something
  }
}
AzariaszTrzciński
New Contributor III

Yeah! I did it!
If someone would to copy this is all the code: JS Bin - Collaborative JavaScript Debugging 

0 Kudos