Clone GraphicsOverlay

821
4
02-21-2019 03:13 AM
GonzaloMuöoz
Occasional Contributor

Hi,

I have a map and an overview map, and I'd like to have same graphics on both maps, but when trying to assign a GraphicsOverlay to the GraphicOverlays of both maps, it returns an exception that object is already owned, because both maps have same graphics.

How can this be done? Can I clone or copy graphics o GraphicOverlays?

Regards

Gonzalo

0 Kudos
4 Replies
ChristopherKoenig
Esri Contributor

Hi Gonzalo,

This can be done.  Each map will need to have its own GraphicsOverlay object and its own renderer object.  You can however use the same maker symbol for both renderers.

Sincerely,

Chris

JoeHershman
MVP Regular Contributor

MapView:GraphicsOverlays is a GraphicsOverlayCollection which has a CopyTo method.  This would be similiar to a Clone.  You create a new array and then copy into that array and then you can assign the new GraphicsOverlayCollection to the overview MapView

LayerCollection operationalLayers = new LayerCollection();
Layer[] tempLayers = new Layer[map.OperationalLayers.Count];
map.OperationalLayers.CopyTo(tempLayers, 0);

foreach (var operationalLayer in tempLayers)
{
map.OperationalLayers.Remove(operationalLayer);
operationalLayers.Add(operationalLayer);
}‍‍‍‍‍‍‍‍‍

The above is done to replace the OperationalLayers in a map, but the general idea is the same, just using GraphicOverlays.  Instead of the .Remove, .Add like above use the temp array and add them to you other MapView

GonzaloMuöoz
Occasional Contributor

Your approach ends into the same result:

Esri.ArcGISRuntime.ArcGISRuntimeException: 'Object already owned.: Graphic cannot be added - it is already in an owning collection.'

0 Kudos
ThadTilton
Esri Contributor

I think you'll have to copy each graphic and add it to the other graphics overlay. 

Here's how I did it:

private void CopyGraphicsButton_Click(object sender, RoutedEventArgs e)
{
GraphicsOverlay go1 = MapView1.GraphicsOverlays.FirstOrDefault();
GraphicsOverlay go2 = MapView2.GraphicsOverlays.FirstOrDefault();

foreach(Graphic g in go1.Graphics)
{
Graphic newGraphic = CopyGraphic(g);
go2.Graphics.Add(newGraphic);
}
}

private Graphic CopyGraphic(Graphic inGraphic)
{
Graphic copyGraphic = new Graphic(inGraphic.Geometry, inGraphic.Attributes, inGraphic.Symbol);
return copyGraphic;
}

I hope that helps! Thad

0 Kudos