MGSelection.addObject   Go to object model Go to parent topic Go to next topic

Syntax

void addObject(MGMapObject obj, boolean notify)

Description

Adds a single map feature to the selection and updates the display.

Note that this method does not work during a busy state. For additional information about the busy state, refer to "Handling Busy State and Map Refresh" in the Autodesk MapGuide Developer's Guide.

Parameters

obj - The map feature to add to the selection.

notify - If true, fires the onSelectionChanged event after the addition.

Error Codes

-1 (busy)

-3 (illegal argument)

Example

The following example is designed to work with the Tutorial.mwf file that is included with Autodesk MapGuide Author. It takes two strings -- a layer name and a feature key -- as its arguments. The function arguments are used with getMapLayer() and getMapObject() to get a map feature. That feature is then added to the selection and zoomed to:

function zoomTo(layerName, featureKey)
{
    var map = getMap();
    var sel = map.getSelection();
    // Get feature, using arguments passed down to function
    var feature = map.getMapLayer(layerName).getMapObject(featureKey);
	
    // Prevent busy state from happening when
    // zoomSelected() is called
    map.setAutoRefresh(false);

    // Clear existing selection (if any); then add feature to it
    sel.clear();
    sel.addObject(feature, false);
    
    // Zoom to feature, which is selected
    map.zoomSelected();
    map.setWidth(5000, "KM");

    // Reset autoRefresh flag; then update display
    map.setAutoRefresh(true);
    map.refresh();
}

You might use the following HTML to zoom to the New Zealand feature on the World Countries layer:

<FORM>
    <INPUT TYPE="button" VALUE="Toggle" ONCLICK="zoomTo(NZ">
</FORM>

Or, you could let the user pick the layer and feature:

    <FORM NAME="form01">
    Name: 
    <INPUT TYPE="text" NAME="layer" VALUE=""> 
    Feature: 
    <INPUT TYPE="text" NAME="feature" VALUE="">
    <INPUT TYPE="button" VALUE="Go!" ONCLICK="zoomTo(document.form01.layer.value, document.form01.feature.value);">
</FORM>

See Also

addObjectsEx