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

Chris Gountanis cgountanis at mpowerinnovations.com
Thu Mar 18 17:50:30 EDT 2010


Pietro this helps and I will try this out. I have worked with Coldfusion and
.NET using MapGuide API and I have played with the MapGuide OL example till
I was blue in the face. This helps fill in the gaps between those. Your
options (useOverlay=true, useAsyncOverlay=true) with [show/hide] should at
least get me showing a map with the ability to turn on and off layers with
JavaScript. If we had more examples like this we might bring open source
back to mapguide open source J I will tell you what, time allowing as I get
basics as stated in my first post cleaned up and documented I will have no
problem reposting the summaries on MapGuide WIKI for others to benefit from.

 

Regards,

Chris Gountanis

 

 

From: Pietro Ianniello [via OSGeo.org]
[mailto:ml-node+4759356-1665868586-98072 at n2.nabble.com] 
Sent: Thursday, March 18, 2010 4:05 PM
To: Chris Gountanis
Subject: Re: MapGuide with Openlayers (Searching for Detailed yet Simple
Development Examples)

 

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.MapDefiniti
on");

            _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


_______________________________________________ 
mapguide-users mailing list 
[hidden email]
<http://n2.nabble.com/user/SendEmail.jtp?type=node&node=4759356&i=0>  
http://lists.osgeo.org/mailman/listinfo/mapguide-users



  _____  

View message @
http://n2.nabble.com/MapGuide-with-Openlayers-Searching-for-Detailed-yet-Sim
ple-Development-Examples-tp4757480p4759356.html 
To start a new topic under MapGuide Users, email
ml-node+1803227-182587645-98072 at n2.nabble.com 
To unsubscribe from MapGuide Users, click
< (link removed) 
3ZXJpbm5vdmF0aW9ucy5jb218MTgwMzIyN3wtMTY3MDQ3Mjg4>  here. 

 


-- 
View this message in context: http://n2.nabble.com/MapGuide-with-Openlayers-Searching-for-Detailed-yet-Simple-Development-Examples-tp4757480p4759592.html
Sent from the MapGuide Users mailing list archive at Nabble.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/mapguide-users/attachments/20100318/0ba00d6e/attachment.html


More information about the mapguide-users mailing list