By Daniel Du
Are you getting MgNotImplementedException when trying to add new layers to map? If yes, keep reading.
Here is my code snippet to create a MapGuide layer and insert it to current map, but it throws MgNotImplementedException at map.Save():
//mapguide site using current session
if (siteConnection == null)
{
MgUserInformation userInfo =
new MgUserInformation(sessionId);
siteConnection = new MgSiteConnection();
siteConnection.Open(userInfo);
}
//Create resource service from site connection
MgResourceService resSvc = siteConnection
.CreateService(MgServiceType.ResourceService)
as MgResourceService;
//Declare an resource id for layer
MgResourceIdentifier filterLayerId =
new MgResourceIdentifier(sessionLayerResId);
//Open current map
MgMap map = new MgMap();
map.Open(resSvc, mapName);
//Create a MgLayerBase object
MgLayerBase filteredLayer =
new MgLayerBase(filterLayerId, resSvc);
//set up the new layer
filteredLayer.LegendLabel = “filtered”;
filteredLayer.Selectable = true;
filteredLayer.DisplayInLegend = true;
filteredLayer.Name = “_filtered”;
filteredLayer.Group = map.GetLayerGroups()[0];
//insert the layer into map on top
map.GetLayers().Insert(0, filteredLayer);
//MgNotImplementedException here ****
map.Save(resSvc);
When running this code snippet, I always get MgNotImplementedException, detailed error message goes as below:
==============================
Not implemented.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: OSGeo.MapGuide.MgNotImplementedException: Not implemented.
Source Error:
Line 412: map.GetLayers().Insert(0, filteredLayer);
Line 413:
Line 414: map.Save(resSvc);
Line 415:
Line 416: }
==============================
Is there anything wrong with my code above?
Solution:
Use MgLayer instead of MgLayerBase when inserting layers into map, it is simple but it is a little tricky and not easy to debug. so the working code snippet will be:
//mapguide site using current session
if (siteConnection == null)
{
MgUserInformation userInfo =
new MgUserInformation(sessionId);
siteConnection = new MgSiteConnection();
siteConnection.Open(userInfo);
}
//Create resource service from site connection
MgResourceService resSvc = siteConnection
.CreateService(MgServiceType.ResourceService)
as MgResourceService;
//Declare an resource id for layer
MgResourceIdentifier filterLayerId =
new MgResourceIdentifier(sessionLayerResId);
//Open current map
MgMap map = new MgMap();
map.Open(resSvc, mapName);
//Create a MgLayerBase object
MgLayer filteredLayer =
new MgLayer(filterLayerId, resSvc);
//set up the new layer
filteredLayer.LegendLabel = “filtered”;
filteredLayer.Selectable = true;
filteredLayer.DisplayInLegend = true;
filteredLayer.Name = “_filtered”;
filteredLayer.Group = map.GetLayerGroups()[0];
//insert the layer into map on top
map.GetLayers().Insert(0, filteredLayer);
//works fine now
map.Save(resSvc);
Hope this helps you.

Leave a Reply