Select to view content in your preferred language

How do I copy A layer service from one map to another?

2646
2
06-28-2010 07:05 AM
jonataspovoas
Regular Contributor
I have two pages on my project (main and print pages), and both has the same map with the same layers

I have this Layers on my maps:

<esri:Map x:Name="MapaImpressao" Background="#FFC3D2E5" IsLogoVisible="False" Extent = "-183.780014728684,-124.638910568344,179.99999,124.638910568344" Margin="-1,7,-1,-14">
[INDENT][INDENT]<esri:ArcGISTiledMapServiceLayer ID="Streets World 2D" Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>

<esri:ArcGISDynamicMapServiceLayer ID="StateCityHighway USA"
Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StateCityHighway_USA/MapSe..."/>

<esri:GraphicsLayer ID="MyGraphicsLayer" />[/INDENT][/INDENT]
</esri:Map>

here's my problem: During execution I have to copy the Layers Alteration from one to another. I already copy the Graphics from one GraphicLayer to another, but the Layers from the  services that the user Disabled/Enabled are giving me a headache. Does anyone knows how to copy them to another map?

when i disable a service, i can copy the change, but when disabling a Layer I can't copy it.

This copy the change at the service
foreach (Layer ChosenLayer in this.Mapa.Layers)
{
[INDENT][INDENT]if (!ChosenLayer.Visible) imprimir.MapaImpressao.Layers[ChosenLayer.ID].Visible = false;[/INDENT][/INDENT]
}

With this i copy my graphycsLayer:
GraphicsLayer Origem = Mapa.Layers["MyGraphicsLayer"] as GraphicsLayer;
GraphicsLayer Destino = imprimir.MapaImpressao.Layers["MyGraphicsLayer"] as GraphicsLayer;
foreach (Graphic i in Origem)
{
[INDENT][INDENT]Graphic g = new Graphic();
g.Geometry = i.Geometry;
g.Symbol = i.Symbol;
Destino.Graphics.Add(g);
[/INDENT][/INDENT]}

Here's a screen shot of the Layers listing box:
0 Kudos
2 Replies
PreetiMaske
Esri Regular Contributor
I tried following code to copy the Layers (altered from the default visibility) from one map to another and it works fine for me. Basically at the click of copy button I fetched the visible layers of the target ArcGISDynamicMapServiceLayer, created a new ArcGISDynamicMapServiceLayer in destination Map and gave same URL as Source map and set visiblity of layers and add the layer to destination Map.

private void copy_Click(object sender, RoutedEventArgs e)
    {
      ArcGISDynamicMapServiceLayer destinationLayer = new ArcGISDynamicMapServiceLayer();
      ArcGISDynamicMapServiceLayer OriginLayer = MyMap.Layers["MyDynamicLayer"] as ArcGISDynamicMapServiceLayer;
     
//give the URL as the source map's DynamicService Layer
      destinationLayer.Url=OriginLayer.Url;

//get all the visible layers from source service
       List<int> visibleLayerList =
          OriginLayer.VisibleLayers != null
          ? OriginLayer.VisibleLayers.ToList() : new List<int>();

//set the visiblelayers to what we got from above
      destinationLayer.VisibleLayers=visibleLayerList.ToArray();

//add to destination Map
      TargetMap.Layers.Add(destinationLayer);
    }
0 Kudos
jonataspovoas
Regular Contributor
Hi,

I've found some small bugs in the code u sugested: If it happens to try copying the service without any change in it's layers, it behaves as if the service was unshown, but the default visibility is enabled! Also, if i deactivate an service, and have a Layer unchecked on the list (in other words, also deactivated) it keeps showing the two activated layers of that service.

One more thing, could you explain to me how this lines works? When my boss asked me, i couldn't explain well.

List<int> visibleLayerList = OriginLayer.VisibleLayers != null ? OriginLayer.VisibleLayers.ToList() : new List<int>();

thanks a lot!
0 Kudos