wiki:maestro/MaestroAPI/samples/4.0/CreateRuntimeMap

Version 3 (modified by davidwilhelmsson, 12 years ago) ( diff )

Added syntax formatting

Creating and working with a Runtime Map

The IMappingService interface provides the means of creating and working with the runtime map. Note that you must call Save() on the Runtime Map before using any API that operates on the Runtime Map (eg. Rendering)

The following example shows how to create a new RuntimeMap

(C#)

IServerConnection conn = ConnectionProviderRegistry.CreateConnection("Maestro.Http", 
    "Username", "Administrator",
    "Password", "admin",
    "Url", "http://localhost/mapguide/mapagent/mapagent.fcgi");

//Create the Mapping Service. Some implementations of IServerConnection may not support this service, so
//its best to inspect the capability object of this connection to determine if this service type is supported
IMappingService mapSvc = (IMappingService)conn.GetService((int)ServiceType.Mapping);

//Get our map definition
ResourceIdentifier resId = new ResourceIdentifier("Library://Samples/Sheboygan/Maps/Sheboygan.MapDefinition");
IMapDefinition mdf = (IMapDefinition)conn.ResourceService.GetResource(resId.ToString());

//Calculate the meters per unit value, this requires the official MapGuide API. Otherwise, you need
//to know this value up-front in order to render images with this instance
double metersPerUnit = 1.0;
if (!string.IsNullOrEmpty(mdf.CoordinateSystem))
{
    MgCoordinateSystemFactory factory = new MgCoordinateSystemFactory();
    MgCoordinateSystem cs = factory.Create(mdf.CoordinateSystem);
    metersPerUnit = cs.ConvertCoordinateSystemUnitsToMeters(1.0);
}

//Generate our runtime map resource id. This must be session-based
ResourceIdentifier rtMapId = new ResourceIdentifier(resId.Name, ResourceTypes.RuntimeMap, conn.SessionID);

//Create the runtime map using our meters per unit value
RuntimeMap map = mapSvc.CreateRuntimeMap(rtMapId, mdf, metersPerUnit);

//Set some display parameters for this map
map.DisplayWidth = 1024;
map.DisplayHeight = 1024;
map.DisplayDpi = 96;

//We have to save it first before we can render from it or use any other API that requires this 
//current map state. Remember to call Save() everytime you change the state of the map
map.Save();

This sample shows how to open an existing Runtime Map. If you want to open a runtime map created by the official API (ie. One created when loading a Web Layout or Application Definition), you will have to use a resource id of the following format:

Session:<session-id>://<map-name>.Map

(C#)

IServerConnection conn = ConnectionProviderRegistry.CreateConnection("Maestro.Http", 
    "Username", "Administrator",
    "Password", "admin",
    "Url", "http://localhost/mapguide/mapagent/mapagent.fcgi");

//Create the Mapping Service. Some implementations of IServerConnection may not support this service, so
//its best to inspect the capability object of this connection to determine if this service type is supported
IMappingService mapSvc = (IMappingService)conn.GetService((int)ServiceType.Mapping);

ResourceIdentifier rtMapId = new ResourceIdentifier(mapName, ResourceTypes.RuntimeMap, conn.SessionID);
RuntimeMap rtMap = mapSvc.OpenMap(rtMapId);

This samples shows to to manipulate the state of the runtime map. In this case, toggling the visibility of the Parcels layer

(C#)

IServerConnection conn = ConnectionProviderRegistry.CreateConnection("Maestro.Http", 
    "Username", "Administrator",
    "Password", "admin",
    "Url", "http://localhost/mapguide/mapagent/mapagent.fcgi");

//Create the Mapping Service. Some implementations of IServerConnection may not support this service, so
//its best to inspect the capability object of this connection to determine if this service type is supported
IMappingService mapSvc = (IMappingService)conn.GetService((int)ServiceType.Mapping);

ResourceIdentifier rtMapId = new ResourceIdentifier(mapName, ResourceTypes.RuntimeMap, conn.SessionID);
RuntimeMap rtMap = mapSvc.OpenMap(rtMapId);

//Toggle visibility of the Parcels layer
rtMap.Layers["Parcels"].Visible = false;

//Any changes to map state require calling Save()
rtMap.Save();

This example shows how to render an opened Runtime Map to a file

(C#)

IServerConnection conn = ConnectionProviderRegistry.CreateConnection("Maestro.Http", 
    "Username", "Administrator",
    "Password", "admin",
    "Url", "http://localhost/mapguide/mapagent/mapagent.fcgi");

//Create the Mapping Service. Some implementations of IServerConnection may not support this service, so
//its best to inspect the capability object of this connection to determine if this service type is supported
IMappingService mapSvc = (IMappingService)conn.GetService((int)ServiceType.Mapping);

ResourceIdentifier rtMapId = new ResourceIdentifier(mapName, ResourceTypes.RuntimeMap, conn.SessionID);
RuntimeMap rtMap = mapSvc.OpenMap(rtMapId);

using(Stream stream = mapSvc.RenderDynamicOverlay(map, null, "PNG"))
{
    //Write this stream out to a file
    using (var fs = new FileStream("RenderMap.png", FileMode.OpenOrCreate))
    {
        int read = 0;
        do
        {
            read = source.Read(buf, 0, buf.Length);
            target.Write(buf, 0, read);
        } while (read > 0);
    }
}

Note: See TracWiki for help on using the wiki.