[mapguide-users] MapGuide with Openlayers (Searching for Detailed yet Simple Development Examples)

Pietro Ianniello pietro.ianniello at gmail.com
Thu Mar 18 17:04:27 EDT 2010


This is a simple example using ASP.NET and OpenLayers using the usual
Sheboygan example. Sorry but I don't know yet how to load legend. For the
selection I have not tried yet, but OpenLayers accepts the xml returned by
selection in params, so using selection  as in MapGuide Dev''s guide should
work [see comment in code] - I didn't tested.
For tooltips you can query the server on mouse move, standard AJAX stuff,
but I would do it only in intranets or with big server power, otherwise you
would be bombing the server with requests.
To have more layers, you can add an overlay [*in options useOverlay=true,
useAsyncOverlay=true, the second only for MapGuide>=2.1*] using the same
session, just changing show/hide layers string.
And yes, to come up with the following code was really hard!
NOTE: call in *Global.asax* MapGuideApi.MgInitializeWebTier(Webconfig Ini
Physical Path);

*Devs guide says this shoul be called on every request, I call it once in
Global.asax, and it works. I don't know more and haven't found any info!*



<%@ Page Language="C#" %>
<%@ Import Namespace="OSGeo.MapGuide" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    private string _strSessionId = "";
    private string _mapName = "";
    string _csvShowLayers = "";
    string _csvHideLayers = "";

    protected string GetMapName()
    {
        return _mapName;
    }
    protected string GetMapSession()
    {
        return _strSessionId;
    }
    protected string GetShowlayers()
    {
        return _csvShowLayers;
    }
    protected string GetHidelayers()
    {
        return _csvHideLayers;
    }

    private void MgDispose(MgDisposable obj)
    {
        if (obj != null) obj.Dispose();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        MgUserInformation userInfo = null;
        MgSiteConnection siteConnection = null;
        MgSite site = null;
        MgResourceIdentifier resourceId = null;
        MgMappingService mappingService = null;

        MgMap map = null;
        MgResourceService resourceService = null;
        MgSelection selection = null;

        MgLayerCollection lColl = null;
        MgLayerBase layerBase = null;
        MgResourceIdentifier sessionIdResourceIdentifier = null;

        try
        {
            userInfo = new MgUserInformation("Anonymous", "");
            siteConnection = new MgSiteConnection();
            siteConnection.Open(userInfo);

            site = siteConnection.GetSite();
            _strSessionId = site.CreateSession();

            //---------------------------------------------------
            //Save new mapguide session
            userInfo.SetMgSessionId(_strSessionId);
            //---------------------------------------------------

            resourceId = new
MgResourceIdentifier("Library://Samples/Sheboygan/Maps/Sheboygan.MapDefinition");
            _mapName = resourceId.GetName();

            //------------------------------------------------
            //Layers objectId
            mappingService =
(MgMappingService)siteConnection.CreateService(MgServiceType.MappingService);
            map = new MgMap();
            resourceService =
siteConnection.CreateService(MgServiceType.ResourceService) as
MgResourceService;
            map.Create(resourceService, resourceId, _mapName);

            lColl = map.GetLayers();
            int iMax = lColl.Count;


            //Let's show only Districts & Hydrography:
            StringBuilder sbShow = new StringBuilder();
            StringBuilder sbHide = new StringBuilder();
            int iHid = 0, iShow = 0;
            for (int i = 0; i < iMax; ++i)
            {
                if (null != layerBase) layerBase.Dispose();
                layerBase = lColl[i];
                string layername = layerBase.GetName();
                if (layername == "Districts")
                {
                    if (iShow > 0) sbShow.Append(",");
                    sbShow.Append(layerBase.GetObjectId());
                    ++iShow;
                }
                else if (layername == "Hydrography")
                {
                    if (iShow > 0) sbShow.Append(",");
                    sbShow.Append(layerBase.GetObjectId());
                    ++iShow;
                }
                else
                {
                    if (iHid > 0) sbHide.Append(",");
                    sbHide.Append(layerBase.GetObjectId());
                    ++iHid;
                }
            }
            _csvHideLayers = sbHide.ToString();
            _csvShowLayers = sbShow.ToString();
            //------------------------------------------------

            //------------------------------------------------
            //Necessary to show maps:
            selection = new MgSelection(map);
            selection.Save(resourceService, _mapName);

            sessionIdResourceIdentifier = new MgResourceIdentifier(
                String.Concat("Session:", _strSessionId, "//", _mapName,
".", MgResourceType.Map));
            map.Save(resourceService, sessionIdResourceIdentifier);
            //------------------------------------------------
        }
        catch
        {
            throw;//TODO: Handle exceptions
        }
        finally
        {
            MgDispose(sessionIdResourceIdentifier);
            MgDispose(selection);

            MgDispose(layerBase);
            MgDispose(lColl);

            MgDispose(resourceService);
            MgDispose(map);

            MgDispose(resourceId);
            MgDispose(mappingService);
            MgDispose(site);
            MgDispose(siteConnection);
            MgDispose(userInfo);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="
http://www.openlayers.org/api/OpenLayers.js"></script>
    <style type="text/css">
    #map_mg
    {
        width: 500px;
        height:500px;
        float: left;
        border:solid 1px #000;
    }
    </style>

</head>
<body onload="init()">
        <form id="form1" runat="server">
    <div>
        <h2>Qick example: TODO: Dispose all map guide objects, use
StringBuilder, etc...</h2>
        <h3>Showing only Districts/Hydrography</h3>
    </div>
        <div id="map_mg">
        </div>
    </form>

    <script type="text/javascript">
        var addrs = "http://localhost/"; //TODO: get the address of your
server
        var mg_url = addrs +
"mapguide/mapagent/mapagent.fcgi?USERNAME=Anonymous&";
        var metersPerUnit = 111319.4908;  //TODO: get value returned from
mapguide on server side
        var inPerUnit = OpenLayers.INCHES_PER_UNIT.m * metersPerUnit;
        OpenLayers.INCHES_PER_UNIT["dd"] = inPerUnit;
        OpenLayers.INCHES_PER_UNIT["degrees"] = inPerUnit;
        OpenLayers.DOTS_PER_INCH = 96;
        var _map = null;

        function init() {
            //TODO: get extent from mapguide on server side
            //      I usually get extent on server side, I don't write here
code, because Double's ToString
            //      in my locle [Italian] gives "," insted of ".", and I
have an assembly  to make conversion.
            //      You can find howto do it easily on Programmer's guide or
on WebApi help
            var extent = new OpenLayers.Bounds(-87.865114442365922,
43.665065564837931, -87.595394059497067, 43.823852564430069);
            var mapOptions = {
                maxExtent: extent,
                maxResolution: 'auto'
            };
            _map = new OpenLayers.Map('map_mg', mapOptions);

            var options = {
                isBaseLayer: true,
                buffer: 1,
                useOverlay: false,
                useAsyncOverlay: false,
                singleTile: true
                //, transitionEffect: 'resize'
            };
            var params = {};
            params.mapName = '<%= GetMapName() %>';
            params.session = '<%= GetMapSession() %>';
            params.hideLayers = '<%= GetHidelayers() %>';
            params.showLayers = '<%= GetShowlayers() %>';
            // - Depending on Groups you can use
params.showGroups/hideGroups
            // - Making a selection os server side, you can get that
selection and pass here as param.selectionXml
            // - I have read somewhere that locale should be passed [and
that in some cases is mandatory] too, but it works for me even without

            var mg_layer = new OpenLayers.Layer.MapGuide("MapGuide Sheboygan
map", mg_url, params, options);
            _map.addLayer(mg_layer);

            _map.addControl(new OpenLayers.Control.MousePosition());
            _map.zoomToMaxExtent();
        }
    </script>
</body>
</html>



Pietro Ianniello
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapguide-users/attachments/20100318/436cb703/attachment.html


More information about the mapguide-users mailing list