| 1 | |
| 2 | == Utilizing !RuntimeMap via Maestro API == |
| 3 | This page describes how to use the MaestroAPI from the [wiki:maestro MapGuide Maestro] project in your own application. |
| 4 | |
| 5 | An example of how to add a layer to the map BEFORE it is loaded, using Meastro API. To get Maestro API, please download latest Maestro package, then reference OSGeo.!MapGuide.MaestroAPI.dll and !MapGuideDotNetApi.dll only. |
| 6 | |
| 7 | If you choose the local version below, Maestro API communicates with MapGuide through a !LocalNativeConnection object which talks to MG through a native connection. So all the !MapGuide unmanaged DLL's must be present in the applications Bin folder. |
| 8 | |
| 9 | If you choose the remote version, Maestro API communicates with MapGuide through HTTP and no MapGuide binaries are required. |
| 10 | |
| 11 | Read about [wiki:maestro/MaestroAPI/basics the difference between LocalNativeConnection and HttpServerConnection here] |
| 12 | |
| 13 | (VB.NET) |
| 14 | {{{ |
| 15 | Imports OSGeo.MapGuide.MaestroAPI |
| 16 | ... |
| 17 | |
| 18 | 'Read the setup, either from QueryString, Form, Cookies or hardcoded values |
| 19 | Dim username As String = IIF(Request.Params("USERNAME") Is Nothing, "Anonymous", Request.Params("USERNAME")) |
| 20 | Dim password As String = IIF(Request.Params("PASSWORD") Is Nothing, "", Request.Params("PASSWORD")) |
| 21 | Dim layout As String = IIF(Request.Params("LAYOUT") Is Nothing, "Library://MyLayout.WebLayout", Request.Params("LAYOUT")) |
| 22 | Dim locale As String = IIF(Request.Params("LOCALE") Is Nothing, "en", Request.Params("LOCALE")) |
| 23 | |
| 24 | |
| 25 | 'Create a connection to MapGuide |
| 26 | Dim conn As IServerConnection |
| 27 | |
| 28 | 'Pick your connection method here |
| 29 | Dim local As Boolean = True |
| 30 | |
| 31 | If (local) Then |
| 32 | 'Replace "webconfig.ini" with the full path to "webconfig.ini" |
| 33 | conn = New LocalNativeConnection("webconfig.ini", username, password, "en") |
| 34 | Else |
| 35 | 'Replace "myserver" with the server name, or use "localhost" |
| 36 | Dim host As New Uri("http://myserver/mapguide/mapagent/mapagent.fcgi") |
| 37 | conn = New HttpServerConnection(host, username, password, "en", true) |
| 38 | End If |
| 39 | |
| 40 | |
| 41 | 'Get the original layout |
| 42 | Dim weblayout As WebLayout = conn.GetWebLayout(layout ) |
| 43 | |
| 44 | 'Get the mapDefinition |
| 45 | Dim mapDef As MapDefinition = conn.GetMapDefinition(weblayout .Map.ResourceId) |
| 46 | |
| 47 | 'Create a new MapLayer object |
| 48 | Dim layer As new MapLayerType() |
| 49 | |
| 50 | 'Absolute minimum properties |
| 51 | layer.Visible = true |
| 52 | layer.ResourceId = "Library://MyExtraLayer.LayerDefinition" |
| 53 | |
| 54 | 'Extra properties |
| 55 | layer.ExpandInLegend = true |
| 56 | layer.ShowInLegend = true |
| 57 | layer.LegendLabel = "My layer" |
| 58 | layer.Name = "MyLayer" |
| 59 | |
| 60 | 'Add the layer to the MapDefinition |
| 61 | mapDef.Layers.Add(layer) |
| 62 | |
| 63 | 'Generate a temporary MapDefinition id |
| 64 | Dim tempMapDef As String = new ResourceIdentifier("MyMap", ResourceTypes.MapDefinition, con.SessionID) |
| 65 | |
| 66 | 'Save the modified map at its new temporary location |
| 67 | con.SaveResourceAs(mapDef, tempMapDefName) |
| 68 | |
| 69 | 'Generate a temporary WebLayout id |
| 70 | Dim tempWebLayout As String = new ResourceIdentifier("MyMap", ResourceTypes.WebLayout, con.SessionID) |
| 71 | |
| 72 | 'Update the WebLayout to point at the modified MapDefinition |
| 73 | weblayout.Map.ResourceId = tempMapDef |
| 74 | |
| 75 | 'Save the modified layout at its new temporary location |
| 76 | con.SaveResourceAs(weblayout, tempWebLayout) |
| 77 | |
| 78 | 'Redirect the page to the viewer, using the temporary map |
| 79 | Response.Redirect("/mapguide/ajaxviewer/?LAYOUT=" & Server.UrlEncode(tempWebLayout) & "&SESSION=" & Server.UrlEncode(con.SessionID), true) |
| 80 | }}} |