Posts Tagged ‘AJAX

11
Jun
13

Autodesk Infrastructure Map Server – Auto-Zoom On Load

Whether you are using AIMS 2014 or MapGuide OpenSource 2.5, there is a way to automatically zoom and highlight objects.

I will do this demo using FUSION or Flexible Web Layouts.

We want to be able to pass parameters to the template like the following.

http://localhost/mapserver2014/fusion/templates/mapguide/slate/index.html?LAYERNAME=Roads&KEYNAME=ID&KEY=644,684&ISSTRING=1

The parameters are:

  • LAYERNAME
  • KEYNAME
  • KEY
  • ISSTRING

The LAYERNAME is the layer name in the map (i.e. Roads)
The KEYNAME is the column you want to search by (i.e. ID)
The KEY is the comma delimited list of matches (i.e. 644,684)
The ISSTRING flag tells whether the KEYNAME is String or Number.

I am using the SLATE template in MapGuide/AIMS, found in C:\Program Files\Autodesk\Autodesk Infrastructure Web Server Extension 2014\www\fusion\templates\mapguide\slate\index.html

To start the process, we need to trigger a javascript function once the map is loaded, around line 174, add a call to a function called zoomToObject()

var initPanelHandler = function() {
zoomToObject();
if(isTaskPaneRegistered) return;

Next create a zoomToObject() function that will pass all the parameters to a PHP Page to get the selectionXML, (paste this above the var showOverviewMap = function() { line):

function zoomToObject()
{
var mapWidget = Fusion.getMapById('Map');
var SESSION =mapWidget.aMaps[0].getSessionID();
var MAPNAME = mapWidget.aMaps[0].getMapName();

var KEY=getParam('KEY');
var ISSTRING=getParam('ISSTRING');
var KEYNAME=getParam('KEYNAME');
var LAYERNAME=getParam('LAYERNAME');

//The GETSELECTIONXML.php returns the XML of selected features.
var AJAXURL = "/mapserver2014/GETSELECTIONXML.php?MAPNAME=" + MAPNAME;
AJAXURL = AJAXURL + "&SESSION=" + SESSION;
AJAXURL = AJAXURL + "&KEYNAME=" + KEYNAME;
AJAXURL = AJAXURL + "&LAYERNAME=" + LAYERNAME;
AJAXURL = AJAXURL + "&KEY=" + KEY;
AJAXURL = AJAXURL + "&ISSTRING=" + ISSTRING;

var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
mapWidget.setSelection(xmlhttp.responseText, true);
}
}
xmlhttp.open("GET",AJAXURL,true);
xmlhttp.send();

}

we also need the getParam() function to harvest the parameters sent to the page:


function getParam(sname)
{
var params = location.search.substr(location.search.indexOf("?")+1);
var sval = "";
params = params.split("&");
// split param and value into individual pieces
for (var i=0; i<params.length; i++)
{
temp = params[i].split("=");
if ( [temp[0]] == sname ) { sval = temp[1]; }
}
return sval;
}

Finally, the GETSELECTIONXML.php (I put this in the www folder of C:\Program Files\Autodesk\Autodesk Infrastructure Web Server Extension 2014\www\)

<?php

$configFilePath = "C:\Program Files\Autodesk\Autodesk Infrastructure Web Server Extension 2014\www\webconfig.ini";

$session = urldecode($_REQUEST["SESSION"]);
$mapName = urldecode($_REQUEST["MAPNAME"]);

$keyName = urldecode(stripslashes($_REQUEST["KEYNAME"]));
$layerName = urldecode(stripslashes($_REQUEST["LAYERNAME"]));
$isString = urldecode(stripslashes($_REQUEST["ISSTRING"]));

$key = urldecode(stripslashes($_REQUEST["KEY"]));

if($isString == true)
{
$key = $key . ',0';
$key = str_replace(",", "','", $key);
$key = "'" . $key . "'";
}

try

{
MgInitializeWebTier($configFilePath);

$userInfo = new MgUserInformation($session);

$siteConnection = new MgSiteConnection();

$siteConnection->Open($userInfo);

$featureService = $siteConnection->CreateService(2);

$resourceService = $siteConnection->CreateService(0);

$map = new MgMap($siteConnection);

$map->Open($mapName);

$queryOptions = new MgFeatureQueryOptions();

$queryOptions->SetFilter($keyName . " in (" . $key . ")");

$layer = $map->GetLayers()->GetItem($layerName);

$parcelDataResId = new MgResourceIdentifier($layer->GetFeatureSourceId());
$featureClassName = $layer->GetFeatureClassName();

$featureReader = $featureService->SelectFeatures($parcelDataResId, $featureClassName, $queryOptions);

$selection = new MgSelection($map);

$selection->AddFeatures($layer, $featureReader, 0);

$selectionXml = $selection->ToXml();

$selection->Save($resourceService, $map->GetName());

//dump the XML out to a JAVASCRIPT variable

echo $selectionXml;

}

catch (MgException $e)
{

echo $e->GetMessage();

echo $e->GetDetails();

}

?>


That’s it.  It will work with the sample data that comes with MapGuide and AIMS, Sheboygan, or with any map you wish.

This will work with any FUSION application (flexible web layout),  on any version of mapguide.  The only thing you have to change is this line:

$configFilePath = “C:\Program Files\Autodesk\Autodesk Infrastructure Web Server Extension 2014\www\webconfig.ini”;

Just point it to the correct www folder.

ZOOMTOOBJECT_MAPGUIDE

Advertisement
03
Mar
09

PHP and AJAX Caching Bug

Wow, this one “bugged” me for days. 

I have a PHP form that gets data from Oracle.  When I click the submit button, I want another page to process some script to keep a many-to-many table up to data.

For example:

<input type=submit onClick=submitDistricts($PUBID)>

The Javascript calls a GET

function submitDistricts(PUBID)
{
var xmlHttp = getXMLHttp();
    xmlHttp.onreadystatechange = function()
  {
    if(xmlHttp.readyState == 4)
    {
      HandleDistrictResponse(xmlHttp.responseText);
    }
  }
 xmlHttp.open(“GET”, “updateDistricts.php?PUBID=”+PUBID);
  xmlHttp.send(null);
}

And this works pretty well.  Except that the call gets cached and I can only submit it once.  YIKES!!

How did I fix it?  Well ensure the GET is always unique.

Change the line from:

xmlHttp.open(“GET”, “updateDistricts.php?PUBID=”+PUBID);

TO:

xmlHttp.open(“GET”, “updateDistricts.php?PUBID=”+PUBID + ‘&’ + Math.random());

That’s it. Now my AJAX call to the database is always unique and has no caching…

That was a weird one!




Gordon Luckett

Arrow Geomatics Inc's Gordon Luckett

Contact

gordon dot luckett at arrowgeo dot com 1-519-837-9500 (Arrow Geomatics Inc.)

Checkout MapGuide Guy’s Youtube Channel

gordonluckett@twitter