How to clone a graphic to a new Graphics layer

3096
3
Jump to solution
04-21-2016 07:40 AM
YogiPatel
New Contributor II

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);

  }

});

0 Kudos
1 Solution

Accepted Solutions
thejuskambi
Regular Contributor

You could simplly create a new graphic.

var newGraphic = new Graphic(oldGraphic.toJson());

View solution in original post

3 Replies
thejuskambi
Regular Contributor

You could simplly create a new graphic.

var newGraphic = new Graphic(oldGraphic.toJson());

View solution in original post

YogiPatel
New Contributor II

That works perfectly... Thanks!

0 Kudos
DavidBlanchard
Esri Contributor

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.

0 Kudos