Counting Layers
Listing Layers
Adding a Layer
Linking Layers
Retrieving Keys of Selected Features
Retrieving Coordinates of a Selected Feature
Invoking Select Radius Mode
Toggling a Layer On and Off
Zooming in on Selected Features
Counting Map Features
Installing Observers for Plug-In and Java Edition
Invoking the Java Edition from HTML

Counting Layers

function countLayers()
{ 
    var map = getMap();
    var layers = map.getMapLayersEx();
    var cnt = layers.size();
    alert(cnt);
}

Listing Layers

function listLayers()
{
    var map = getMap();
    var layers = map.getMapLayersEx();
    var cnt = layers.size();
    var i;
    var msg = "";
    for (i = 0; i<cnt; i++)
    {
        var layer = layers.item(i);
        msg = msg + layer.getName() + "\n";
    }
    alert(msg);
}  

Adding a Layer

function doAddLayer()
{
    var map = getMap();
    map.addMapLayer("hydro.mlf", document.obs);
}  

Linking Layers

Autodesk MapGuide Author enables you to set map layer attribute properties for specific scale ranges. (Refer to "Specifying a Layer's Attribute Properties" in Autodesk MapGuide User's Guide.) For example, you might set a layer to be invisible when a user zooms out. Using the Viewer API, you can extend this functionality by linking layers to one or more designated control layers. If an action such as a zoom-out causes a control layer to become invisible, the API can make the linked layers invisible as well. You can include code to do this in the onViewChanging event:

function onViewChanging(map)
// States, Countries, and ZIP Codes are control layers.
// If one of them is visible, all other layers are too.
// If all of them are invisible, all other layers are too.
{
    var states = map.getMapLayer ("States");
    var countries = map.getMapLayer("Countries");
    var zipCodes = map.getMapLayer("ZIP Codes");
    var vis = (states.getVisibility()||counties.getVisibility() || zipCodes.getVisibility());
    var layers = map.getMapLayersEx();
    for (var i=0; i<.size(); i++)
    {
        var layer = layers.item(i);
        if (!layer.equals(states) && !layer.equals(counties) && !layer.equals(zipCodes))
        {
            layer.setVisibility(vis);
        }
    }
} 

Retrieving Keys of Selected Features

In this example, the doGetKey() function displays a dialog box showing the keys of selected map features. If no features are selected, an alert displays instead:

function doGetKey()
{
    var map = getMap();
    if (map.getSelection().getNumObjects() == 0)
    {
        alert ("Make selection first!");
        return;
    }
    var sel = map.getSelection();
    var objs = sel.getMapObjectsEx(null);
    var cntObjects = objs.size();
    var msg = "Keys of selected features are:\n";
    for (i=0;i<cntObjects;i++)
    {
        var obj=objs.item(i);
        var key=obj.getKey();
        msg=msg + obj.getMapLayer().getName() + " " + key + "\n";
    }
    alert(msg);
}

Retrieving Coordinates of a Selected Feature

This function demonstrates code that retrieves the coordinates of a selected feature if it is a particular type, in this case, a parcel feature:

function doGetCoordinates()
{
    var map=getMap();
    // Get selected features
    var sel=map.getSelection();
    // Get parcels layer
    var layer=map.getMapLayer("Parcels");
    if (layer== null)
    {
        alert("No Parcels layer found in this map!");
        return;
    }
    // Is there only one parcel selected?
    if ((sel.getNumObjects()> 1) || (sel.getNumObjects() == 0) || (sel.getMapObjectsEx(layer).size() == 0))
    {
        alert("Select only one parcel!");
        return;
    }
    // Get the parcel object
    var obj = sel.getMapObjectsEx(layer).item(0);
    // Get its vertices collection
    var vertices = map.createObject("MGCollection");
    var cntVertices = map.createObject("MGCollection");
    var res = obj.getVertices(vertices, cntVertices);
    if (res == 0)
    {
        alert("No access to coordinate information!");
        return;
    }
    // Get parcel coordinates
    msg = "Parcel:" + obj.getKey() + "\n";
    msg = msg + "Coordinates in MCS unit\n";
    for(var i=0; i<cntVertices.item(0); i++)
    {
        var pnt=vertices.item(i);
        msg=msg + pnt.getX() + "," + pnt.getY() + "\n";
    }
    alert(msg);
}  

Invoking Select Radius Mode

The doSelectRadius() function gets an instance of the map and uses that instance to call the selectRadiusMode API method:

function doSelectRadius()
{
    var map = getMap();
    map.selectRadiusMode();
} 

Toggling a Layer On and Off

function layerToggle(name)
{
    var map = getMap();
    var layer = map.getMapLayer(name);
    
    if (layer == null)
        alert("layer not found.");
    else
        layer.setVisibility(!layer.getVisibility());
        
    map.refresh();
} 

Zooming in on Selected Features

function zoomSelected()
{
    var map = getMap();
    var selected = map.getSelection().getMapObjectsEx(null);
    if (selected.size()>0)
        map.zoomSelected();
    else
        alert("nothing selected!");
} 

Counting Map Features

function showObjectCount()
{
    if (legendSet)
        return;
    var map = getMap();
    if (!map.isBusy())
    {
        var layers = map.getMapLayersEx();
        var cnt = layers.size();
        var i;
        var msg = "";
        for (i = 0; i<cnt; i++)
        {
            var layer = layers.item(i);
            var objectCount = layer.getMapObjectsEx().size();
            var label = layer.getLegendLabel();
            label = label + " " + objectCount + " features";
            layer.setLegendLabel(label);
        }
    }
    legendSet = true;
}    

Installing Observers for Plug-In and Java Edition

This example shows how check the Autodesk MapGuide Viewer version and install the appropriate event observer applet for Autodesk MapGuide Viewer Plug-In or Autodesk MapGuide Viewer, Java Edition. You can download the MGDetectClass.zip file from Plug-In/Java Edition Downloads.

<SCRIPT LANGUAGE="JavaScript">
// Embed the detect applet to check if the Java edition is installed 
document.write('<APPLET');
document.write(' CODEBASE="detect_class"');
document.write(' ARCHIVE="MGDetectClass.zip"');
document.write(' CODE=MGDetectClass');

// Extract result from detector
tempurl = document.URL;
index = tempurl.indexOf("DETECTED=");
result = tempurl.substring(index, tempurl.length);

if (result == "DETECTED=true")
{
    // The Java edition was installed, so we embed the Java edition Observer Applet
    // and name it obsJava 
    document.write("<Applet CODE=\"MapGuideObserver6J.class\" WIDTH=2 HEIGHT=2 NAME=\"obsJava\" MAYSCRIPT>");
    document.write("</Applet>");
	
    // After the page loads, the browser automatically calls the onLoad function. 
    // onLoad calls the setSelectionChangedObserver method from MGMap, providing the Java edition 
    // with the observer object that handles selection changed events.
    function onLoad()
    {
        if (navigator.appName() == "Netscape")
            getMap().setSelectionChangedObserver(document.obsJava);
    }

    function onSelectionChanged(map)
    {
        alert("Selection Changed");
    }
} 

// The Java edition was not installed so we check to see if the browser is Netscape
else if (navigator.appName() == "Netscape")
{
    // The browser is Netscape, so we embed the Plug-In Observer Applet
    document.write("<Java Applet CODE=\"MapGuideObserver6.class\" WIDTH=2 HEIGHT=2 NAME=\"obs\" MAYSCRIPT>");
    document.write("</Java Applet>");
   
    function onLoad()
    {
        if (navigator.appName() == "Netscape")
            getMap().setSelectionChangedObserver(document.obs);
    }
    
    function onSelectionChanged(map)
    {
        alert("Selection Changed");
    }
}
</SCRIPT>

Invoking the Java Edition from HTML

This example shows how to use the <APPLET> tag to display the Autodesk MapGuide Viewer, Java Edition in an HTML page.

<HTML>

<HEAD>
<TITLE>MapGuide Java Client Example</TITLE>
</HEAD>

<BODY>

<H2>Simple Invokation of Installed MapGuide Applet</H2>

<APPLET 
    WIDTH=500 
    HEIGHT=300 
    ALIGN="baseline"
    CODE="com/autodesk/mgjava/MGMapApplet.class">
    <PARAM 
        NAME="mwfUrl"
        VALUE="http://www.mapguide.com/maps/usa.mwf">
</APPLET>