Vishful thinking…

Adding a map resource dynamically to the map using the .NET ADF

Posted in ArcGIS, ESRI by viswaug on April 1, 2008

I just completed a feature in our current project allowing the user to be able to select the map resource to view from a list box and have the map loaded with the selected resource. To accomplish this, we had to add map resources to the map dynamically without it being preset on the MapResourceManager using the Visual Studio designer. Here is a code snippet to insert a “ArcGIS Server Local” map resource into the map.

private MapResourceItem AddMapResource(Map map, AGSLocalMapService mapService, int index)

        {

            MapResourceManager mrm = map.MapResourceManagerInstance;

            MapResourceItem mri = new MapResourceItem();

            GISResourceItemDefinition definition = new GISResourceItemDefinition();

            mri.Name = mapService.Name;

            definition.DataSourceDefinition = mapService.MapServerName;

            definition.DataSourceType = “ArcGIS Server Local”;

            definition.ResourceDefinition = mapService.DataFrame + “@” + mapService.MapService;

            definition.DataSourceShared = true;

            mri.Parent = mrm;

            mri.Definition = definition;

            DisplaySettings displaySettings = new DisplaySettings();

            displaySettings.DisplayInTableOfContents = true;

            displaySettings.Visible = true;

            displaySettings.ImageDescriptor.ImageFormat = ImageFormat.PNG24;

            mri.DisplaySettings = displaySettings;

 

            mrm.ResourceItems.Insert(index, mri);

            mrm.CreateResource(mri);

 

            return mri;

        }

and the snippet to insert a graphics resource item.

public static MapResourceItem AddGraphicsMapResource(Map map, string graphicsResourceName)

        {

            if (map != null)

            {

                MapResourceItem mapResourceItem = new MapResourceItem();

                GISResourceItemDefinition graphicsDef = new GISResourceItemDefinition();

                graphicsDef.DataSourceDefinition = “In Memory”;

                graphicsDef.DataSourceType = “GraphicsLayer”;

                mapResourceItem.Definition = graphicsDef;

                DisplaySettings graphicsDisplay = new DisplaySettings();

                graphicsDisplay.DisplayInTableOfContents = false;

                //graphicsDisplay.Transparency = 50.0F;

                graphicsDisplay.Visible = true;

                mapResourceItem.DisplaySettings = graphicsDisplay;

                mapResourceItem.Name = graphicsResourceName;

                map.MapResourceManagerInstance.Initialize();

                map.MapResourceManagerInstance.ResourceItems.Insert(0, mapResourceItem);

                map.MapResourceManagerInstance.CreateResource(mapResourceItem);

                return mapResourceItem;

            }

            return null;

        }