Clone features from getSelectedFeatures() method

2153
4
Jump to solution
08-04-2016 06:35 AM
clementinechasles
New Contributor

Hello !

I would like to clone in a new array (a deep copy without references) the features I have selected from a feature layer with the getSelectedFeatures() method.

I try to use lang.clone from dojo but it doesn't work. I have this error : "TypeError: this._url is null".

I understand it cames from the feature layer. But I don't know how to fix this error or even if it's possible to do what I want to do by this way. I'm quite new with JSAPI.

I already used lang.clone with polygons from serviceAreaSolveResult and it worked very well !

Could someone help me on this problem ?

Thanks in advance.

Clemini.

0 Kudos
1 Solution

Accepted Solutions
thejuskambi
Occasional Contributor III

Cloning Graphic objects may not be a good idea. especially if it is already added to the map. GraphicsLayer to which it has been added will be one of the properties of the graphic object.

The best thing to do is to convert it to json and create a new graphic from that json.

var jsonGraphic = graphic.toJson();

var newGraphic = new Graphic(jsonGraphic);

View solution in original post

4 Replies
TimWitt2
MVP Alum

You could do a for loop on your getSelectedFeatures and then use push to get them into a new array?

0 Kudos
thejuskambi
Occasional Contributor III

Cloning Graphic objects may not be a good idea. especially if it is already added to the map. GraphicsLayer to which it has been added will be one of the properties of the graphic object.

The best thing to do is to convert it to json and create a new graphic from that json.

var jsonGraphic = graphic.toJson();

var newGraphic = new Graphic(jsonGraphic);

clementinechasles
New Contributor

Thank you very much !

It works.

I had already seen this method (toJson) but I had badly implemented.

0 Kudos
thejuskambi
Occasional Contributor III

Glad I could help, Please mark the question as answered.

0 Kudos