Jan 2, 2010

How to add map resource dynamically to MapResourceManager + ArcGIS 9.3 and 9.3.1 .Net WebADF

In ArcGIS webADF, After publishing the services using ArcGIS SERVER(say map service), we can just add a map resource to the MapResourceManager manually in design time.

Now lets see how to add the map resource dynamically through code. Because mostly we may have multiple map resource that has to be shown only on request.

Below is the code through which we can achieve this.

Method call:
AddMapResourceDynamically("GlobalService", "Layers@GlobalService", false);
- GlobalService is the service name.
- Layers@GlobalService is the service definition(i.e Layers@Service_Name is the format)
- true or false to list the resource in the TOC Control.

Method Definition:
private void AddMapResourceDynamically(string resName, string resDefinition, bool IsTOC)
        string m_ResourceName = string.Empty;
        MapResourceItem mapResourceItem = null;
        GISResourceItemDefinition resDef = null;
        DisplaySettings displaySettings = null;
            mapResourceItem = new MapResourceItem();
            resDef = new GISResourceItemDefinition();
            mapResourceItem.Name = resName;

            // The services url which we use to fetch the services.
            resDef.DataSourceDefinition = @"http://hostname/arcgis/services";
            resDef.DataSourceType = "ArcGIS Server Internet";
            resDef.ResourceDefinition = resDefinition;
            resDef.DataSourceShared = true;
            mapResourceItem.Parent = MapResourceManager1;
            mapResourceItem.Definition = resDef;

            displaySettings = new DisplaySettings();
            displaySettings.Transparency = 0;
            displaySettings.Visible = true;
            displaySettings.DisplayInTableOfContents = IsTOC;
            displaySettings.ImageDescriptor.TransparentColor = System.Drawing.Color.White;
            displaySettings.ImageDescriptor.TransparentBackground = true;
            mapResourceItem.DisplaySettings = displaySettings;

            // Insert the new resource item at the beginning (top).             MapResourceManager1.ResourceItems.Insert(MapResourceManager1.ResourceItems.Count, mapResourceItem);

            // Create the map resource and return a reference in case it will be the 
            // primary map resource.
            IMapResource mapResource =       MapResourceManager1.CreateResource(mapResourceItem);

            // If no other resource items are present, set the new map resource item as the 
            // primary map resource.
            if (MapResourceManager1.ResourceItems.Count < 1)
                Map1.PrimaryMapResource = mapResourceItem.Name;
        catch (Exception)
            mapResourceItem = null;
            resDef = null;
            displaySettings = null;

The above code will add the map resource dynamically to the MapResourceManager and display it on the map on runtime.


Alessandro Accardo said...

Hi, I've tried your code, and this doesn't seem to work properly. The resource is properly added but the map control doesn't refresh. I can view the brand new resource added just by setting his visibility false and then true in the toc and then panning the map.
How to fix this problem? I have set Image blending mode to "Browser" and I call this method from a simple button in an update panel. What can I do?

Mukund The Lion said...

The code which i provided is the one currently i am using in my projects and it works without issues. something may be wrong in Assigning the resource.
1. if u are using more than one resource , say u are adding one resource through the code i provided and one through manual process in the design view,then please make sure the transparentBackgrount property is set true.
2. Try changing the order of the resources added.

Try the above tips and let me know if you can resolve the issue.

Alessandro Accardo said...

Hi, and thank you for the reply.
I said my problem is about the map refresh, and not about the resources' hierarchy. I've modified the position parameter in the resource insert statement by replacing your resources count with a 0, because it's useless with a Add method that complains the same thing as it but with only one input parameter. So I can insert a resource at the top level in the toc and hierarchy.
My problem, as I just said, is the map refresh that doesn't work at all.

Mukund The Lion said...

As i know , the map refresh happens whenever the page loads, so if u are calling the AddResource method in a button click, obviously the page should load and the map should refresh. But if u are trying to ajaxify the process using an update panel, then the problem is that.Try to ajaxify using some other way may be callbacks.
Try removing the update panel for the button and see what's happening.

Steve.McKinney@SICS said...

Awesome code! Thank you for posting. Please keep up the good work!

Mukund The Lion said...

You are Welcome! Thanks for Encouraging!

jony said...

Hi. I Try Your code and return.

"Map resource \"btsadmin\" is in an unknown coordinate system. A map resource with an unknown coordinate system can only be displayed in the Map control if it is the only resource."

I Have set the coordinate properly. what is happening to this..?? please advice.?