Hi, I'm a bit new to Javascript and the JSAPI, and am running into an issue.
I have an app with two maps. In the app, the user populates the first map by submitting a findTask. The results (points) are added to a Graphics layer. The user then is able to click on points they want to select for further analysis, and those points are copied to the second map.
The problem I'm having is when I copy the graphics to a new Graphics layer for the second map, they disappear from the first map. I'm seeing in Flex that there is a "graphic.Geometry.Clone()" function that allows one to clone points. Is there an equivalent in JSAPI? Or is there any other way I can copy (not cut) points from one map to the other?
array.map(firstLayer.graphics, function (graphic) { if (graphic.attributes["selected"] == true) { secondLayer.add(graphic); graphic.setSymbol(Selected_Marker); } });
Solved! Go to Solution.
You could simplly create a new graphic.
var newGraphic = new Graphic(oldGraphic.toJson());
You could simplly create a new graphic.
var newGraphic = new Graphic(oldGraphic.toJson());
That works perfectly... Thanks!
You should be able to accomplish this by getting the JSON object from the Graphic and then creating a new Graphic using the JSON object. It would look something like this:
require(["esri/graphic"], function(Graphic) { … you code here … graphicJSON = originalGraphic.toJson() graphicCopy = new Graphic(graphicJSON) … you code here … });
There may be a more elegant way to this, but this approach is the first one that comes to my mind.
Nifty little trick! Very useful.