Skip to content

Instantly share code, notes, and snippets.

@johanvdw
Last active December 14, 2015 21:19
Show Gist options
  • Save johanvdw/5150653 to your computer and use it in GitHub Desktop.
Save johanvdw/5150653 to your computer and use it in GitHub Desktop.
<?php
try
{
include 'C:\Program Files\OSGEO\mapguide\www\mapadmin/constants.php';
$locale = "en"; // localizable string
$errorMsg = "";
// Initialize web tier with the site configuration file. The config
// file should be in the directory above as this script.
$webConfigFile = __FILE__;
$pos = strrpos($webConfigFile, '\\');
if ($pos == false)
$pos = strrpos($webConfigFile, '/');
$relativeLocation = '../'.MgConfigProperties::DefaultConfigurationFilename;
$webConfigFile = substr_replace($webConfigFile, $relativeLocation, $pos+1);
MgInitializeWebTier($webConfigFile);
}
catch ( MgException $e )
{
$errorMsg = $e->GetExceptionMessage();
echo $errorMsg;
}
catch ( Exception $e )
{
$errorMsg = $e->getMessage();
echo $errorMsg;
}
// Establish a connection with a MapGuide site.
$user = new MgUserInformation('Anonymous', '');
$siteConnection = new MgSiteConnection();
$siteConnection->Open($user);
// Create a session repository
$site = $siteConnection->GetSite();
$sessionID = $site->CreateSession();
$user->SetMgSessionId($sessionID);
// Get an instance of the required services.
$resourceService = $siteConnection->CreateService(MgServiceType::ResourceService);
$mappingService = $siteConnection->CreateService(MgServiceType::MappingService);
// Get a runtime map from a map definition
$resourceID = new MgResourceIdentifier('Library://Samples/Sheboygan/MapsTiled/Sheboygan.MapDefinition');
$map = new MgMap();
$map->Create($resourceService, $resourceID, 'SheboyganOL');
// Show information about the map
echo "Name of map: '" . $map->GetName() . "'n";
echo " Session ID of map: " . $map->GetSessionId() . "n";
echo " Object ID: " . $map->GetObjectId() . "n";
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>OpenLayers MapGuide Layer Example</title>
<style type="text/css">
#map
{
width: 80%;
height: 100%;
border: 1px solid black;
float: left;
}
#selectioninfo
{
width: 19%;
float:left;
}
td {
text-align: left;
background-color: transparent;
color: black;
font-family: Helvetica;
font-size: 8pt;
}
th {
background-color: Gray;
border: 1px outset white;
color: white;
font-family: Helvetica;
font-size: 9pt;
font-weight: bold;
}
</style>
<script src="OpenLayers-2.12/OpenLayers.js" type="text/javascript"></script>
<script src="jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
var map;
//Adjust the scale assumptions for MapGuide layers
//Tiled layers MUST use a DPI value of 96, untiled layers can use a
//different DPI value which will be passed to the server as a parameter.
//Tiled and untiled layers must adjust the OL INCHES_PER_UNIT values
//for any degree-based projections.
var metersPerUnit = 111319.4908; //value returned from mapguide
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 mapagent = "/mapguide/mapagent/mapagent.fcgi";
var mapdefinition = 'Library://Samples/Sheboygan/MapsTiled/Sheboygan.MapDefinition';
var a = $.get("createsessionandmap.php",
function (data) {
window.mapname = data.map;
window.session = data.sessionId;
initMap();
});
//Initialises the map after a session is created on the server
function initMap() {
var extent = new OpenLayers.Bounds(-87.765, 43.691, -87.695,43.7975);
var tempScales = [100000,51795,26827,13895,7197,3727.59,1930.69,1000];
var mapOptions = {
maxExtent: extent,
scales: tempScales
};
map = new OpenLayers.Map('map', mapOptions);
var params = {
mapdefinition: mapdefinition,
locale: 'en',
basemaplayergroupname: "Base Layer Group",
useOverlay: true,
useAsyncOverlay: true
};
var options = {
singleTile: false,
transitionEffect: 'resize'
};
var url = "/mapguide/mapagent/mapagent.fcgi?SESSION=" + session;
var layer = new OpenLayers.Layer.MapGuide("MapGuide OS tiled layer", url, params, options);
map.addLayer(layer);
var options = {
isBaseLayer: false,
useOverlay: true,
useAsyncOverlay: false,
buffer: 1,
singleTile: true,
transitionEffect: 'resize'
};
var params = {
session: session,
mapname: mapname,
locale: 'en',
version: '2.1.0',
selectioncolor: '0xFF000000',
behavior: 7
};
dynlayer = new OpenLayers.Layer.MapGuide("MapGuide OS Selection layer", url, params, options);
map.addLayer(dynlayer);
window.dynlayer = dynlayer;
//map control to dynamically switch on/off a layer
map.addControl(new OpenLayers.Control.LayerSwitcher());
//selection control: if shift is pressed a selection is made
var selectcontrol = new OpenLayers.Control();
OpenLayers.Util.extend(selectcontrol, {
draw: function () {
// this Handler.Box will intercept the shift-mousedown
// before Control.MouseDefault gets to see it
this.box = new OpenLayers.Handler.Box(selectcontrol,
{ "done": this.notice },
{ keyMask: OpenLayers.Handler.MOD_SHIFT });
this.box.activate();
},
notice: selectRectangle
});
map.addControl(selectcontrol);
map.zoomToMaxExtent();
window.map = map;
}
/**
Selection of a rectangle
*/
function selectRectangle(bounds) {
var ll = map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.left, bounds.bottom));
var ur = map.getLonLatFromPixel(new OpenLayers.Pixel(bounds.right, bounds.top));
var polygon = "POLYGON((" + ll.lon.toFixed(4) + " " +
ll.lat.toFixed(4) + ", " +
ll.lon.toFixed(4) + " " +
ur.lat.toFixed(4) + ", " +
ur.lon.toFixed(4) + " " +
ur.lat.toFixed(4) + ", " +
ur.lon.toFixed(4) + " " +
ll.lat.toFixed(4) + ", " +
ll.lon.toFixed(4) + " " +
ll.lat.toFixed(4) + "))"; ;
$.post(mapagent,
{ SESSION: session, MAPNAME: mapname, GEOMETRY: polygon,
'MAXFEATURES': '-1', OPERATION: 'QUERYMAPFEATURES', PERSIST: '1', SEQ: Math.random(), VERSION: '1.0.0', SELECTIONVARIANT: 'INTERSECTS', CLIENTAGENT: "ol"
},
showSelection, "xml"
);
}
function selectionJsonToTable(data) {
var table = $("<table width=100%>");
$.each(data, function (layername, v) {
var tr = $("<tr>");
tr.append($("<th colspan='2'>").append(layername));
table.append(tr);
$.each(this, function (k, v) {
$.each(v.values, function (k1, v1) {
var tr = $("<tr>");
tr.append($("<td>").append(v1.name));
tr.append($("<td>").append(v1.value));
table.append(tr);
})
;
table.append($("<tr><td colspan='2'><hr /></td></tr>"));
});
}
)
$("#selectioninfo").empty().append(table);
}
/**
Callback functie van selectRectangle (or others): refreshes the map and shows selection info in a table.
**/
function showSelection(data) {
dynlayer.redraw(true);
url = "/mapguide/mapviewerphp/getselectedfeatures.php";
$.get(url, { SESSION: session, MAPNAME: mapname, LOCALE: 'en', SEQ: Math.random() }, selectionJsonToTable, "json");
}
</script>
</head>
<body onload="">
<div id="map">
</div>
<div id="selectioninfo">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment